• Forum
  • Lounge
  • Setting DirectX up with an internet conn

 
Setting DirectX up with an internet connection

I have recetly bought a book that has been teaching me how to program a multiplayer FPS in DirectX and it set the network up as a LAN connection and I was wondering how I could set the code up so it connects to the internet and not lan, here is the class that handles the network (I had to keep the destructor and some other functions out because of the character limit):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

Network::Network( GUID guid, void (*HandleNetworkMessageFunction)( ReceivedMessage *msg ) )
{
	// Initialise the critical sections.
	InitializeCriticalSection( &m_sessionCS );
	InitializeCriticalSection( &m_playerCS );
	InitializeCriticalSection( &m_messageCS );

	// Invalidate the DirectPlay peer interface and the device.
	m_dpp = NULL;
	m_device = NULL;

	// Store the application's GUID.
	memcpy( &m_guid, &guid, sizeof( GUID ) );

	// Create the enumerated session list.
	m_sessions = new LinkedList< SessionInfo >;

	// Create the player list.
	m_players = new LinkedList< PlayerInfo >;

	// Create the network message list.
	m_messages = new LinkedList< ReceivedMessage >;

	// Load the network settings.
	Script *settings = new Script( "NetworkSettings.txt" );
	if( settings->GetNumberData( "processing_time" ) == NULL )
	{
		m_port = 2509;
		m_sendTimeOut = 100;
		m_processingTime = 100;
	}
	else
	{
		m_port = *settings->GetNumberData( "port" );
		m_sendTimeOut = *settings->GetNumberData( "send_time_out" );
		m_processingTime = *settings->GetNumberData( "processing_time" );
	}
	SAFE_DELETE( settings );

	// Initially the network is not allowed to receive application specific messages.
	m_receiveAllowed = false;

	// Set the network message handler.
	HandleNetworkMessage = HandleNetworkMessageFunction;

	// Create and initialise the DirectPlay peer interface.
	CoCreateInstance( CLSID_DirectPlay8Peer, NULL, CLSCTX_INPROC, IID_IDirectPlay8Peer, (void**)&m_dpp );
	m_dpp->Initialize( (PVOID)this, NetworkMessageHandler, DPNINITIALIZE_HINT_LANSESSION );

	// Create the device address.
	CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC, IID_IDirectPlay8Address, (LPVOID*) &m_device );
	m_device->SetSP( &CLSID_DP8SP_TCPIP );
	m_device->AddComponent( DPNA_KEY_PORT, &m_port, sizeof(DWORD), DPNA_DATATYPE_DWORD );
}


// Updates the network object, allowing it to process messages.

void Network::Update()
{
	EnterCriticalSection( &m_messageCS );

	ReceivedMessage *message = m_messages->GetFirst();

	unsigned long endTime = timeGetTime() + m_processingTime;
	while( endTime > timeGetTime() && message != NULL )
	{
		HandleNetworkMessage( message );
		m_messages->Remove( &message );
		message = m_messages->GetFirst();
	}

	LeaveCriticalSection( &m_messageCS );
}

// Enumerates for sessions on the local network.

void Network::EnumerateSessions()
{
	// Empty the lists.
	m_players->Empty();
	m_messages->Empty();
	m_sessions->Empty();

	// Prepare the application description.
	DPN_APPLICATION_DESC description;
	ZeroMemory( &description, sizeof( DPN_APPLICATION_DESC ) );
	description.dwSize = sizeof( DPN_APPLICATION_DESC );
	description.guidApplication = m_guid;

	// Enumerate sessions synchronously.
	m_dpp->EnumHosts( &description, NULL, m_device, NULL, 0, 1, 0, 0, NULL, NULL, DPNENUMHOSTS_SYNC );
}


// Attempts to host a session.

bool Network::Host( char *name, char *session, int players, void *playerData, unsigned long dataSize )
{
	WCHAR wide[MAX_PATH];

	// Prepare and set the player information structure.
	DPN_PLAYER_INFO player;
	ZeroMemory( &player, sizeof( DPN_PLAYER_INFO ) );
	player.dwSize = sizeof( DPN_PLAYER_INFO );
	player.pvData = playerData;
	player.dwDataSize = dataSize;
	player.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
	mbstowcs( wide, name, MAX_PATH );
	player.pwszName = wide;
	if( FAILED( m_dpp->SetPeerInfo( &player, NULL, NULL,DPNSETPEERINFO_SYNC ) ) )
		return false;

	// Prepare the application description.
	DPN_APPLICATION_DESC description;
	ZeroMemory( &description, sizeof( DPN_APPLICATION_DESC ) );
	description.dwSize = sizeof( DPN_APPLICATION_DESC );
	description.guidApplication = m_guid;
	description.dwMaxPlayers = players;
	mbstowcs( wide, session, MAX_PATH );
	description.pwszSessionName = wide;

	// Host the session.
	if( FAILED( m_dpp->Host( &description, &m_device, 1, NULL, NULL, NULL, 0 ) ) )
		return false;

	return true;
}


