00001 /********************************************************************************/ 00002 /* */ 00003 /* Netrinjo-Engine - a library for game-development */ 00004 /* Copyright (C) 2005 by Tobias Nadler */ 00005 /* */ 00006 /* This library is free software; you can redistribute it and/or */ 00007 /* modify it under the terms of the GNU Lesser General Public */ 00008 /* License as published by the Free Software Foundation; either */ 00009 /* version 2.1 of the License, or (at your option) any later version. */ 00010 /* */ 00011 /* This library is distributed in the hope that it will be useful, */ 00012 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 00013 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ 00014 /* Lesser General Public License for more details. */ 00015 /* */ 00016 /* You should have received a copy of the GNU Lesser General Public */ 00017 /* License along with this library; if not, write to the Free Software */ 00018 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00019 /* */ 00020 /********************************************************************************/ 00021 00022 #ifndef NETSTRUCTS_H 00023 #define NETSTRUCTS_H 00024 00025 #include "netpeermanager.h" 00026 00049 // admin-ids: 0x0** 00051 #define TYP_ADMIN_SPEED 0x010 00053 #define TYP_ADMIN_CLOSE 0x020 00055 #define TYP_ADMIN_REJECT 0x031 00056 //#define TYP_ADMIN_REQUEST 0x080 // last 7 Bits indicate, which request is meant 00057 //#define TYP_ADMIN_ANSWER 0x080 // you can distinguish between request and answer 00058 // by finding out whether or not this app is the server-app 00060 #define TYP_ADMIN_MAXPING 0x081 00062 #define MAX_ADMIN_PASSWORD 31 00063 00064 // connection-ids: 0x1** 00066 #define TYP_CONNECT 0x101 00068 #define TYP_DISCONNECT 0x102 00070 #define TYP_CONNECTIONLIST 0x108 00072 #define TYP_PING_REQUEST 0x110 00074 #define TYP_PING_ANSWER 0x111 00075 00076 // chat-ids: 0x2** 00078 #define TYP_CHAT_MESSAGE 0x202 00079 00080 // game-ids: 0x3** 00082 #define TYP_NEXT_TURN 0x301 00083 00084 // command-ids: 0x4** 00086 #define TYP_BASIC_COMMAND 0x400 00087 00091 struct NetBasicData 00092 { 00096 Uint32 type() { return SDLNet_Read32( &nType ); } 00101 void setType( Uint32 t ) { SDLNet_Write32( t, &nType ); } 00102 protected: 00104 Uint32 nType; 00105 }; 00106 00115 struct NetBasicAdminData : NetBasicData 00116 { 00117 NetBasicAdminData() 00118 { 00119 chPassword[0] = '\0'; 00120 } 00122 char chPassword[MAX_ADMIN_PASSWORD+1]; 00123 }; 00124 00129 struct NetAdminSpeed : NetBasicAdminData 00130 { 00131 NetAdminSpeed() 00132 { 00133 setType( TYP_ADMIN_SPEED ); 00134 } 00136 Uint32 get() { return SDLNet_Read32( &nTime ); } 00138 void set( Uint32 t ) { SDLNet_Write32( t, &nTime ); } 00139 protected: 00141 Uint32 nTime; 00142 }; 00143 00147 struct NetAdminReject : NetBasicAdminData 00148 { 00149 NetAdminReject() 00150 { 00151 setType( TYP_ADMIN_REJECT ); 00152 ip.host = 0; 00153 ip.port = 0; 00154 } 00156 IPaddress ip; 00157 }; 00158 00162 struct NetConnectData : NetBasicData 00163 { 00164 NetConnectData() 00165 { 00166 setType( TYP_CONNECT ); 00167 chName[PEER_MAX_NAME] = '\0'; 00168 } 00170 int getPort() { return SDLNet_Read16( &nListenPort ); } 00172 void setPort( int p ) { SDLNet_Write16( p, &nListenPort ); } 00174 char chName[PEER_MAX_NAME+1]; 00176 char chInfo[64]; 00177 protected: 00182 Uint16 nListenPort; 00183 }; 00184 00189 struct NetDisconnectData : NetBasicData 00190 { 00191 NetDisconnectData() 00192 { 00193 setType( TYP_DISCONNECT ); 00194 chReason[PEER_MAX_REASON] = '\0'; 00195 } 00197 char chReason[PEER_MAX_REASON+1]; 00198 }; 00199 00206 struct NetConnectionList 00207 { 00212 NetConnectionList( int max = 253 ) 00213 { 00214 pData = new char[sizeof(NetBasicData) + sizeof(Uint32) + max*sizeof(IPaddress)]; 00215 pIPs = (IPaddress*)(pData + sizeof(NetBasicData) + sizeof(Uint32)); 00216 pType = (NetBasicData*)pData; 00217 pCount = (Uint32*)(pData + sizeof(NetBasicData)); 00218 *pCount = 0; 00219 bAllocated = true; 00220 pType->setType( TYP_CONNECTIONLIST ); 00221 } 00226 NetConnectionList( void *data ) 00227 { 00228 pData = (char*)data; 00229 pIPs = (IPaddress*)(pData + sizeof(NetBasicData) + sizeof(Uint32)); 00230 pType = (NetBasicData*)pData; 00231 pCount = (Uint32*)(pData + sizeof(NetBasicData)); 00232 bAllocated = false; 00233 } 00234 ~NetConnectionList() 00235 { 00236 if( bAllocated && pData ) 00237 delete[] pData; 00238 pData = NULL; 00239 } 00241 void *getData() { return pData; } 00247 IPaddress *get( int i ) 00248 { 00249 if( i >= SDLNet_Read32( pCount ) ) 00250 return NULL; 00251 return &pIPs[i]; 00252 } 00260 void set( IPaddress a, int i ) 00261 { 00262 if( SDLNet_Read32( pCount ) <= i ) 00263 SDLNet_Write32( i+1, pCount ); 00264 pIPs[i] = a; 00265 } 00270 int size() 00271 { 00272 return SDLNet_Read32( pCount )*sizeof(IPaddress) + sizeof(NetBasicData) + sizeof(Uint32); 00273 } 00275 Uint32 count() 00276 { 00277 return SDLNet_Read32( pCount ); 00278 } 00279 private: 00280 NetBasicData *pType; 00281 IPaddress *pIPs; 00282 char *pData; 00283 Uint32 *pCount; 00284 bool bAllocated; 00285 }; 00286 00290 struct NetValue32Data : NetBasicData 00291 { 00295 NetValue32Data( Uint32 typ = 0 ) 00296 { 00297 setType( typ ); 00298 nValue = 0; 00299 } 00301 Uint32 get() { return SDLNet_Read32( &nValue ); } 00303 void set( Uint32 v ) { SDLNet_Write32( v, &nValue ); } 00304 00305 protected: 00307 Uint32 nValue; 00308 }; 00309 00310 #endif