Debian Codegraph @

networking/directplay.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/directplay/directplay.cpp $
21 * $Revision: 12 $
22 * $Date: 4/14/99 1:12a $
23 * $Author: Jeff $
24 *
25 * Directplay API
26 *
27 * $Log: /DescentIII/Main/directplay/directplay.cpp $
28 *
29 * 12 4/14/99 1:12a Jeff
30 * fixed case mismatched #includes
31 *
32 * 11 2/16/99 12:36a Kevin
33 * Fixes for release builds of OEM V3 and KAtmai
34 *
35 * 10 2/15/99 1:22p Kevin
36 * Changes for GameGauge
37 *
38 * 9 1/21/99 11:15p Jeff
39 * pulled out some structs and defines from header files and moved them
40 * into seperate header files so that multiplayer dlls don't require major
41 * game headers, just those new headers. Side effect is a shorter build
42 * time. Also cleaned up some header file #includes that weren't needed.
43 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
44 * manage.h and multi.h
45 *
46 * 8 10/08/98 5:20p Kevin
47 * Fixed dependancies and fixed demo config
48 *
49 * 7 9/02/98 6:54p Kevin
50 * Fixed general directplay support up, and got modem-modem working
51 *
52 * 6 8/24/98 10:55a Kevin
53 * new directplay stuff
54 *
55 * 5 8/19/98 11:49a Kevin
56 * Got DirectPlay IPX working, and localized connection DLLs
57 *
58 * 4 8/14/98 4:54p Kevin
59 * More directplay stuff
60 *
61 * 3 8/13/98 6:36p Kevin
62 * checked that lpDirectPlay4A was non null
63 *
64 * 2 8/13/98 6:32p Kevin
65 * Initial implementation of directplay API
66 *
67 * 1 8/13/98 6:25p Kevin
68 *
69 */
70
71 #include <windows.h>
72 #include <winsock.h>
73 #include <wsipx.h>
74 #include <ras.h>
75 #include <objbase.h>
76
77 #include "descent.h"
78 #include "player.h"
79 #include "appdatabase.h"
80
81 #include "pstypes.h"
82 #include "pserror.h"
83 #include "mono.h"
84 #include "networking.h"
85 #include "ddio.h"
86 #include "mem.h"
87 #include "multi.h"
88
89 #include "directplay.h"
90
91 bool Use_DirectPlay = false;
92
93 DPSESSIONDESC2 Directplay_sessions[MAX_DP_GAMES];
94 int Num_directplay_games = 0;
95
96 char Directplay_session_desc[MAX_DP_GAMES][200];
97 bool Directplay_lobby_launched_game = false;
98 void *lpAddress = NULL;
99
100 // char Directplay_modem_list[MAX_MODEMS][200];
101 modem_list Modems_found[MAX_MODEMS];
102 int Num_modems_found = 0;
103
104 LPVOID lpConnectionBuffer;
105 LPDIRECTPLAY4A lpDirectPlay4A = NULL;
106 LPDIRECTPLAYLOBBY3A lpDirectPlayLobby3A = NULL;
107 LPDPLCONNECTION lpdplconnection = NULL;
108
109 // {915CE160-3125-11d2-BB3D-00104B27BFF0}
110 static const GUID DPD3_GUID = {0x915ce160, 0x3125, 0x11d2, {0xbb, 0x3d, 0x0, 0x10, 0x4b, 0x27, 0xbf, 0xf0}};
111
112 uint32_t Pending_dp_conn[MAX_PENDING_NEW_CONNECTIONS];
113
114 struct dpconnections {
115 char name[100];
116 void *conn;
117 };
118
119 dpconnections dpconns[MAX_DIRECTPLAY_CONNECTIONS];
120
121 BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(LPCGUID lpguidSP, LPVOID lpConnection, DWORD dwConnectionSize,
122 LPCDPNAME lpName, DWORD dwFlags, LPVOID lpContext) {
123
124 mprintf(0, "Directplay connection found: %s\n", lpName->lpszShortNameA);
125 // Do something with the list of connections
126 for (int i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
127 if (dpconns[i].conn == NULL) {
128 dpconns[i].conn = mem_malloc(dwConnectionSize);
129 memcpy(dpconns[i].conn, lpConnection, dwConnectionSize);
130 strcpy(dpconns[i].name, lpName->lpszShortNameA);
131 return (TRUE);
132 }
133 }
134 mprintf(0, "No space for Directplay entry.\n");
135
136 return (TRUE);
137 }
138
139 BOOL FAR PASCAL EnumSessionsCallback(LPCDPSESSIONDESC2 lpSessionDesc, LPDWORD lpdwTimeOut, DWORD dwFlags,
140 LPVOID lpContext) {
141 if (!lpSessionDesc)
142 return (FALSE);
143
144 if (Num_directplay_games < MAX_DP_GAMES) {
145 mprintf(0, "Found Directplay game[%d]: %s\n", Num_directplay_games, lpSessionDesc->lpszSessionNameA);
146 memcpy(&Directplay_sessions[Num_directplay_games], lpSessionDesc, sizeof(DPSESSIONDESC2));
147 strcpy(Directplay_session_desc[Num_directplay_games], lpSessionDesc->lpszSessionNameA);
148 Directplay_sessions[Num_directplay_games].lpszSessionNameA = Directplay_session_desc[Num_directplay_games];
149 /*
150 mprintf(0,"Found Directplay game[%d]: %s\n",
151 Num_directplay_games,
152 Directplay_sessions[Num_directplay_games].lpszSessionNameA);
153 */
154 Num_directplay_games++;
155 } else
156 return FALSE;
157
158 return (TRUE);
159 }
160
161 int dp_SelectDirectPlayConnection(char *name) {
162 HRESULT hr;
163 void *connection = NULL;
164 int i;
165 if (!lpDirectPlay4A)
166 return 0;
167 if (name) {
168 if (lpAddress)
169 mem_free(lpAddress);
170 lpAddress = NULL;
171 for (i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
172 if (stricmp(dpconns[i].name, name) == 0) {
173 mprintf(0, "Found DirectPlay connection: %s\n", name);
174 connection = dpconns[i].conn;
175 break;
176 }
177 }
178 if (!connection) {
179 mprintf(0, "Unable to find DirectPlay connection: %s\n", name);
180 return 0;
181 }
182 } else
183 connection = lpAddress;
184
185 memset(&dpconns, 0, sizeof(dpconnections) * MAX_DIRECTPLAY_CONNECTIONS);
186
187 hr = lpDirectPlay4A->InitializeConnection(connection, 0);
188
189 for (i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
190 if (dpconns[i].conn != NULL) {
191 mem_free(dpconns[i].conn);
192 dpconns[i].conn = NULL;
193 dpconns[i].name[0] = NULL;
194 }
195 }
196
197 if (FAILED(hr)) {
198 mprintf(0, "lpDirectPlay4A->InitializeConnection() failed.\n");
199 switch (hr) {
200 case DPERR_ALREADYINITIALIZED:
201 mprintf(0, "Error: DPERR_ALREADYINITIALIZED\n");
202 break;
203 case DPERR_INVALIDFLAGS:
204 mprintf(0, "Error: DPERR_INVALIDFLAGS\n");
205 break;
206 case DPERR_INVALIDPARAMS:
207 mprintf(0, "Error: DPERR_INVALIDPARAMS\n");
208 break;
209 case DPERR_UNAVAILABLE:
210 mprintf(0, "Error: DPERR_UNAVAILABLE\n");
211 break;
212 default:
213 mprintf(0, "Error: Unknown\n");
214 }
215
216 return 0;
217 } else
218 return 1;
219 }
220
221 int dp_InitDirectPlay(char *conn_name, void *parms, int num_elements) {
222 DWORD dwAddressSize;
223 HRESULT hr;
224 char *conn = conn_name;
225 for (int i = 0; i < MAX_PENDING_NEW_CONNECTIONS; i++)
226 Pending_dp_conn[i] = DPID_UNKNOWN;
227
228 // Delete any previous directplay instance
229 if (lpDirectPlay4A)
230 lpDirectPlay4A->Release();
231 if (SUCCEEDED(CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay4A,
232 (LPVOID *)&lpDirectPlay4A))) {
233 lpDirectPlay4A->EnumConnections(&DPD3_GUID, DirectPlayEnumConnectionsCallback, 0, 0);
234 } else {
235 lpDirectPlay4A = NULL;
236 }
237 if (parms) {
238 if (lpDirectPlayLobby3A) {
239 lpDirectPlayLobby3A->Release();
240 lpDirectPlayLobby3A = NULL;
241 }
242
243 hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A,
244 (LPVOID *)&lpDirectPlayLobby3A);
245 if (SUCCEEDED(hr)) {
246 hr = lpDirectPlayLobby3A->CreateCompoundAddress((LPDPCOMPOUNDADDRESSELEMENT)parms, num_elements, NULL,
247 &dwAddressSize);
248 if (hr == DPERR_BUFFERTOOSMALL) {
249 if (lpAddress)
250 mem_free(lpAddress);
251 lpAddress = mem_malloc(dwAddressSize);
252 hr = lpDirectPlayLobby3A->CreateCompoundAddress((LPDPCOMPOUNDADDRESSELEMENT)parms, num_elements, lpAddress,
253 &dwAddressSize);
254 if (FAILED(hr)) {
255 mprintf(0, "lpDirectPlayLobby3A->CreateCompoundAddress() failed. DirectPlay *NOT* initialized!\n");
256 return 0;
257 } else {
258 mprintf(0, "lpDirectPlayLobby3A->CreateCompoundAddress() succeeded!\n");
259 }
260 } else {
261 mprintf(0, "lpDirectPlayLobby3A->CreateCompoundAddress() buffer size request failed. DirectPlay *NOT* initialized!\n");
262 }
263 }
264 }
265 if (dp_SelectDirectPlayConnection(conn)) {
266 DP_active = TRUE;
267 mprintf(0, "DirectPlay Initialized\n");
268 return 1;
269 }
270 return 0;
271 }
272
273 // Everyone should call this when leaving the game.
274 void dp_EndGame() {
275 mprintf(0, "Ending DirectPlay game.\n");
276 if ((!DP_active) || (!lpDirectPlay4A))
277 return;
278 lpDirectPlay4A->Close();
279 lpDirectPlay4A->Release();
280 lpDirectPlay4A = NULL;
281 }
282
283 // This function gets called when a game is started, so Direcplay knows the title, and to listen
284 int dp_StartGame(char *gamename) {
285 HRESULT hres;
286 DPNAME server_player;
287 DPSESSIONDESC2 sessionDesc;
288
289 mprintf(0, "Starting DirectPlay game: %s\n", gamename);
290
291 if ((!DP_active) || (!lpDirectPlay4A)) {
292 mprintf(0, "Can't start game because DirectPlay isn't initialized!\n");
293 return 0;
294 }
295 // Host a new session.
296 ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
297 sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
298 sessionDesc.dwFlags =
299 DPSESSION_CLIENTSERVER | DPSESSION_KEEPALIVE | DPSESSION_OPTIMIZELATENCY | DPSESSION_DIRECTPLAYPROTOCOL;
300 sessionDesc.guidApplication = DPD3_GUID;
301 sessionDesc.dwMaxPlayers = MAX_NET_PLAYERS;
302 sessionDesc.dwCurrentPlayers = 1; // The server is a player
303 sessionDesc.lpszSessionNameA = gamename;
304
305 ZeroMemory(&server_player, sizeof(DPNAME));
306 server_player.dwSize = sizeof(DPNAME);
307 server_player.lpszShortNameA = Players[Player_num].callsign;
308 server_player.lpszLongNameA = NULL;
309
310 // if(lpAddress)
311 //{
312 // hres = lpDirectPlay4A->Open((DPSESSIONDESC2 *)lpAddress, DPOPEN_CREATE | DPOPEN_RETURNSTATUS);
313 //}
314 // else
315 //{
316 hres = lpDirectPlay4A->Open(&sessionDesc, DPOPEN_CREATE | DPOPEN_RETURNSTATUS);
317 //}
318
319 if (SUCCEEDED(hres)) {
320 NetPlayers[Player_num].hPlayerEvent = CreateEvent(NULL, // no security
321 FALSE, // auto reset
322 FALSE, // initial event reset
323 NULL); // no name
324 if (NetPlayers[Player_num].hPlayerEvent == NULL) {
325 mprintf(0, "Failed to create directplay notification event!\n");
326 dp_EndGame();
327 DP_active = false;
328 return 0;
329 }
330 hres = lpDirectPlay4A->CreatePlayer(&NetPlayers[Player_num].dpidPlayer, &server_player,
331 NetPlayers[Player_num].hPlayerEvent, NULL, 0, DPPLAYER_SERVERPLAYER);
332 if (FAILED(hres)) {
333 mprintf(0, "Failed to create directplay player in dp_StartGame()\n");
334 switch (hres) {
335 case DPERR_CANTADDPLAYER:
336 mprintf(0, "Unable to create player. Error code: DPERR_CANTADDPLAYER\n");
337 break;
338 case DPERR_CANTCREATEPLAYER:
339 mprintf(0, "Unable to create player. Error code: DPERR_CANTCREATEPLAYER\n");
340 break;
341 case DPERR_CONNECTIONLOST:
342 mprintf(0, "Unable to create player. Error code: DPERR_CONNECTIONLOST\n");
343 break;
344 case DPERR_INVALIDFLAGS:
345 mprintf(0, "Unable to create player. Error code: DPERR_INVALIDFLAGS\n");
346 break;
347 case DPERR_INVALIDPARAMS:
348 mprintf(0, "Unable to create player. Error code: DPERR_INVALIDPARAMS\n");
349 break;
350 case DPERR_NOCONNECTION:
351 mprintf(0, "Unable to create player. Error code: DPERR_NOCONNECTION\n");
352 break;
353 default:
354 mprintf(0, "Unable to create player. Error code: Unknown\n");
355 break;
356 }
357 return 0;
358 }
359 // Success
360 return 1;
361 } else {
362 switch (hres) {
363
364 case DPERR_ACCESSDENIED:
365 mprintf(0, "Failed to create Directplay session (DPERR_ACCESSDENIED)!\n");
366 break;
367 case DPERR_ALREADYINITIALIZED:
368 mprintf(0, "Failed to create Directplay session (DPERR_ALREADYINITIALIZED)!\n");
369 break;
370 case DPERR_AUTHENTICATIONFAILED:
371 mprintf(0, "Failed to create Directplay session (DPERR_AUTHENTICATIONFAILED)!\n");
372 break;
373 case DPERR_CANNOTCREATESERVER:
374 mprintf(0, "Failed to create Directplay session (DPERR_CANNOTCREATESERVER)!\n");
375 break;
376 case DPERR_CANTLOADCAPI:
377 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADCAPI)!\n");
378 break;
379 case DPERR_CANTLOADSECURITYPACKAGE:
380 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADSECURITYPACKAGE)!\n");
381 break;
382 case DPERR_CANTLOADSSPI:
383 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADSSPI)!\n");
384 break;
385 case DPERR_CONNECTING:
386 mprintf(0, "Failed to create Directplay session (DPERR_CONNECTING)!\n");
387 break;
388 case DPERR_CONNECTIONLOST:
389 mprintf(0, "Failed to create Directplay session (DPERR_CONNECTIONLOST)!\n");
390 break;
391 case DPERR_ENCRYPTIONFAILED:
392 mprintf(0, "Failed to create Directplay session (DPERR_ENCRYPTIONFAILED)!\n");
393 break;
394 case DPERR_ENCRYPTIONNOTSUPPORTED:
395 mprintf(0, "Failed to create Directplay session (DPERR_ENCRYPTIONNOTSUPPORTED)!\n");
396 break;
397 case DPERR_INVALIDFLAGS:
398 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDFLAGS)!\n");
399 break;
400 case DPERR_INVALIDPARAMS:
401 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDPARAMS)!\n");
402 break;
403 case DPERR_INVALIDPASSWORD:
404 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDPASSWORD)!\n");
405 break;
406 case DPERR_LOGONDENIED:
407 mprintf(0, "Failed to create Directplay session (DPERR_LOGONDENIED)!\n");
408 break;
409 case DPERR_NOCONNECTION:
410 mprintf(0, "Failed to create Directplay session (DPERR_NOCONNECTION)!\n");
411 break;
412 case DPERR_NONEWPLAYERS:
413 mprintf(0, "Failed to create Directplay session (DPERR_NONEWPLAYERS)!\n");
414 break;
415 case DPERR_NOSESSIONS:
416 mprintf(0, "Failed to create Directplay session (DPERR_NOSESSIONS)!\n");
417 break;
418 case DPERR_SIGNFAILED:
419 mprintf(0, "Failed to create Directplay session (DPERR_SIGNFAILED)!\n");
420 break;
421 case DPERR_TIMEOUT:
422 mprintf(0, "Failed to create Directplay session (DPERR_TIMEOUT)!\n");
423 break;
424 case DPERR_UNINITIALIZED:
425 mprintf(0, "Failed to create Directplay session (DPERR_UNINITIALIZED)!\n");
426 break;
427 case DPERR_USERCANCEL:
428 mprintf(0, "Failed to create Directplay session (DPERR_USERCANCEL)!\n");
429 break;
430 default:
431 mprintf(0, "Failed to create Directplay session (unknown)!\n");
432 }
433 }
434 return 0;
435 }
436
437 int dp_ListDirectPlayGames() {
438 DPSESSIONDESC2 sessionDesc;
439 HRESULT hr;
440 if (!DP_active)
441 return DPERR_EXCEPTION;
442 if (!lpDirectPlay4A)
443 return DPERR_EXCEPTION;
444
445 Num_directplay_games = 0;
446 // Search for this kind of session.
447 ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
448 sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
449 sessionDesc.guidApplication = DPD3_GUID;
450
451 hr = lpDirectPlay4A->EnumSessions(&sessionDesc, 0, EnumSessionsCallback, 0,
452 DPENUMSESSIONS_AVAILABLE | DPENUMSESSIONS_RETURNSTATUS | DPENUMSESSIONS_ASYNC);
453 if (hr == DPERR_CONNECTING) {
454 mprintf(0, "Waiting for EnumSessions() to connect.\n");
455 return DPERR_CONNECTING;
456 }
457
458 if FAILED (hr) {
459 mprintf(0, "Unable to EnumSessions() for Directplay.\n");
460 switch (hr) {
461 case DPERR_CONNECTING:
462 mprintf(0, "Error code: DPERR_CONNECTING\n");
463 break;
464 case DPERR_CONNECTIONLOST:
465 mprintf(0, "Error code: DPERR_CONNECTIONLOST\n");
466 break;
467 case DPERR_EXCEPTION:
468 mprintf(0, "Error code: DPERR_EXCEPTION\n");
469 break;
470 case DPERR_GENERIC:
471 mprintf(0, "Error code: DPERR_GENERIC\n");
472 break;
473 case DPERR_INVALIDOBJECT:
474 mprintf(0, "Error code: DPERR_INVALIDOBJECT\n");
475 break;
476 case DPERR_INVALIDPARAMS:
477 mprintf(0, "Error code: DPERR_INVALIDPARAMS\n");
478 break;
479 case DPERR_NOCONNECTION:
480 mprintf(0, "Error code: DPERR_NOCONNECTION\n");
481 break;
482 case DPERR_UNINITIALIZED:
483 mprintf(0, "Error code: DPERR_UNINITIALIZED\n");
484 break;
485 case DPERR_USERCANCEL:
486 mprintf(0, "Error code: DPERR_USERCANCEL\n");
487 break;
488 default:
489 mprintf(0, "Error code: Unknown\n");
490 break;
491 }
492 return DPERR_NOCONNECTION;
493 } else
494 mprintf(0, "EnumSessions() returned DP_OK\n");
495
496 return DP_OK;
497 }
498
499 // This function will look for incoming messages, and dispatch them accordingly
500 void dp_DirectPlayDispatch() {
501 HRESULT hr;
502 uint8_t packet_data[32000]; // MAX_PACKET_SIZE+1];
503 DWORD dwMsgBufferSize = 32000; // MAX_PACKET_SIZE;
504 DPID idFrom = 0;
505 DPID idTo = 0;
506 network_address from_addr;
507 LPDPMSG_CREATEPLAYERORGROUP new_player_msg;
508 // Put messages in the buffers
509 if ((!DP_active) || (!lpDirectPlay4A)) {
510 mprintf(0, "Can't dispatch DirectPlay message because DirectPlay isn't active!\n");
511 return;
512 }
513
514 hr = lpDirectPlay4A->Receive(&idFrom, &idTo, DPRECEIVE_ALL, packet_data, &dwMsgBufferSize);
515 if (FAILED(hr)) {
516 switch (hr) {
517 case DPERR_BUFFERTOOSMALL:
518 mprintf(0, "Error: lpDirectPlay4A->Receive() returned DPERR_BUFFERTOOSMALL -- needs %d\n", dwMsgBufferSize);
519 break;
520 case DPERR_GENERIC:
521 mprintf(0, "Error: lpDirectPlay4A->Receive() returned DPERR_GENERIC\n");
522 break;
523 case DPERR_INVALIDOBJECT:
524 mprintf(0, "Error: lpDirectPlay4A->Receive() returned DPERR_INVALIDOBJECT\n");
525 break;
526 case DPERR_INVALIDPARAMS:
527 mprintf(0, "Error: lpDirectPlay4A->Receive() returned DPERR_INVALIDPARAMS\n");
528 break;
529 case DPERR_INVALIDPLAYER:
530 mprintf(0, "Error: lpDirectPlay4A->Receive() returned DPERR_INVALIDPLAYER\n");
531 break;
532 case DPERR_NOMESSAGES:
533 // Not really an error....
534 // mprintf(0,"Error: lpDirectPlay4A->Receive() returned DPERR_NOMESSAGES\n");
535 break;
536 default:
537 mprintf(0, "Unknown error code from DirectPlay::Receive() -- %x\n", hr);
538 }
539 } else {
540 // Now we have actual data -- do something with it.
541 if (idFrom == DPID_SYSMSG) {
542 // This is a system message
543 LPDPMSG_GENERIC lpMsg = (LPDPMSG_GENERIC)packet_data;
544 switch (lpMsg->dwType) {
545 case DPSYS_CREATEPLAYERORGROUP:
546 // New player joined the game
547 {
548 mprintf(0, "Got a DPSYS_CREATEPLAYERORGROUP packet!\n");
549 new_player_msg = (LPDPMSG_CREATEPLAYERORGROUP)lpMsg;
550 // Store this player's info
551 for (int i = 0; i < MAX_PENDING_NEW_CONNECTIONS; i++) {
552 if (Pending_dp_conn[i] == DPID_UNKNOWN) {
553 // new connection slot
554 Pending_dp_conn[i] = new_player_msg->dpId;
555 break;
556 }
557 }
558 }
559 break;
560 case DPSYS_DESTROYPLAYERORGROUP:
561 // Player left the game
562 break;
563 case DPSYS_ADDPLAYERTOGROUP:
564 case DPSYS_DELETEPLAYERFROMGROUP:
565 case DPSYS_SESSIONLOST:
566 case DPSYS_HOST:
567 case DPSYS_SETPLAYERORGROUPDATA:
568 case DPSYS_SETPLAYERORGROUPNAME:
569 case DPSYS_SENDCOMPLETE:
570 break;
571 default:
572 Int3(); // Get Kevin
573 mprintf(0, "Unknown system message from DirectPlay! %d\n", lpMsg->dwType);
574 }
575 } else {
576 if (dwMsgBufferSize >= MAX_PACKET_SIZE) {
577 mprintf(0, "Error: Packet too large for D3 to handle! Discarding!!!!!\n");
578 } else {
579 // Presumably this is from a player we know about...
580 memset(&from_addr, 0, sizeof(network_address));
581 from_addr.connection_type = NP_DIRECTPLAY;
582 memcpy(from_addr.address, &idFrom, sizeof(DPID));
583 nw_psnet_buffer_packet((uint8_t *)packet_data, dwMsgBufferSize, &from_addr);
584 }
585 }
586 }
587 }
588
589 // Send a packet to a direct play user
590 int dp_DirectPlaySend(network_address *who_to, uint8_t *data, int len, bool reliable) {
591 DPID idTo;
592 HRESULT hr;
593 uint32_t send_flags;
594 if ((!DP_active) || (!lpDirectPlay4A)) {
595 mprintf(0, "Can't send DirectPlay message because DirectPlay isn't active!\n");
596 return 0;
597 }
598
599 ASSERT(who_to->connection_type == NP_DIRECTPLAY);
600
601 send_flags = DPSEND_ASYNC;
602 if (reliable)
603 send_flags |= DPSEND_GUARANTEED;
604
605 memcpy(&idTo, who_to->address, sizeof(DPID));
606 hr = lpDirectPlay4A->SendEx(NetPlayers[Player_num].dpidPlayer, idTo, send_flags, data, len, 0, 0, NULL, NULL);
607
608 if (FAILED(hr))
609 return 0;
610 else
611 return 1;
612 }
613
614 void dp_DirectPlayDestroyPlayer(DPID who) {
615 HRESULT hr;
616
617 if ((!DP_active) || (!lpDirectPlay4A))
618 return;
619
620 hr = lpDirectPlay4A->DestroyPlayer(who);
621 if (FAILED(hr)) {
622 switch (hr) {
623 case DPERR_ACCESSDENIED:
624 mprintf(0, "lpDirectPlay4A->DestroyPlayer() returned DPERR_ACCESSDENIED.\n");
625 break;
626 case DPERR_CONNECTIONLOST:
627 mprintf(0, "lpDirectPlay4A->DestroyPlayer() returned DPERR_CONNECTIONLOST.\n");
628 break;
629 case DPERR_INVALIDOBJECT:
630 mprintf(0, "lpDirectPlay4A->DestroyPlayer() returned DPERR_INVALIDOBJECT.\n");
631 break;
632 case DPERR_INVALIDPLAYER:
633 mprintf(0, "lpDirectPlay4A->DestroyPlayer() returned DPERR_INVALIDPLAYER.\n");
634 break;
635 case DPERR_UNAVAILABLE:
636 mprintf(0, "lpDirectPlay4A->DestroyPlayer() returned DPERR_UNAVAILABLE.\n");
637 break;
638 default:
639 mprintf(0, "Unknown error returned by nw_DirectPlayDestroyPlayer() -- %x\n", hr);
640 }
641 }
642 }
643
644 // if(Use_DirectPlay)
645
646 int dp_DirectPlayJoinGame(LPDPSESSIONDESC2 session) {
647 HRESULT hr;
648 do {
649 hr = lpDirectPlay4A->Open(session, DPOPEN_JOIN | DPOPEN_RETURNSTATUS);
650 if (DPERR_CONNECTING == hr) {
651 mprintf(0, "Waiting for Directplay session to start(DPERR_CONNECTING)!\n");
652 Sleep(50);
653 }
654 } while (DPERR_CONNECTING == hr);
655 if (FAILED(hr)) {
656 switch (hr) {
657
658 case DPERR_ACCESSDENIED:
659 mprintf(0, "Failed to create Directplay session (DPERR_ACCESSDENIED)!\n");
660 break;
661 case DPERR_ALREADYINITIALIZED:
662 mprintf(0, "Failed to create Directplay session (DPERR_ALREADYINITIALIZED)!\n");
663 break;
664 case DPERR_AUTHENTICATIONFAILED:
665 mprintf(0, "Failed to create Directplay session (DPERR_AUTHENTICATIONFAILED)!\n");
666 break;
667 case DPERR_CANNOTCREATESERVER:
668 mprintf(0, "Failed to create Directplay session (DPERR_CANNOTCREATESERVER)!\n");
669 break;
670 case DPERR_CANTLOADCAPI:
671 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADCAPI)!\n");
672 break;
673 case DPERR_CANTLOADSECURITYPACKAGE:
674 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADSECURITYPACKAGE)!\n");
675 break;
676 case DPERR_CANTLOADSSPI:
677 mprintf(0, "Failed to create Directplay session (DPERR_CANTLOADSSPI)!\n");
678 break;
679 case DPERR_CONNECTIONLOST:
680 mprintf(0, "Failed to create Directplay session (DPERR_CONNECTIONLOST)!\n");
681 break;
682 case DPERR_ENCRYPTIONFAILED:
683 mprintf(0, "Failed to create Directplay session (DPERR_ENCRYPTIONFAILED)!\n");
684 break;
685 case DPERR_ENCRYPTIONNOTSUPPORTED:
686 mprintf(0, "Failed to create Directplay session (DPERR_ENCRYPTIONNOTSUPPORTED)!\n");
687 break;
688 case DPERR_INVALIDFLAGS:
689 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDFLAGS)!\n");
690 break;
691 case DPERR_INVALIDPARAMS:
692 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDPARAMS)!\n");
693 break;
694 case DPERR_INVALIDPASSWORD:
695 mprintf(0, "Failed to create Directplay session (DPERR_INVALIDPASSWORD)!\n");
696 break;
697 case DPERR_LOGONDENIED:
698 mprintf(0, "Failed to create Directplay session (DPERR_LOGONDENIED)!\n");
699 break;
700 case DPERR_NOCONNECTION:
701 mprintf(0, "Failed to create Directplay session (DPERR_NOCONNECTION)!\n");
702 break;
703 case DPERR_NONEWPLAYERS:
704 mprintf(0, "Failed to create Directplay session (DPERR_NONEWPLAYERS)!\n");
705 break;
706 case DPERR_NOSESSIONS:
707 mprintf(0, "Failed to create Directplay session (DPERR_NOSESSIONS)!\n");
708 break;
709 case DPERR_SIGNFAILED:
710 mprintf(0, "Failed to create Directplay session (DPERR_SIGNFAILED)!\n");
711 break;
712 case DPERR_TIMEOUT:
713 mprintf(0, "Failed to create Directplay session (DPERR_TIMEOUT)!\n");
714 break;
715 case DPERR_UNINITIALIZED:
716 mprintf(0, "Failed to create Directplay session (DPERR_UNINITIALIZED)!\n");
717 break;
718 case DPERR_USERCANCEL:
719 mprintf(0, "Failed to create Directplay session (DPERR_USERCANCEL)!\n");
720 break;
721 default:
722 mprintf(0, "Failed to create Directplay session (unknown)!\n");
723 }
724 // return 0;
725 } else {
726 HRESULT hres;
727 DPNAME client_player;
728 ZeroMemory(&client_player, sizeof(DPNAME));
729 client_player.dwSize = sizeof(DPNAME);
730 client_player.lpszShortNameA = Players[Player_num].callsign;
731 client_player.lpszLongNameA = NULL;
732
733 mprintf(0, "Creating local player...\n");
734 NetPlayers[Player_num].hPlayerEvent = CreateEvent(NULL, // no security
735 FALSE, // auto reset
736 FALSE, // initial event reset
737 NULL); // no name
738 if (NetPlayers[Player_num].hPlayerEvent == NULL) {
739 mprintf(0, "Failed to create directplay notification event!\n");
740 dp_EndGame();
741 DP_active = false;
742 return 0;
743 }
744 hres = lpDirectPlay4A->CreatePlayer(&NetPlayers[Player_num].dpidPlayer, &client_player,
745 NetPlayers[Player_num].hPlayerEvent, NULL, 0, 0);
746 if (FAILED(hres)) {
747 mprintf(0, "Failed to create directplay player in dp_DirectPlayJoinGame()\n");
748 switch (hres) {
749 case DPERR_CANTADDPLAYER:
750 mprintf(0, "Unable to create player. Error code: DPERR_CANTADDPLAYER\n");
751 break;
752 case DPERR_CANTCREATEPLAYER:
753 mprintf(0, "Unable to create player. Error code: DPERR_CANTCREATEPLAYER\n");
754 break;
755 case DPERR_CONNECTIONLOST:
756 mprintf(0, "Unable to create player. Error code: DPERR_CONNECTIONLOST\n");
757 break;
758 case DPERR_INVALIDFLAGS:
759 mprintf(0, "Unable to create player. Error code: DPERR_INVALIDFLAGS\n");
760 break;
761 case DPERR_INVALIDPARAMS:
762 mprintf(0, "Unable to create player. Error code: DPERR_INVALIDPARAMS\n");
763 break;
764 case DPERR_NOCONNECTION:
765 mprintf(0, "Unable to create player. Error code: DPERR_NOCONNECTION\n");
766 break;
767 default:
768 mprintf(0, "Unable to create player. Error code: Unknown\n");
769 break;
770 }
771 return 0;
772 }
773 return 1;
774 }
775 return 1;
776 }
777
778 void dp_ShutdownDirectPlay() {
779 int i;
780 mprintf(0, "Freeing DirectPlay memory\n");
781 for (i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
782 if (dpconns[i].conn != NULL) {
783 mem_free(dpconns[i].conn);
784 dpconns[i].conn = NULL;
785 dpconns[i].name[0] = NULL;
786 }
787 }
788 if (lpdplconnection)
789 mem_free(lpdplconnection);
790 if (lpAddress)
791 mem_free(lpAddress);
792 lpAddress = NULL;
793 lpdplconnection = NULL;
794 // Delete any previous directplay instance
795 mprintf(0, "Releasing DirectPlay COM object (lpDirectPlay4A)\n");
796 // if(lpDirectPlay4A)
797 // lpDirectPlay4A->Release();
798 // lpDirectPlay4A = NULL;
799 mprintf(0, "Done shutting down DirectPlay.\n");
800 }
801
802 // ---------------------------------------------------------------------------
803 // EnumModemAddress
804 // ---------------------------------------------------------------------------
805 // Description: Enumeration callback called by DirectPlayLobby.
806 // Enumerates the DirectPlay address chunks. If the
807 // chunk contains modem strings, add them to the control.
808 // Arguments:
809 // REFGUID [in] GUID of the address type
810 // DWORD [in] size of chunk
811 // LPVOID [in] pointer to chunk
812 // LPVOID [in] user-defined context
813 // Returns:
814 // BOOL FALSE to stop enumerating after the first callback
815 BOOL FAR PASCAL EnumModemAddress(REFGUID lpguidDataType, DWORD dwDataSize, LPCVOID lpData, LPVOID lpContext) {
816 HWND hWnd = (HWND)lpContext;
817 LPSTR lpszStr = (LPSTR)lpData;
818
819 // modem
820 if (IsEqualGUID(lpguidDataType, DPAID_Modem)) {
821 // loop over all strings in list
822 while (strlen(lpszStr)) {
823 mprintf(0, "Found modem: %s\n", lpszStr);
824 if (Num_modems_found < MAX_MODEMS) {
825 // store modem name
826 strcpy(Modems_found[Num_modems_found].name, lpszStr);
827 Num_modems_found++;
828 }
829 // skip to next string
830 lpszStr += strlen(lpszStr) + 1;
831 }
832 }
833 return (TRUE);
834 }
835
836 // char Directplay_modem_list[MAX_MODEMS][200];
837 // int Num_modems_found = 0;
838
839 // Call this function with size set to 0, and it will fill in size with
840 // the amount of buffer space you need
841 // Otherwise, it will fill in the buffer with a bunch of null delimited
842 // strings, with a double null at the end.
843 int dp_GetModemChoices(char *buffer, LPDWORD size) {
844 LPDIRECTPLAY4A lpTempDP4;
845 HRESULT hr;
846 void *connection = NULL;
847 ;
848 int i;
849 Num_modems_found = 0;
850 if (SUCCEEDED(
851 CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay4A, (LPVOID *)&lpTempDP4))) {
852
853 lpTempDP4->EnumConnections(&DPD3_GUID, DirectPlayEnumConnectionsCallback, 0, 0);
854
855 for (i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
856 if (stricmp(dpconns[i].name, "Modem Connection For DirectPlay") == 0) {
857 // mprintf(0,"Found DirectPlay connection: %s\n",name);
858 connection = dpconns[i].conn;
859 break;
860 }
861 }
862 if (!connection) {
863 mprintf(0, "Unable to find DirectPlay Modem connection.\n");
864 return 0;
865 }
866
867 memset(&dpconns, 0, sizeof(dpconnections) * MAX_DIRECTPLAY_CONNECTIONS);
868
869 hr = lpTempDP4->InitializeConnection(connection, 0);
870
871 for (i = 0; i < MAX_DIRECTPLAY_CONNECTIONS; i++) {
872 if (dpconns[i].conn != NULL) {
873 mem_free(dpconns[i].conn);
874 dpconns[i].conn = NULL;
875 dpconns[i].name[0] = NULL;
876 }
877 }
878
879 if (FAILED(hr)) {
880 mprintf(0, "lpTempDP4->InitializeConnection() failed.\n");
881 switch (hr) {
882 case DPERR_ALREADYINITIALIZED:
883 mprintf(0, "Error: DPERR_ALREADYINITIALIZED\n");
884 break;
885 case DPERR_INVALIDFLAGS:
886 mprintf(0, "Error: DPERR_INVALIDFLAGS\n");
887 break;
888 case DPERR_INVALIDPARAMS:
889 mprintf(0, "Error: DPERR_INVALIDPARAMS\n");
890 break;
891 case DPERR_UNAVAILABLE:
892 mprintf(0, "Error: DPERR_UNAVAILABLE\n");
893 break;
894 default:
895 mprintf(0, "Error: Unknown\n");
896 }
897
898 return 0;
899 }
900 lpTempDP4->GetPlayerAddress(0, buffer, size);
901 if (buffer) {
902 if (lpDirectPlayLobby3A) {
903 lpDirectPlayLobby3A->Release();
904 lpDirectPlayLobby3A = NULL;
905 }
906 hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A,
907 (LPVOID *)&lpDirectPlayLobby3A);
908 if (SUCCEEDED(hr))
909 lpDirectPlayLobby3A->EnumAddress(EnumModemAddress, buffer, *size, 0);
910 else
911 mprintf(0, "Couldn't create IID_IDirectPlayLobby3A COM Instance!\n");
912 }
913
914 lpTempDP4->Release();
915 return 1;
916 }
917 return 0;
918 }
919
920 // Register a DirectPlay lobby aware application
921 // Use this so a directplay lobby provider such as zone.com can launch the game
922 //
923 // Parameters:
924 // appname The non-localized name of the application (ie. "Descent 3") DON'T LOCALIZE THIS! IT IS AN ID
925 // exefile Executable file name (without path)
926 // exepath Path to executable file
927 // arguments Any command line arguments the app needs
928 // workingdir The Working directory for the application
929 // description Localized description of the application
930 void dp_RegisterLobbyApplication(char *appname, char *exefile, char *exepath, char *arguments, char *workingdir,
931 char *description) {
932 static DPAPPLICATIONDESC AppDesc;
933
934 AppDesc.dwSize = sizeof(DPAPPLICATIONDESC);
935 AppDesc.dwFlags = 0;
936 AppDesc.lpszApplicationNameA = appname;
937 AppDesc.guidApplication = DPD3_GUID;
938 AppDesc.lpszFilenameA = exefile;
939 AppDesc.lpszCommandLineA = arguments;
940 AppDesc.lpszPathA = exepath;
941 AppDesc.lpszCurrentDirectoryA = workingdir;
942 AppDesc.lpszDescriptionA = description;
943
944 HRESULT hr;
945
946 mprintf(0, "Registering DirectPlay Lobby application:\n");
947 mprintf(0, "AppDesc.lpszApplicationNameA = %s\n", appname);
948 mprintf(0, "AppDesc.lpszFilenameA = %s\n", exefile);
949 mprintf(0, "AppDesc.lpszCommandLineA = %s\n", arguments);
950 mprintf(0, "AppDesc.lpszPathA = %s\n", exepath);
951 mprintf(0, "AppDesc.lpszCurrentDirectoryA = %s\n", workingdir);
952 mprintf(0, "AppDesc.lpszDescriptionA = %s\n", description);
953
954 hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A,
955 (LPVOID *)&lpDirectPlayLobby3A);
956 if (SUCCEEDED(hr)) {
957 hr = lpDirectPlayLobby3A->RegisterApplication(0, &AppDesc);
958 if (FAILED(hr)) {
959 mprintf(0, "IDirectPlayLobby3::RegisterApplication() failed.\n");
960 if (hr == DPERR_INVALIDFLAGS)
961 mprintf(0, "Error = DPERR_INVALIDFLAGS\n");
962 else if (hr == DPERR_INVALIDPARAMS)
963 mprintf(0, "Error = DPERR_INVALIDPARAMS\n");
964 else
965 mprintf(0, "Error = ??????????? %x\n", hr);
966 } else {
967 mprintf(0, "IDirectPlayLobby3::RegisterApplication() succeded.\n");
968 }
969 } else {
970 mprintf(0, "IDirectPlayLobby3 CoCreateInstance() failed.\n");
971 return;
972 }
973
974 // lpDirectPlayLobby3A->Release();
975 }
976
977 // Returns TRUE if the game was launched from a lobby
978 bool dp_DidLobbyLaunchGame() {
979 HRESULT hr;
980 DWORD buffersize = sizeof(DPLCONNECTION);
981
982 Directplay_lobby_launched_game = false;
983 if (lpDirectPlayLobby3A) {
984 lpDirectPlayLobby3A->Release();
985 lpDirectPlayLobby3A = NULL;
986 }
987 CoInitialize(NULL);
988 hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A,
989 (LPVOID *)&lpDirectPlayLobby3A);
990 if (SUCCEEDED(hr)) {
991 hr = lpDirectPlayLobby3A->GetConnectionSettings(0, NULL, &buffersize);
992 if (FAILED(hr)) {
993 if (hr == DPERR_NOTLOBBIED) {
994 mprintf(0, "Application not started from a lobby.\n");
995 lpDirectPlayLobby3A->Release();
996 lpDirectPlayLobby3A = NULL;
997 return false;
998 }
999 mprintf(0, "Unable to determine DirectPlay lobby connection info buffer size!!!\n");
1000 lpDirectPlayLobby3A->Release();
1001 lpDirectPlayLobby3A = NULL;
1002 return false;
1003 }
1004 lpdplconnection = (LPDPLCONNECTION)mem_malloc(buffersize);
1005 hr = lpDirectPlayLobby3A->GetConnectionSettings(0, lpdplconnection, &buffersize);
1006 lpDirectPlayLobby3A->Release();
1007 lpDirectPlayLobby3A = NULL;
1008 if (FAILED(hr)) {
1009 mprintf(0, "IDirectPlayLobby3::GetConnectionSettings() failed.\n");
1010 if (hr == DPERR_NOTLOBBIED) {
1011 mprintf(0, "Application not started from a lobby.\n");
1012 return false;
1013 } else {
1014 mprintf(0, "Unknown error from IDirectPlayLobby3::GetConnectionSettings().\n");
1015 return false;
1016 }
1017 } else {
1018 mprintf(0, "Application was started from a lobby.\n");
1019 Directplay_lobby_launched_game = true;
1020 return true;
1021 }
1022 } else {
1023 mprintf(0, "IDirectPlayLobby3 CoCreateInstance() failed.\n");
1024 }
1025 return false;
1026 }
1027
1028 // Autoconnects to a game or starts one, based on the directplay lobby stuff
1029 bool dp_AutoConnect() {
1030 if (!Directplay_lobby_launched_game)
1031 return false;
1032
1033 if (lpdplconnection->dwFlags == DPLCONNECTION_CREATESESSION) {
1034 // We are hosting a game
1035 Use_DirectPlay = true;
1036 // What level do we use? what dll? what pps, etc....
1037
1038 } else if (lpdplconnection->dwFlags == DPLCONNECTION_JOINSESSION) {
1039 // We are joining a game
1040 Use_DirectPlay = true;
1041 }
1042
1043 return false;
1044 }