// Attempts to join a selected session from the enumerated session list.

bool Network::Join( char *name, int session, void *playerData, unsigned long dataSize )
{
	WCHAR wide[MAX_PATH];

	// Empty the player list and the newtork message.
	m_players->Empty();
	m_messages->Empty();

	// Ignore invalid sessions.
	if( session < 0 )
		return false;

	// Prepare and set the player information structure.
	DPN_PLAYER_INFO player;
	ZeroMemory( &player, sizeof( DPN_PLAYER_INFO ) );
	player.dwSize = sizeof( DPN_PLAYER_INFO );
	player.pvData = playerData;
	player.dwDataSize = dataSize;
	player.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
	mbstowcs( wide, name, MAX_PATH );
	player.pwszName = wide;
	if( FAILED( m_dpp->SetPeerInfo( &player, NULL, NULL, DPNSETPEERINFO_SYNC ) ) )
		return false;

	// Enter the sessions linked list critical section.
	EnterCriticalSection( &m_sessionCS );

	// Find the host of the selected session.
	m_sessions->Iterate( true );
	for( int s = 0; s < session + 1; s++ )
	{
		if( m_sessions->Iterate() ==  NULL )
		{
			LeaveCriticalSection( &m_sessionCS );
			return false;
		}
	}

	// Join the session.
	if( FAILED( m_dpp->Connect( &m_sessions->GetCurrent()->description, m_sessions->GetCurrent()->address, m_device, NULL, NULL, NULL, 0, NULL, NULL, NULL, DPNCONNECT_SYNC ) ) )
	{
		LeaveCriticalSection( &m_sessionCS );
		return false;
	}
	LeaveCriticalSection( &m_sessionCS );

	return true;
}

// Terminates the current session.

void Network::Terminate()
{
	// Only allow the host to terminate a session.
	if( m_dpnidHost == m_dpnidLocal )
		m_dpp->TerminateSession( NULL, 0, 0 );

	// Close the connection. This will also uninitialise the DirectPlay peer interface.
	if( m_dpp != NULL )
		m_dpp->Close( DPNCLOSE_IMMEDIATE );

	// Initialise the DirectPlay peer interface.
	m_dpp->Initialize( (PVOID)this, NetworkMessageHandler, DPNINITIALIZE_HINT_LANSESSION );
}

// Returns the next iterated session from the enumerated session list.

SessionInfo *Network::GetNextSession( bool restart )
{
	EnterCriticalSection( &m_sessionCS );

	m_sessions->Iterate( restart );
	if( restart == true )
		m_sessions->Iterate();

	LeaveCriticalSection( &m_sessionCS );

	return m_sessions->GetCurrent();
}


// Returns a pointer to the player information structure of the given player.

PlayerInfo *Network::GetPlayer( DPNID dpnid )
{
	EnterCriticalSection( &m_playerCS );

	m_players->Iterate( true );
	while( m_players->Iterate() )
	{
		if( m_players->GetCurrent()->dpnid == dpnid )
		{
			LeaveCriticalSection( &m_playerCS );

			return m_players->GetCurrent();
		}
	}

	LeaveCriticalSection( &m_playerCS );

	return NULL;
}

void Network::Send( void *data, long size, DPNID dpnid, long flags )
{
	DPNHANDLE hAsync;
	DPN_BUFFER_DESC dpbd;

	if( ( dpbd.dwBufferSize = size ) == 0 )
		return;
	dpbd.pBufferData = (BYTE*)data;

	m_dpp->SendTo( dpnid, &dpbd, 1, m_sendTimeOut, NULL, &hAsync, flags | DPNSEND_NOCOMPLETE | DPNSEND_COALESCE );
}
note: microsoft recommends to use winsock instead of directplay , because it is "considered deprecated"...

http://msdn.microsoft.com/en-us/library/bb153243%28VS.85%29.aspx

(did you buy an old book? - which DX-version?)
Last edited on
Yeah it was quite an old book, I couldn't find any new ones on the same subject (that had as many good reviews) and it was Dx version 9.0 I think, something like that, didn't know that microsoft has said that... Oh well, I guess I'll have to read up on winsock instead
Topic archived. No new replies allowed.