Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

NetMgr.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  NetMgr.c                                                                            */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description:                                                                        */
00006 /*                                                                                      */
00007 /*  Copyright (c) 1997, 1999, Eclipse Entertainment; All rights reserved.               */
00008 /*                                                                                      */
00009 /*  See the accompanying file LICENSE.TXT for terms on the use of this library.         */
00010 /*  This library is distributed in the hope that it will be useful but WITHOUT          */
00011 /*  ANY WARRANTY OF ANY KIND and without any implied warranty of MERCHANTABILITY        */
00012 /*  or FITNESS FOR ANY PURPOSE.  Refer to LICENSE.TXT for more details.                 */
00013 /*                                                                                      */
00014 /****************************************************************************************/
00015 #include <Windows.h>
00016 #include <Assert.h>
00017 #include <stdio.h>
00018 
00019 #include "Genesis.h"
00020 #include "Errorlog.h"
00021 #include "Ram.h"
00022 
00023 #include "NetMgr.h"
00024 #include "Buffer.h"
00025 
00026 //=====================================================================================
00027 //=====================================================================================
00028 typedef struct NetMgr
00029 {
00030         NetMgr                          *SelfCheck1;
00031 
00032         geCSNetMgr                      *CSNetMgr;
00033 
00034         // Local loopback info
00035         geBoolean                       UseLocalBuffers;
00036 
00037         uint8                           ClientBuffer[NETMGR_LOCAL_MSG_BUFFER_SIZE];
00038         uint8                           ServerBuffer[NETMGR_LOCAL_MSG_BUFFER_SIZE];
00039 
00040         Buffer_Data                     ClientToServerBuffer;
00041         Buffer_Data                     ServerToClientBuffer;
00042 
00043         NetMgr                          *SelfCheck2;
00044 } NetMgr;
00045 
00046 //=====================================================================================
00047 //      NetMgr_Create
00048 //=====================================================================================
00049 NetMgr *NetMgr_Create(geBoolean UseLocalBuffers)
00050 {
00051         NetMgr  *NMgr;
00052 
00053         NMgr = GE_RAM_ALLOCATE_STRUCT(NetMgr);
00054 
00055         if (!NMgr)
00056         {
00057                 geErrorLog_AddString(-1, "NetMgr_Create:  Could not create NetMgr object.", NULL);
00058                 return NULL;
00059         }
00060 
00061         memset(NMgr, 0 , sizeof(NetMgr));
00062 
00063         // Setup local message buffers...
00064         Buffer_Set(&NMgr->ClientToServerBuffer, NMgr->ClientBuffer, NETMGR_LOCAL_MSG_BUFFER_SIZE);
00065         Buffer_Set(&NMgr->ServerToClientBuffer, NMgr->ServerBuffer, NETMGR_LOCAL_MSG_BUFFER_SIZE);
00066 
00067         NMgr->UseLocalBuffers = UseLocalBuffers;
00068 
00069         if (!UseLocalBuffers)
00070         {
00071                 NMgr->CSNetMgr = geCSNetMgr_Create();
00072 
00073                 if (!NMgr->CSNetMgr)
00074                 {
00075                         geErrorLog_AddString(-1,"NetMgr_Create Could not create geCSNeMgr...\n", NULL);
00076                         NetMgr_FreeAllObjects(NMgr);
00077                         return NULL;
00078                 }
00079         }
00080 
00081         NMgr->SelfCheck1 = NMgr;
00082         NMgr->SelfCheck2 = NMgr;
00083 
00084         return NMgr;
00085 }
00086 
00087 //=====================================================================================
00088 //      NetMgr_Destroy
00089 //=====================================================================================
00090 void NetMgr_Destroy(NetMgr *NMgr)
00091 {
00092         assert(NetMgr_IsValid(NMgr));
00093 
00094         NetMgr_FreeAllObjects(NMgr);
00095 }
00096 
00097 //=====================================================================================
00098 //      NetMgr_FreeAllObjects
00099 //=====================================================================================
00100 void NetMgr_FreeAllObjects(NetMgr *NMgr)
00101 {
00102         assert(NMgr);
00103 
00104         if (NMgr->CSNetMgr)
00105         {
00106                 assert(NMgr->UseLocalBuffers == GE_FALSE);
00107                 geCSNetMgr_StopSession(NMgr->CSNetMgr);
00108                 geCSNetMgr_Destroy(&NMgr->CSNetMgr);
00109         }
00110                 
00111         NMgr->CSNetMgr = NULL;
00112 
00113         geRam_Free(NMgr);
00114 }
00115 
00116 //=====================================================================================
00117 //      NetMgr_IsValid
00118 //=====================================================================================
00119 geBoolean NetMgr_IsValid(NetMgr *NMgr)
00120 {
00121         if (!NMgr)
00122         {
00123                 geErrorLog_AddString(-1, "NetMgr_IsValid:  NULL NetMgr object.", NULL);
00124                 return GE_FALSE;
00125         }
00126 
00127         if (NMgr->SelfCheck1 != NMgr)
00128         {
00129                 geErrorLog_AddString(-1, "NetMgr_IsValid:  NMgr->SeflfCheck1 != NMgr", NULL);
00130                 return GE_FALSE;
00131         }
00132 
00133         if (NMgr->SelfCheck2 != NMgr)
00134         {
00135                 geErrorLog_AddString(-1, "NetMgr_IsValid:  NMgr->SelfCheck2 != NMgr", NULL);
00136                 return GE_FALSE;
00137         }
00138 
00139         return GE_TRUE;
00140 }
00141 
00142 //===========================================================================
00143 //      NetMgr_StartSession
00144 //===========================================================================
00145 geBoolean NetMgr_StartSession(NetMgr *NMgr, const char *SessionName, const char *PlayerName)
00146 {
00147         assert(NetMgr_IsValid(NMgr));
00148 
00149         assert(NMgr->UseLocalBuffers == GE_FALSE);
00150 
00151         if (!geCSNetMgr_StartSession(NMgr->CSNetMgr, SessionName, PlayerName))
00152                 return GE_FALSE;
00153 
00154         return GE_TRUE;
00155 }
00156 
00157 //===========================================================================
00158 //      NetMgr_JoinSession
00159 //===========================================================================
00160 geBoolean NetMgr_JoinSession(NetMgr *NMgr, const char *IPAddress, const char *PlayerName)
00161 {
00162         geCSNetMgr_NetSession   *SessionList;
00163         int32                                   NumSessions;
00164 
00165         assert(NetMgr_IsValid(NMgr));
00166         assert(NMgr->UseLocalBuffers == GE_FALSE);
00167 
00168         if (!geCSNetMgr_FindSession(NMgr->CSNetMgr, IPAddress, &SessionList, &NumSessions))
00169         {
00170                 geErrorLog_AddString(-1, "NetMgr_JoinSession:  geCSNetMgr_JoinSession failed...", NULL);
00171                 return GE_FALSE;
00172         }
00173 
00174         if (!NumSessions)
00175         {
00176                 geErrorLog_AddString(-1, "NetMgr_JoinSession:  Could not find a session at address:", IPAddress);
00177                 return GE_FALSE;
00178         }
00179 
00180         if (!geCSNetMgr_JoinSession(NMgr->CSNetMgr, PlayerName, &SessionList[0]))
00181         {
00182                 geErrorLog_AddString(-1, "NetMgr_JoinSession:  Could not join a session at address:", IPAddress);
00183                 return GE_FALSE;
00184         }
00185 
00186         return GE_TRUE;
00187 }
00188 
00189 //===========================================================================
00190 //      NetMgr_GetOurID
00191 //===========================================================================
00192 geCSNetMgr_NetID NetMgr_GetOurID(NetMgr *NMgr)
00193 {
00194         assert(NetMgr_IsValid(NMgr));
00195 
00196         return geCSNetMgr_GetOurID(NMgr->CSNetMgr);
00197 }
00198 
00199 //===========================================================================
00200 //      NetMgr_SendServerMessage
00201 //      Send a message to the server
00202 //===========================================================================
00203 geBoolean NetMgr_SendServerMessage(NetMgr *NMgr, Buffer_Data *Buffer, geBoolean G)
00204 {
00205         assert(NetMgr_IsValid(NMgr));
00206 
00207         if (NMgr->UseLocalBuffers)
00208         {
00209                 if (!Buffer_FillBuffer(&NMgr->ClientToServerBuffer, Buffer))
00210                         return GE_FALSE;
00211         }
00212         else
00213         {
00214                 if (!geCSNetMgr_SendToServer(NMgr->CSNetMgr, G, Buffer->Data, Buffer->Pos))
00215                         return GE_FALSE;
00216         }
00217 
00218         return GE_TRUE;
00219 }
00220 
00221 //===========================================================================
00222 //      NetMgr_SendClientMessage
00223 //      Send a message to the client
00224 //===========================================================================
00225 geBoolean NetMgr_SendClientMessage(NetMgr *NMgr, geCSNetMgr_NetID NetID, Buffer_Data *Buffer, geBoolean G)
00226 {
00227         assert(NetMgr_IsValid(NMgr));
00228     
00229         if (NetID == NETMGR_SPECIAL_BOT_NETID)
00230                 return GE_TRUE;
00231 
00232         if (NMgr->UseLocalBuffers)
00233         {
00234                 if (!Buffer_FillBuffer(&NMgr->ServerToClientBuffer, Buffer))
00235                         return GE_FALSE;
00236         }
00237         else
00238         {
00239                 if (!geCSNetMgr_SendToClient(NMgr->CSNetMgr, NetID, G, Buffer->Data, Buffer->Pos))
00240                         return GE_FALSE;
00241         }
00242 
00243         return GE_TRUE;
00244 }
00245 
00246 //=====================================================================================
00247 //      NetMgr_ReceiveServerMessage
00248 //=====================================================================================
00249 geBoolean NetMgr_ReceiveServerMessage(NetMgr *NMgr, geCSNetMgr_NetMsgType *Type, Buffer_Data *Buffer)
00250 {
00251         assert(NetMgr_IsValid(NMgr));
00252 
00253         if (NMgr->UseLocalBuffers)
00254         {
00255                 *Type = NET_MSG_USER;
00256 
00257                 // Use the short-curcuit  buffers for local messages 
00258                 Buffer->Size = NMgr->ServerToClientBuffer.Pos;
00259                 Buffer->Data = NMgr->ServerToClientBuffer.Data;
00260                 
00261                 NMgr->ServerToClientBuffer.Pos = 0;
00262 
00263                 if (!Buffer->Size)
00264                         *Type = NET_MSG_NONE;
00265         }
00266         else
00267         {
00268                 // Use real message system if in real network mode
00269                 if (!geCSNetMgr_ReceiveFromServer(NMgr->CSNetMgr, Type, &Buffer->Size, &Buffer->Data))
00270                         return GE_FALSE;
00271         }
00272 
00273         return GE_TRUE;
00274 }
00275 
00276 //=====================================================================================
00277 //      NetMgr_ReceiveClientMessage
00278 //=====================================================================================
00279 geBoolean NetMgr_ReceiveClientMessage(NetMgr *NMgr, geCSNetMgr_NetMsgType *MsgType, geCSNetMgr_NetID *ClientID, Buffer_Data *Buffer)
00280 {
00281         if (NMgr->UseLocalBuffers)
00282         {
00283                 *ClientID = NETMGR_SINGLE_PLAYER_NETID;
00284                 *MsgType = NET_MSG_USER;
00285                         
00286                 Buffer->Size = NMgr->ClientToServerBuffer.Pos;
00287                 Buffer->Data = NMgr->ClientToServerBuffer.Data;
00288                 NMgr->ClientToServerBuffer.Pos = 0;
00289 
00290                 if (!Buffer->Size)
00291                         *MsgType = NET_MSG_NONE;
00292         }
00293         else
00294         {
00295                 geCSNetMgr_NetID        ToID;
00296 
00297                 //if (!geCSNetMgr_ReceiveAllMessages(NMgr->CSNetMgr, ClientID, &ToID, MsgType, &Buffer->Size, &Buffer->Data))
00298                 //      return GE_FALSE;
00299                 if (!geCSNetMgr_ReceiveFromClient(NMgr->CSNetMgr, MsgType, ClientID, &Buffer->Size, &Buffer->Data))
00300                         return GE_FALSE;
00301         }
00302 
00303         return GE_TRUE;         // Got a message for'em
00304 }
00305 
00306 //=====================================================================================
00307 //      NetMgr_ResetClientBuffer
00308 //=====================================================================================
00309 void NetMgr_ResetClientBuffer(NetMgr *NMgr)
00310 {
00311         NMgr->ClientToServerBuffer.Pos = 0;
00312 }
00313 
00314 //=====================================================================================
00315 //      NetMgr_ResetServerBuffer
00316 //=====================================================================================
00317 void NetMgr_ResetServerBuffer(NetMgr *NMgr)
00318 {
00319         NMgr->ServerToClientBuffer.Pos = 0;
00320 }

Generated on Tue Sep 30 12:36:02 2003 for GTestAndEngine by doxygen 1.3.2