00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <assert.h>
00023
00024
00025 #include <Windows.H>
00026 #include <objbase.h>
00027
00028 #include "CSNetMgr.h"
00029 #include "NetPlay.h"
00030
00031 #include "BaseType.h"
00032 #include "Ram.h"
00033 #include "ErrorLog.h"
00034
00035 #include <InitGuid.h>
00036
00037 #pragma message(" some assertions in here would be nice:")
00038
00039 #define PACKET_HEADER_SIZE 1
00040
00041 #define NET_TIMEOUT 15000 // Givem 15 secs
00042
00043
00044
00045 DEFINE_GUID(GENESIS_GUID,
00046
00047 0x33925241, 0x0, 0x11d0, 0x80, 0x63, 0x0, 0xa0, 0xc9, 0xa, 0xe8, 0x91);
00048
00049
00050 static geBoolean NetSession = GE_FALSE;
00051 static geBoolean WeAreTheServer = GE_FALSE;
00052 static DPID OurPlayerId;
00053 static DPID ServerId;
00054
00055 #define BUFFER_SIZE 20000
00056
00057
00058 static uint8 Packet[BUFFER_SIZE];
00059
00060 static geCSNetMgr_NetClient gClient;
00061
00062 static BOOL geCSNetMgr_ProcessSystemMessage(geCSNetMgr *M,geCSNetMgr_NetID IdTo, LPDPMSG_GENERIC lpMsg, geCSNetMgr_NetMsgType *Type, geCSNetMgr_NetClient *Client);
00063
00064 typedef struct geCSNetMgr
00065 {
00066
00067 geCSNetMgr *Valid;
00068 } geCSNetMgr;
00069
00070
00071 geBoolean geCSNetMgr_IsValid(geCSNetMgr *M)
00072 {
00073 if ( M == NULL )
00074 return GE_FALSE;
00075
00076 if ( M->Valid != M )
00077 return GE_FALSE;
00078
00079 return GE_TRUE;
00080 }
00081
00082 GENESISAPI geCSNetMgr * GENESISCC geCSNetMgr_Create(void)
00083 {
00084 geCSNetMgr *M;
00085
00086 M = GE_RAM_ALLOCATE_STRUCT(geCSNetMgr);
00087
00088 if ( M == NULL)
00089 {
00090 geErrorLog_Add(-1, NULL);
00091 return NULL;
00092 }
00093
00094 M->Valid = M;
00095
00096 return M;
00097 }
00098
00099
00100 GENESISAPI void GENESISCC geCSNetMgr_Destroy(geCSNetMgr **ppM)
00101 {
00102 assert( ppM != NULL );
00103 assert( geCSNetMgr_IsValid(*ppM)!=GE_FALSE );
00104
00105 (*ppM) -> Valid = 0;
00106 geRam_Free(*ppM);
00107 *ppM = NULL;
00108 };
00109
00110
00111
00112
00113 GENESISAPI geBoolean GENESISCC geCSNetMgr_ReceiveFromServer(geCSNetMgr *M, geCSNetMgr_NetMsgType *Type, int32 *Size, uint8 **Data)
00114 {
00115 DPID IdTo;
00116 DWORD BSize = BUFFER_SIZE;
00117 HRESULT Result;
00118
00119 *Size = 0;
00120 *Data = NULL;
00121 *Type = NET_MSG_NONE;
00122
00123 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00124
00125 if (!NetSession)
00126 {
00127 geErrorLog_AddString(-1, "geCSNetMgr_ReceiveFromServer: No net session.\n", NULL);
00128 return GE_FALSE;
00129 }
00130
00131 #if 1
00132
00133 if (!geCSNetMgr_ReceiveSystemMessage(M, OurPlayerId, Type, &gClient))
00134 {
00135 geErrorLog_AddString(-1, "geCSNetMgr_ReceiveFromServer: geCSNetMgr_ReceiveSystemMessage failed.\n", NULL);
00136 return GE_FALSE;
00137 }
00138
00139 if (*Type != NET_MSG_NONE)
00140 {
00141 *Size = sizeof( geCSNetMgr_NetClient );
00142 *Data = (uint8*)&gClient;
00143 return( GE_TRUE );
00144 }
00145 #endif
00146
00147
00148 IdTo = 0;
00149
00150 Result = NetPlayReceive(&ServerId, &IdTo, DPRECEIVE_FROMPLAYER, (uint8*)&Packet, &BSize);
00151
00152 if (Result != DP_OK)
00153 {
00154 if (Result == DPERR_NOMESSAGES)
00155 return GE_TRUE;
00156
00157 geErrorLog_AddString(-1, "geCSNetMgr_ReceiveFromServer: NetPlayReceive failed.\n", NULL);
00158 return GE_FALSE;
00159 }
00160
00161 if (BSize > 0)
00162 {
00163 *Type = Packet[0];
00164 *Size = BSize - PACKET_HEADER_SIZE;
00165 *Data = (uint8*)&Packet[1];
00166 }
00167
00168 return GE_TRUE;
00169 }
00170
00171
00172
00173
00174 GENESISAPI geBoolean GENESISCC geCSNetMgr_ReceiveFromClient(geCSNetMgr *M, geCSNetMgr_NetMsgType *Type, geCSNetMgr_NetID *IdClient, int32 *Size, uint8 **Data)
00175 {
00176 DPID IdTo;
00177 DWORD BSize = BUFFER_SIZE;
00178 HRESULT Result;
00179
00180 *Size = 0;
00181 *Data = NULL;
00182 *Type = NET_MSG_NONE;
00183
00184 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00185
00186 if (!NetSession)
00187 return GE_FALSE;
00188
00189
00190 if (!geCSNetMgr_ReceiveSystemMessage(M, ServerId, Type, &gClient))
00191 return GE_FALSE;
00192
00193 if (*Type != NET_MSG_NONE)
00194 {
00195 *Size = sizeof( geCSNetMgr_NetClient );
00196 *Data = (uint8*)&gClient;
00197 return( GE_TRUE );
00198 }
00199
00200
00201 IdTo = ServerId;
00202
00203 Result = NetPlayReceive((DPID*)IdClient, &IdTo, DPRECEIVE_TOPLAYER, (uint8*)&Packet, &BSize);
00204
00205 if (Result != DP_OK)
00206 {
00207 if (Result == DPERR_NOMESSAGES)
00208 return GE_TRUE;
00209
00210 return GE_FALSE;
00211 }
00212
00213 if (BSize > 0)
00214 {
00215 *Type = Packet[0];
00216 *Size = BSize - PACKET_HEADER_SIZE;
00217 *Data = (uint8*)&Packet[1];
00218 }
00219
00220 return GE_TRUE;
00221 }
00222
00223
00224
00225
00226 GENESISAPI geBoolean GENESISCC geCSNetMgr_ReceiveSystemMessage(geCSNetMgr *M, geCSNetMgr_NetID IdFor, geCSNetMgr_NetMsgType *Type, geCSNetMgr_NetClient *Client)
00227 {
00228 DPID SystemId, IdTo;
00229 DWORD BSize = BUFFER_SIZE;
00230 HRESULT Result;
00231
00232 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00233
00234 *Type = NET_MSG_NONE;
00235
00236 if (!NetSession)
00237 return GE_FALSE;
00238
00239
00240 SystemId = DPID_SYSMSG;
00241 IdTo = IdFor;
00242
00243 Result = NetPlayReceive(&SystemId,&IdTo, DPRECEIVE_FROMPLAYER | DPRECEIVE_TOPLAYER, &Packet, &BSize);
00244
00245 if (Result != DP_OK)
00246 {
00247 if (Result == DPERR_NOMESSAGES)
00248 return GE_TRUE;
00249
00250 return GE_FALSE;
00251 }
00252
00253 if (BSize > 0)
00254 {
00255 if (!geCSNetMgr_ProcessSystemMessage(M, IdFor, (LPDPMSG_GENERIC)&Packet, Type, Client))
00256 return GE_FALSE;
00257 }
00258
00259 return GE_TRUE;
00260 }
00261
00262
00263
00264
00265 static geBoolean geCSNetMgr_ProcessSystemMessage(geCSNetMgr *M, geCSNetMgr_NetID IdTo, LPDPMSG_GENERIC lpMsg, geCSNetMgr_NetMsgType *Type, geCSNetMgr_NetClient *Client)
00266 {
00267 DWORD dwSize;
00268
00269 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00270
00271 *Type = NET_MSG_NONE;
00272
00273 switch( lpMsg->dwType)
00274 {
00275 case DPSYS_CREATEPLAYERORGROUP:
00276 {
00277 if(WeAreTheServer && IdTo == ServerId)
00278 {
00279 LPDPMSG_CREATEPLAYERORGROUP lpAddMsg = (LPDPMSG_CREATEPLAYERORGROUP) lpMsg;
00280
00281
00282 if( lpAddMsg->dpId == ServerId )
00283 return GE_TRUE;
00284
00285
00286 *Type = NET_MSG_CREATE_CLIENT;
00287
00288 strncpy(Client->Name, lpAddMsg->dpnName.lpszShortNameA, MAX_CLIENT_NAME);
00289 Client->Id = lpAddMsg->dpId;
00290
00291
00292
00293 dwSize = sizeof( ServerId ) + PACKET_HEADER_SIZE;
00294
00295 assert(dwSize < BUFFER_SIZE);
00296
00297 Packet[0] = NET_MSG_SERVER_ID;
00298 memcpy( &Packet[1], &ServerId, sizeof( ServerId ) );
00299
00300
00301 if (NetPlaySend( ServerId, Client->Id, DPSEND_GUARANTEED, (void*)&Packet, dwSize ) != DP_OK)
00302 return GE_FALSE;
00303 }
00304
00305 break;
00306 }
00307
00308 case DPSYS_DESTROYPLAYERORGROUP:
00309 {
00310 if (WeAreTheServer && IdTo == ServerId)
00311 {
00312 LPDPMSG_DESTROYPLAYERORGROUP lpDestroyMsg = (LPDPMSG_DESTROYPLAYERORGROUP)lpMsg;
00313
00314 Client->Id = lpDestroyMsg->dpId;
00315 *Type = NET_MSG_DESTROY_CLIENT;
00316 }
00317
00318 break;
00319 }
00320
00321 case DPSYS_HOST:
00322 {
00323 WeAreTheServer = GE_TRUE;
00324 *Type = NET_MSG_HOST;
00325 break;
00326 }
00327
00328 case DPSYS_SESSIONLOST:
00329 {
00330 *Type = NET_MSG_SESSIONLOST;
00331 break;
00332 }
00333
00334 }
00335
00336 return GE_TRUE;
00337 }
00338
00339
00340
00341
00342 GENESISAPI geBoolean GENESISCC geCSNetMgr_ReceiveAllMessages(geCSNetMgr *M, geCSNetMgr_NetID *IdFrom, geCSNetMgr_NetID *IdTo, geCSNetMgr_NetMsgType *Type, int32 *Size, uint8 **Data)
00343 {
00344 DWORD BSize = BUFFER_SIZE;
00345 HRESULT Result;
00346
00347 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00348
00349 *Size = 0;
00350 *Data = NULL;
00351 *Type = NET_MSG_NONE;
00352
00353 if (!NetSession)
00354 return GE_FALSE;
00355
00356 *IdFrom = 0;
00357 *IdTo = 0;
00358
00359
00360 Result = NetPlayReceive((DPID*)IdFrom, (DPID*)IdTo, DPRECEIVE_ALL, &Packet, &BSize);
00361
00362
00363 if (*IdTo != ServerId)
00364 return GE_TRUE;
00365
00366 if (Result != DP_OK)
00367 {
00368 if (Result == DPERR_NOMESSAGES)
00369 return GE_TRUE;
00370
00371 return GE_FALSE;
00372 }
00373
00374 if (BSize > 0)
00375 {
00376 if (*IdFrom == DPID_SYSMSG)
00377 {
00378
00379
00380 if (!geCSNetMgr_ProcessSystemMessage(M, *IdTo, (LPDPMSG_GENERIC)&Packet, Type, &gClient))
00381 return GE_FALSE;
00382
00383 if (*Type != NET_MSG_NONE)
00384 {
00385 *Size = sizeof( geCSNetMgr_NetClient );
00386 *Data = (uint8*)&gClient;
00387 }
00388 }
00389 else
00390 {
00391 *Type = Packet[0];
00392 *Size = BSize - PACKET_HEADER_SIZE;
00393 *Data = (uint8*)&Packet[1];
00394 return GE_TRUE;
00395 }
00396 }
00397
00398 return GE_TRUE;
00399 }
00400
00401
00402
00403
00404
00405 GENESISAPI geCSNetMgr_NetID GENESISCC geCSNetMgr_GetServerID(geCSNetMgr *M)
00406 {
00407 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00408 return(ServerId);
00409 }
00410
00411
00412
00413
00414 GENESISAPI geCSNetMgr_NetID GENESISCC geCSNetMgr_GetOurID(geCSNetMgr *M)
00415 {
00416 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00417 return(OurPlayerId);
00418 }
00419
00420
00421
00422
00423 GENESISAPI geCSNetMgr_NetID GENESISCC geCSNetMgr_GetAllPlayerID(geCSNetMgr *M)
00424 {
00425 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00426 return(DPID_ALLPLAYERS);
00427 }
00428
00429
00430
00431
00432 GENESISAPI geBoolean GENESISCC geCSNetMgr_WeAreTheServer(geCSNetMgr *M)
00433 {
00434 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00435 return (WeAreTheServer);
00436 }
00437
00438
00439
00440
00441 GENESISAPI geBoolean GENESISCC geCSNetMgr_StartSession(geCSNetMgr *M, const char *SessionName, const char *PlayerName)
00442 {
00443 char Name2[MAX_CLIENT_NAME];
00444
00445 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00446 assert(PlayerName);
00447
00448 NetSession = GE_FALSE;
00449 WeAreTheServer = GE_FALSE;
00450
00451 if (!InitNetPlay((LPGUID)&GENESIS_GUID))
00452 return GE_FALSE;
00453
00454 if (!NetPlayCreateSession((char*)SessionName, 16))
00455 return GE_FALSE;
00456
00457 if (!NetPlayCreatePlayer(&ServerId, "Server", NULL, NULL, 0, GE_TRUE))
00458 {
00459 geErrorLog_AddString(-1, "geCSNetMgr_StartSession: NetPlayCreatePlayer failed for server player.\n", NULL);
00460 return GE_FALSE;
00461 }
00462
00463 strncpy(Name2, PlayerName, MAX_CLIENT_NAME);
00464
00465 if (!NetPlayCreatePlayer(&OurPlayerId, Name2, NULL, NULL, 0, GE_FALSE))
00466 {
00467 geErrorLog_AddString(-1, "geCSNetMgr_StartSession: NetPlayCreatePlayer failed for client player.\n", NULL);
00468 return GE_FALSE;
00469 }
00470
00471 WeAreTheServer = GE_TRUE;
00472 NetSession = GE_TRUE;
00473
00474 return GE_TRUE;
00475 }
00476
00477
00478
00479
00480 GENESISAPI geBoolean GENESISCC geCSNetMgr_FindSession(geCSNetMgr *M, const char *IPAdress, geCSNetMgr_NetSession **SessionList, int32 *SessionNum)
00481 {
00482 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00483
00484 NetSession = GE_FALSE;
00485 WeAreTheServer = GE_FALSE;
00486
00487 if (!InitNetPlay((LPGUID)&GENESIS_GUID))
00488 return GE_FALSE;
00489
00490 if(!NetPlayEnumSession((char*)IPAdress, (SESSION_DESC**)SessionList, SessionNum) )
00491 return GE_FALSE;
00492
00493 return( GE_TRUE );
00494 }
00495
00496
00497
00498
00499 GENESISAPI geBoolean GENESISCC geCSNetMgr_JoinSession(geCSNetMgr *M, const char *Name, const geCSNetMgr_NetSession* Session)
00500 {
00501
00502 uint32 StartTime;
00503 DWORD BSize = BUFFER_SIZE;
00504
00505 WeAreTheServer = GE_FALSE;
00506 NetSession = GE_FALSE;
00507
00508 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00509
00510 if (!NetPlayJoinSession( (SESSION_DESC*)Session) )
00511 {
00512 geErrorLog_AddString(-1, "geCSNetMgr_JoinSession: NetPlayJoinSession failed.\n", NULL);
00513 return GE_FALSE;
00514 }
00515
00516 if (!NetPlayCreatePlayer(&OurPlayerId, (char*)Name, NULL, NULL, 0, GE_FALSE))
00517 {
00518 geErrorLog_AddString(-1, "geCSNetMgr_JoinSession: NetPlayCreatePlayer failed.\n", NULL);
00519 return GE_FALSE;
00520 }
00521
00522
00523
00524
00525
00526
00527
00528 StartTime = timeGetTime();
00529 #if 1
00530 while( NET_TIMEOUT > (timeGetTime() - StartTime) )
00531 {
00532 DPID IdFrom, IdTo;
00533 HRESULT Result;
00534
00535 BSize = BUFFER_SIZE;
00536
00537 IdFrom = IdTo = 0;
00538
00539 Result = NetPlayReceive(&IdFrom, &IdTo, DPRECEIVE_ALL, &Packet, &BSize);
00540
00541 if (Result == DP_OK)
00542 {
00543 if (BSize > 0)
00544 {
00545 if( (IdFrom != DPID_SYSMSG) &&(Packet[0] == NET_MSG_SERVER_ID) )
00546 {
00547 memcpy( &ServerId, &Packet[1], sizeof( ServerId ) );
00548 NetSession = GE_TRUE;
00549 return GE_TRUE;
00550 }
00551 }
00552 }
00553 else if (Result != DPERR_NOMESSAGES)
00554 return GE_FALSE;
00555 }
00556 #else
00557 NetSession = GE_TRUE;
00558 ServerId = DPID_SERVERPLAYER;
00559 return( GE_TRUE);
00560 #endif
00561
00562 return( GE_FALSE);
00563 }
00564
00565
00566
00567
00568 GENESISAPI geBoolean GENESISCC geCSNetMgr_StopSession(geCSNetMgr *M)
00569 {
00570 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00571
00572 if (NetSession)
00573 {
00574 NetSession = GE_FALSE;
00575
00576 if (WeAreTheServer)
00577 {
00578 if (!NetPlayDestroyPlayer(ServerId))
00579 return GE_FALSE;
00580 }
00581
00582 if (!NetPlayDestroyPlayer(OurPlayerId))
00583 return GE_FALSE;
00584
00585 if (!DeInitNetPlay())
00586 {
00587
00588 return GE_FALSE;
00589 }
00590
00591 }
00592
00593 WeAreTheServer = GE_FALSE;
00594 return GE_TRUE;
00595 }
00596
00597
00598
00599
00600 GENESISAPI geBoolean GENESISCC geCSNetMgr_SendToServer(geCSNetMgr *M, BOOL Guaranteed, uint8 *Data, int32 DataSize)
00601 {
00602 DWORD dwFlags = 0;
00603
00604 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00605 assert(DataSize+PACKET_HEADER_SIZE < BUFFER_SIZE);
00606
00607 if (!NetSession)
00608 return GE_FALSE;
00609
00610 if( DataSize+PACKET_HEADER_SIZE >= BUFFER_SIZE )
00611 return GE_FALSE;
00612
00613 if (Guaranteed)
00614 dwFlags |= DPSEND_GUARANTEED;
00615
00616 memcpy( &Packet[1], Data, DataSize );
00617 DataSize += PACKET_HEADER_SIZE;
00618 Packet[0] = NET_MSG_USER;
00619
00620 if (NetPlaySend(OurPlayerId, ServerId, dwFlags, (void*)&Packet, DataSize) != DP_OK)
00621 return GE_FALSE;
00622
00623 return GE_TRUE;
00624 }
00625
00626
00627
00628
00629 GENESISAPI geBoolean GENESISCC geCSNetMgr_SendToClient(geCSNetMgr *M, geCSNetMgr_NetID To, BOOL Guaranteed, uint8 *Data, int32 DataSize)
00630 {
00631 DWORD dwFlags = 0;
00632
00633 assert( geCSNetMgr_IsValid(M)!=GE_FALSE );
00634 assert(DataSize+PACKET_HEADER_SIZE < BUFFER_SIZE);
00635
00636 if (!NetSession)
00637 return GE_FALSE;
00638
00639 if( DataSize+PACKET_HEADER_SIZE >= BUFFER_SIZE )
00640 return GE_FALSE;
00641
00642 if (Guaranteed)
00643 dwFlags |= DPSEND_GUARANTEED;
00644
00645 memcpy( &Packet[1], Data, DataSize );
00646 DataSize += PACKET_HEADER_SIZE;
00647 Packet[0] = NET_MSG_USER;
00648
00649 if (NetPlaySend(ServerId, To, dwFlags, (void*)&Packet, DataSize) != DP_OK)
00650 return GE_FALSE;
00651
00652 return GE_TRUE;
00653 }