Changeset 6139 in orxonox.OLD for trunk/src/lib/network
- Timestamp:
- Dec 16, 2005, 6:45:32 PM (19 years ago)
- Location:
- trunk/src/lib/network
- Files:
-
- 13 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/Makefile.am
r5996 r6139 11 11 data_stream.cc \ 12 12 network_protocol.cc \ 13 server_socket.cc 13 server_socket.cc \ 14 handshake.cc \ 15 network_game_manager.cc \ 16 converter.cc 17 14 18 15 19 … … 22 26 data_stream.h \ 23 27 network_protocol.h \ 24 server_socket.h 28 server_socket.h \ 29 handshake.h \ 30 network_game_manager.h \ 31 converter.h 25 32 33 -
trunk/src/lib/network/network_manager.cc
r5997 r6139 50 50 this->tmpStream = NULL; 51 51 this->hostID = -1; 52 this->bGameServer = false; 52 53 53 54 PRINTF(0)("NetworkManager created\n"); … … 95 96 } 96 97 97 this->tmpStream = new NetworkStream(ipAddress , NET_CLIENT);98 this->tmpStream = new NetworkStream(ipAddress); 98 99 return 1; 99 100 } … … 106 107 int NetworkManager::createServer(unsigned int port) 107 108 { 108 this->tmpStream = new NetworkStream(port, NET_SERVER); 109 this->tmpStream = new NetworkStream(port); 110 this->bGameServer = true; 109 111 SDL_Delay(20); 110 112 return 1; … … 120 122 { 121 123 /* creating a new network stream, it will register itself automaticaly to the class list */ 122 this->tmpStream = new NetworkStream(address, sync, NET_CLIENT); 124 this->tmpStream = new NetworkStream(address); 125 this->tmpStream->connectSynchronizeable(sync); 123 126 } 124 127 … … 132 135 PRINTF(0)("Create a new server socket\n"); 133 136 /* creating a new network stream, it will register itself automaticaly to the class list */ 134 this->tmpStream = new NetworkStream(port, sync, NET_SERVER); 137 this->tmpStream = new NetworkStream(port); 138 this->tmpStream->connectSynchronizeable(sync); 139 this->bGameServer = true; 135 140 } 136 141 -
trunk/src/lib/network/network_manager.h
r5997 r6139 45 45 void setHostID(int id); 46 46 /** Returns the hostID @return The hostID of the object */ 47 inline int getHostID() { return this->hostID; }; 47 inline int getHostID() { return this->hostID; } 48 inline bool isGameServer() { return this->bGameServer; } 48 49 49 private: 50 50 51 void connectSynchronizeable(Synchronizeable& sync); 51 52 void synchronize(); 52 53 54 private: 53 55 NetworkManager(); 54 56 55 57 56 58 private: 57 const std::list<BaseObject*>* netStreamList; // list with refs to all network streams 58 const std::list<BaseObject*>* syncList; // list of synchronizeables 59 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 60 NetworkStream* tmpStream; //!< FIXME: this is only for testing purposes 61 int hostID; //!< The Host-ID of the Manager 59 const std::list<BaseObject*>* netStreamList; // list with refs to all network streams 60 const std::list<BaseObject*>* syncList; // list of synchronizeables 61 static NetworkManager* singletonRef; //!< Pointer to the only instance of this Class 62 NetworkStream* tmpStream; //!< FIXME: this is only for testing purposes 63 int hostID; //!< The Host-ID of the Manager 64 bool bGameServer; //!< true if it is a server 62 65 63 66 }; -
trunk/src/lib/network/network_protocol.cc
r5822 r6139 57 57 * @arg bufferLength: the length of the internal buffer 58 58 * @arg source: reference to the source Synchronizeable object 59 * @arg remoteID: id number of the remote Synchronizeable object60 59 * @return: the new data length with header (the header data is included into byte* data) 61 60 * -1 if there isn't enough space in the buffer for the header 62 61 */ 63 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source , unsigned int remoteID)62 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source) 64 63 { 65 printf("NetworkProtocol:create header length = %i, bufferLength = %i\n", length, bufferLength);64 PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength); 66 65 //If there isn't enough space for the header return -1 67 66 if (length + this->headerLength > bufferLength) 68 67 return -1; 69 68 70 // for(int i = 0; i < length; i++)71 // printf("send byte[%i]=%u\n", i, data[i]);72 69 73 74 //Create space for the header 70 // FIXME: without move Create space for the header 75 71 for( int i = length - 1; i >= 0; i--) 76 72 data[i + this->headerLength] = data[i]; 77 73 78 74 //Now create the header 79 /* protocol identifier */80 data[0] = 255;81 /* version number */82 data[1] = 0;83 75 /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ 84 data[2] = (byte)source.getClassID(); 85 /* receiver ID */ 86 data[3] = remoteID; 76 data[0] = (byte)(source.getUniqueID()); 87 77 /* data length*/ 88 data[ 4] = length;78 data[1] = length; 89 79 90 80 … … 100 90 Header NetworkProtocol::extractHeader(byte* data, int length) 101 91 { 102 PRINTF( 0)("extract Header\n");92 PRINTF(5)("extract Header\n"); 103 93 //Test if received data can contain a header 104 94 if (length < headerLength) 105 95 { 106 PRINTF( 0)("Received data is to short; it can't contain a header!");96 PRINTF(1)("Received data is to short; it can't contain a header!\n"); 107 97 Header h; 108 h. protocol= 0;98 h.length = 0; 109 99 return h; 110 100 } … … 114 104 //&h = data; 115 105 116 h.protocol = data[0]; 117 /* version number */ 118 h.version = data[1]; 119 /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ 120 h.senderID = data[2]; 121 /* receiver ID */ 122 h.receiverID = data[3]; 106 /* unique ID */ 107 h.synchronizeableID = data[0]; 123 108 /* data length*/ 124 h.length = data[ 4];109 h.length = data[1]; 125 110 126 127 // for(int i = 0; i < length; i++)128 // printf("recS byte[%i]=%u\n", i, data[i]);129 130 //Remove header131 // for (int i = headerLength; i < length; i++)132 // data[i - headerLength] = data[i];133 111 134 112 return h; -
trunk/src/lib/network/network_protocol.h
r5822 r6139 6 6 #define _NETWORK_PROTOCOL_H 7 7 8 /* include base_object.h since all classes are derived from this one */ 8 9 #include "base_object.h" 9 10 … … 11 12 #include "netdefs.h" 12 13 13 #define HEADER_LENGTH 114 15 14 typedef struct Header 16 15 { 17 byte protocol; 18 byte version; 19 byte senderID; 20 byte receiverID; 16 byte synchronizeableID; 21 17 byte length; 22 18 }; … … 33 29 ~NetworkProtocol(); 34 30 35 int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source , unsigned int remoteID);31 int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source); 36 32 Header extractHeader(byte* data, int length); 37 33 -
trunk/src/lib/network/network_socket.cc
r5996 r6139 28 28 #include "debug.h" 29 29 30 31 30 /** 32 31 * Default constructor … … 52 51 this->tcpSocket = sock; 53 52 54 SDL_CreateThread(thread_read, (void*)this);55 SDL_CreateThread(thread_write, (void*)this);53 readThread = SDL_CreateThread(thread_read, (void*)this); 54 writeThread = SDL_CreateThread(thread_write, (void*)this); 56 55 } 57 56 … … 65 64 outgoingBufferLength = 0; 66 65 66 readThread = NULL; 67 writeThread = NULL; 68 69 70 thread_write_running = false; 71 thread_read_running = false; 72 67 73 incomingBufferMutex = SDL_CreateMutex(); 68 74 outgoingBufferMutex = SDL_CreateMutex(); 75 76 69 77 socketMutex = SDL_CreateMutex(); 70 78 terminateThread = false; … … 89 97 * Default destructor 90 98 */ 91 NetworkSocket::~ NetworkSocket( ) 92 { 99 NetworkSocket::~NetworkSocket( ) 100 { 101 this->terminateThread = true; 93 102 /* Quit SDL_net */ 94 103 // NOTE: what if other instances of NetworkSocket running? … … 99 108 SDL_DestroyMutex(outgoingBufferMutex); 100 109 SDL_DestroyMutex(socketMutex); 110 SDL_DestroyMutex(threadTerminationMutex); 101 111 } 102 112 … … 122 132 } 123 133 124 SDL_CreateThread(thread_read, (void*)this);125 SDL_CreateThread(thread_write, (void*)this);134 readThread = SDL_CreateThread(thread_read, (void*)this); 135 writeThread = SDL_CreateThread(thread_write, (void*)this); 126 136 } 127 137 … … 152 162 int NetworkSocket::writeBytes(byte * data, int length) 153 163 { 154 PRINTF( 0)("NetworkSocket::writeBytes()\n");164 PRINTF(5)("NetworkSocket::writeBytes()\n"); 155 165 #ifdef _USE_OUTGOING_BUFFER 156 166 … … 200 210 int NetworkSocket::readBytes(byte * data, int length) 201 211 { 202 PRINTF( 0)("NetworkSocket::readBytes()\n");212 PRINTF(5)("NetworkSocket::readBytes()\n"); 203 213 if (data==NULL) 204 214 return 0; … … 256 266 NetworkSocket * self = (NetworkSocket*)data; 257 267 268 self->thread_read_running = true; 269 258 270 while (!self->terminateThread) 259 271 { … … 263 275 264 276 //if buffer is full 265 if (nbytestoread<=0 )277 if (nbytestoread<=0 || !self->tcpSocket) 266 278 { 267 279 SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER); … … 275 287 if (nbytesread<=0) 276 288 { 277 printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); 289 if (nbytesread<0) 290 printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); 278 291 279 292 SDL_mutexP(self->socketMutex); … … 284 297 SDL_mutexV(self->socketMutex); 285 298 SDL_mutexV(self->incomingBufferMutex); 286 return -1;299 continue; 287 300 } 288 301 … … 295 308 } 296 309 310 SDL_mutexP(self->threadTerminationMutex); 311 self->thread_read_running = false; 312 313 if ( !self->thread_write_running ) 314 { 315 //delete self; 316 SDL_mutexV(self->threadTerminationMutex); 317 } 318 else 319 { 320 SDL_mutexV(self->threadTerminationMutex); 321 } 322 323 324 PRINTF(0)("QUIT READ THREAD\n"); 297 325 return 0; 298 326 } … … 305 333 NetworkSocket * self = (NetworkSocket*)data; 306 334 335 self->thread_write_running = true; 336 307 337 while (!self->terminateThread) 308 338 { … … 314 344 315 345 //if buffer is full 316 if (nbytestowrite<=0 )346 if (nbytestowrite<=0 || !self->tcpSocket) 317 347 { 318 348 SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER); … … 342 372 343 373 SDL_mutexV(self->socketMutex); 344 return -1;374 continue; 345 375 } 346 376 347 377 } 348 378 349 printf("QUIT WRITE THREAD\n"); 379 SDL_mutexP(self->threadTerminationMutex); 380 self->thread_write_running = false; 381 382 if ( !self->thread_read_running ) 383 { 384 //delete self; 385 SDL_mutexV(self->threadTerminationMutex); 386 } 387 else 388 { 389 SDL_mutexV(self->threadTerminationMutex); 390 } 391 392 393 PRINTF(0)("QUIT WRITE THREAD\n"); 350 394 return 0; 351 395 } … … 353 397 bool NetworkSocket::writePacket( byte * data, int length ) 354 398 { 355 PRINTF( 0)("NetworkSocket::writePacket()\n");399 PRINTF(5)("NetworkSocket::writePacket()\n"); 356 400 if (length>255) 357 401 { … … 368 412 int NetworkSocket::readPacket( byte * data, int maxLength ) 369 413 { 370 PRINTF( 0)("NetworkSocket::readPacket()\n");414 PRINTF(5)("NetworkSocket::readPacket()\n"); 371 415 if (incomingBufferLength<1) 372 416 { -
trunk/src/lib/network/network_socket.h
r5996 r6139 63 63 bool terminateThread; 64 64 65 SDL_mutex* threadTerminationMutex; 65 66 static int thread_read(void * data); 67 bool thread_read_running; 68 bool thread_write_running; 69 70 SDL_Thread* readThread; 71 SDL_Thread* writeThread; 72 66 73 #ifdef _USE_OUTGOING_BUFFER 67 74 static int thread_write(void * data); … … 74 81 void init(); 75 82 83 //dont make this public use destroy() instead 84 ~NetworkSocket(); 85 76 86 public: 77 87 … … 79 89 NetworkSocket(IPaddress ip); 80 90 NetworkSocket(TCPsocket sock); 81 ~NetworkSocket(); 91 void destroy() { terminateThread = true; }; 92 82 93 83 94 void connectToServer(IPaddress ip); … … 87 98 int readPacket(byte * data, int maxLength); 88 99 100 inline bool isOk() { return tcpSocket!=NULL; } 101 89 102 }; 90 103 -
trunk/src/lib/network/network_stream.cc
r5996 r6139 26 26 #include "connection_monitor.h" 27 27 #include "synchronizeable.h" 28 #include "network_manager.h" 28 29 #include "list.h" 29 30 #include "debug.h" 31 #include "class_list.h" 30 32 31 33 /* include your own header */ … … 45 47 /* initialize the references */ 46 48 this->type = NET_CLIENT; 47 this->networkSocket = new NetworkSocket();48 49 this->networkProtocol = new NetworkProtocol(); 49 this->synchronizeables = NULL;50 50 this->connectionMonitor = new ConnectionMonitor(); 51 51 } 52 52 53 NetworkStream::NetworkStream(IPaddress& address , NodeType type)54 { 55 this->type = type;53 NetworkStream::NetworkStream(IPaddress& address) 54 { 55 this->type = NET_CLIENT; 56 56 this->init(); 57 this->networkSocket = new NetworkSocket(address);57 this->networkSockets.push_back(new NetworkSocket(address)); 58 58 this->networkProtocol = new NetworkProtocol(); 59 this->synchronizeables = NULL;60 59 this->connectionMonitor = new ConnectionMonitor(); 61 } 62 63 64 NetworkStream::NetworkStream(unsigned int port, NodeType type) 65 { 66 this->type = type; 60 61 Handshake* hs = new Handshake(false); 62 hs->setUniqueID( 0 ); 63 this->handshakes.push_back(hs); 64 this->connectSynchronizeable(*hs); 65 PRINTF(0)("NetworkStream: %s\n", hs->getName()); 66 } 67 68 69 NetworkStream::NetworkStream(unsigned int port) 70 { 71 this->type = NET_SERVER; 67 72 this->init(); 68 this->networkSocket = new NetworkSocket(); 69 // this->networkSocket->listen(port); 73 this->serverSocket = new ServerSocket(port); 70 74 this->networkProtocol = new NetworkProtocol(); 71 this->synchronizeables = NULL;72 75 this->connectionMonitor = new ConnectionMonitor(); 73 } 74 75 76 NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) 77 : DataStream() 78 { 79 this->type = type; 80 this->init(); 81 this->networkSocket = new NetworkSocket(address); 82 this->networkProtocol = new NetworkProtocol(); 83 this->synchronizeables = &sync; 84 this->connectionMonitor = new ConnectionMonitor(); 76 this->networkSockets.push_back( NULL ); 77 this->handshakes.push_back( NULL ); 85 78 this->bActive = true; 86 } 87 88 89 NetworkStream::NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type) 90 : DataStream() 91 { 92 this->type = type; 93 this->init(); 94 this->networkSocket = new NetworkSocket(); 95 // this->networkSocket->listen(port); 96 this->networkProtocol = new NetworkProtocol(); 97 this->synchronizeables = &sync; 98 this->connectionMonitor = new ConnectionMonitor(); 99 this->bActive = true; 79 80 this->setMaxConnections( 10 ); 100 81 } 101 82 … … 105 86 /* set the class id for the base object */ 106 87 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 107 this->state = NET_REC_HEADER;108 88 this->bActive = false; 89 this->serverSocket = NULL; 90 myHostId = 0; 109 91 } 110 92 … … 112 94 NetworkStream::~NetworkStream() 113 95 { 114 115 networkSocket->disconnectServer(); 116 117 if( this->networkSocket) 118 delete networkSocket; 96 if ( this->serverSocket ) 97 { 98 serverSocket->close(); 99 delete serverSocket; 100 } 101 102 for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) 103 { 104 if ( *i ) 105 { 106 (*i)->disconnectServer(); 107 (*i)->destroy(); 108 } 109 } 110 111 for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) 112 { 113 if ( *i ) 114 { 115 delete (*i); 116 } 117 } 119 118 120 119 delete connectionMonitor; … … 125 124 void NetworkStream::connectSynchronizeable(Synchronizeable& sync) 126 125 { 127 this->synchronizeables = &sync; 128 if( this->networkSocket != NULL) 126 this->synchronizeables.push_back(&sync); 127 sync.setNetworkStream( this ); 128 129 if( this->networkSockets.size()>0 ) 129 130 this->bActive = true; 130 131 } 131 132 133 void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync) 134 { 135 this->synchronizeables.remove(&sync); 136 137 if( this->networkSockets.size()<=0 ) 138 this->bActive = false; 139 } 140 132 141 133 142 void NetworkStream::processData() 134 143 { 135 int dataLength = 0; 144 if ( this->type == NET_SERVER ) 145 this->updateConnectionList(); 146 else 147 { 148 if ( networkSockets[0] && !networkSockets[0]->isOk() ) 149 { 150 PRINTF(1)("lost connection to server\n"); 151 152 //delete networkSockets[i]; 153 networkSockets[0]->disconnectServer(); 154 networkSockets[0]->destroy(); 155 networkSockets[0] = NULL; 156 //TODO: delete handshake from synchronizeable list so i can delete it 157 if ( handshakes[0] ) 158 delete handshakes[0]; 159 handshakes[0] = NULL; 160 } 161 } 162 163 for (int i = 0; i<handshakes.size(); i++) 164 { 165 if ( handshakes[i] ) 166 { 167 if ( handshakes[i]->completed() ) 168 { 169 if ( handshakes[i]->ok() ) 170 { 171 if ( type != NET_SERVER ) 172 { 173 NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); 174 myHostId = NetworkManager::getInstance()->getHostID(); 175 } 176 PRINT(0)("handshake finished\n"); 177 delete handshakes[i]; 178 handshakes[i] = NULL; 179 //TODO: replace handshake by entitymanager 180 } 181 else 182 { 183 PRINT(1)("handshake failed!\n"); 184 networkSockets[i]->disconnectServer(); 185 delete handshakes[i]; 186 handshakes[i] = NULL; 187 //TODO: handle error 188 } 189 } 190 } 191 } 192 136 193 137 194 /* DOWNSTREAM */ 138 printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); 139 PRINT(0)("============= DOWNSTREAM:===============\n"); 140 /* first of all read the synchronizeable's data: */ 141 if( this->isServer()) 142 dataLength = this->synchronizeables->readBytes((byte*)downBuffer); 143 144 if( dataLength > 0) 145 { 146 /* send the received data to connectionMonitor */ 147 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 148 149 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, 150 *(this->synchronizeables), 12/* some random number (no real id)*/); 151 152 /* pass the data to the network socket */ 153 // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 154 /* check if there was an error */ 155 if( dataLength == -1) 156 { 157 PRINTF(0)("Error in writing data to the NetworkSocket\n"); 158 } 159 } 160 195 196 int dataLength; 197 int reciever; 198 Header header; 199 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 200 { 201 //TODO: remove items from synchronizeables if they dont exist 202 if ( (*it)!=NULL && (*it)->getOwner() == myHostId ) 203 { 204 do { 205 reciever = 0; 206 dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); 207 208 209 if ( dataLength<=0 ){ 210 reciever = 0; 211 continue; 212 } 213 214 dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); 215 216 //FIXME: this is a hack, find a better way 217 Header* header = (Header*)downBuffer; 218 if ( header->synchronizeableID<100 ) 219 header->synchronizeableID = 0; 220 221 if ( dataLength<=0 ) 222 continue; 223 224 if ( reciever!=0 ) 225 { 226 if ( networkSockets[reciever] != NULL ) 227 { 228 PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever); 229 networkSockets[reciever]->writePacket(downBuffer, dataLength); 230 } 231 else 232 { 233 PRINTF(1)("networkSockets[reciever] == NULL\n"); 234 } 235 } 236 else 237 { 238 for ( int i = 0; i<networkSockets.size(); i++) 239 { 240 if ( networkSockets[i] != NULL ) 241 { 242 PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever); 243 networkSockets[i]->writePacket(downBuffer, dataLength); 244 } 245 } 246 } 247 248 } while( reciever!=0 ); 249 } 250 else 251 { 252 PRINTF(0)("synchronizeables == NULL"); 253 } 254 } 161 255 162 256 /* UPSTREAM */ 163 dataLength = 0; 164 PRINT(0)("============== UPSTREAM:================\n"); 165 /* first of all read the next Orxonox Network Header */ 166 167 if( this->state == NET_REC_HEADER) 168 { 169 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); 170 if( dataLength == sizeof(Header)) 171 { 172 this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); 173 printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", 174 this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, 175 this->packetHeader.receiverID, this->packetHeader.length); 176 /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ 177 178 this->state = NET_REC_DATA; 179 } 180 } 181 if( this->state == NET_REC_DATA) 182 { 183 /* now read the data */ 184 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); 185 /* check if the data is available and process it if so */ 186 if( dataLength == this->packetHeader.length) 187 { 188 printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); 189 /* send the received data to connectionMonitor */ 190 this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); 191 /* now pass the data to the sync object */ 192 if( !this->isServer()) 193 this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); 194 195 this->state = NET_REC_HEADER; 196 } 197 } 198 199 200 } 257 258 for ( int i = 0; i<networkSockets.size(); i++) 259 { 260 if ( networkSockets[i] ) 261 { 262 do { 263 dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); 264 265 if ( dataLength<=0 ) 266 continue; 267 268 PRINTF(5)("read %d bytes from socket\n", dataLength); 269 header = networkProtocol->extractHeader(upBuffer, dataLength); 270 dataLength -= sizeof(header); 271 272 if ( dataLength != header.length ) 273 { 274 PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); 275 continue; 276 } 277 278 if ( header.synchronizeableID == 0 ) 279 { 280 header.synchronizeableID = i; 281 } 282 283 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 284 { 285 if ( *it && (*it)->getUniqueID()==header.synchronizeableID ) 286 (*it)->writeBytes(upBuffer+sizeof(header), dataLength); 287 } 288 289 } while ( dataLength>0 ); 290 } 291 } 292 } 293 294 void NetworkStream::updateConnectionList( ) 295 { 296 //check for new connections 297 298 NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); 299 300 if ( tempNetworkSocket ) 301 { 302 int clientId; 303 if ( freeSocketSlots.size() >0 ) 304 { 305 clientId = freeSocketSlots.back(); 306 freeSocketSlots.pop_back(); 307 networkSockets[clientId] = tempNetworkSocket; 308 handshakes[clientId] = new Handshake(true, clientId); 309 handshakes[clientId]->setUniqueID(clientId); 310 } else 311 { 312 clientId = networkSockets.size(); 313 networkSockets.push_back(tempNetworkSocket); 314 Handshake* tHs = new Handshake(true, clientId); 315 tHs->setUniqueID(clientId); 316 handshakes.push_back(tHs); 317 } 318 319 if ( clientId > this->maxConnections ) 320 { 321 handshakes[clientId]->doReject(); 322 PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId); 323 } 324 else 325 326 PRINTF(0)("New Client: %d\n", clientId); 327 328 this->connectSynchronizeable(*handshakes[clientId]); 329 } 330 331 332 //check if connections are ok else remove them 333 for ( int i = 1; i<networkSockets.size(); i++) 334 { 335 if ( networkSockets[i] && !networkSockets[i]->isOk() ) 336 { 337 //TODO: tell EntityManager that this player left the game 338 PRINTF(0)("Client is gone: %d\n", i); 339 340 //delete networkSockets[i]; 341 networkSockets[i]->disconnectServer(); 342 networkSockets[i]->destroy(); 343 networkSockets[i] = NULL; 344 //TODO: delete handshake from synchronizeable list so i can delete it 345 if ( handshakes[i] ) 346 delete handshakes[i]; 347 handshakes[i] = NULL; 348 349 if ( i == networkSockets.size()-1 ) 350 { 351 networkSockets.pop_back(); 352 handshakes.pop_back(); 353 } 354 else 355 { 356 freeSocketSlots.push_back(i); 357 } 358 } 359 } 360 361 362 } 363 364 void NetworkStream::setMaxConnections( int n ) 365 { 366 if ( !this->isServer() ) 367 { 368 PRINTF(1)("Cannot set maxConnections because I am no server.\n"); 369 } 370 if ( this->networkSockets.size() > 1 ) 371 { 372 PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size()); 373 return; 374 } 375 this->maxConnections = n; 376 } 377 378 -
trunk/src/lib/network/network_stream.h
r5996 r6139 7 7 #define _NETWORK_STREAM 8 8 9 #include <vector> 10 #include <list> 11 9 12 #include "data_stream.h" 10 13 #include "network_protocol.h" 14 #include "server_socket.h" 15 #include "handshake.h" 11 16 12 17 class Synchronizeable; 13 18 class NetworkSocket; 19 class ServerSocket; 14 20 class ConnectionMonitor; 15 21 class NetworkProtocol; 16 22 17 18 //<! The state of the NetworkStream 19 typedef enum NetStat { 20 NET_REC_HEADER = 0, //!< Waiting for header 21 NET_REC_DATA, //!< Waiting for data 22 23 NUM_STATES //!< Number of states 24 }; 23 typedef std::list<Synchronizeable*> SynchronizeableList; 24 typedef std::vector<NetworkSocket*> NetworkSocketVector; 25 typedef std::vector<Handshake*> HandshakeVector; 25 26 26 27 … … 30 31 public: 31 32 NetworkStream(); 32 NetworkStream(IPaddress& address , NodeType type);33 NetworkStream(unsigned int port , NodeType type);33 NetworkStream(IPaddress& address); 34 NetworkStream(unsigned int port); 34 35 35 NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type);36 NetworkStream(unsigned int port, Synchronizeable& sync, NodeType type);37 36 ~NetworkStream(); 38 37 void init(); 39 38 40 39 void connectSynchronizeable(Synchronizeable& sync); 40 void disconnectSynchronizeable(Synchronizeable& sync); 41 41 42 42 inline bool isServer() const { return (this->type == NET_SERVER)? true:false; } 43 43 inline bool isActive() const { return this->bActive; } 44 45 inline int getMaxConnections(){ return maxConnections; } 46 void setMaxConnections( int n ); 44 47 45 48 virtual void processData(); … … 48 51 NetworkProtocol* networkProtocol; 49 52 ConnectionMonitor* connectionMonitor; 50 Synchronizeable* synchronizeables; 51 NetworkSocket* networkSocket; 53 SynchronizeableList synchronizeables; 54 NetworkSocketVector networkSockets; 55 HandshakeVector handshakes; 56 ServerSocket* serverSocket; 52 57 int type; 53 int state;54 58 Header packetHeader; 55 59 bool bActive; 60 std::list<int> freeSocketSlots; 61 62 int myHostId; 63 int maxConnections; 64 65 void updateConnectionList(); 56 66 }; 57 67 #endif /* _NETWORK_STREAM */ -
trunk/src/lib/network/server_socket.cc
r5996 r6139 110 110 111 111 112 NetworkSocket ServerSocket::getNewSocket( )112 NetworkSocket* ServerSocket::getNewSocket( ) 113 113 { 114 if ( !listenSocket ) 115 { 116 PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n"); 117 close(); 118 return NULL; 119 } 120 114 121 TCPsocket sock = SDLNet_TCP_Accept(listenSocket); 115 122 … … 120 127 else 121 128 { 122 return NetworkSocket(sock);129 return new NetworkSocket(sock); 123 130 } 124 131 } 125 132 133 void ServerSocket::close( ) 134 { 135 if ( listenSocket ) 136 { 137 SDLNet_TCP_Close( listenSocket ); 138 listenSocket = NULL; 139 } 140 141 _isListening = false; 142 } 143 -
trunk/src/lib/network/server_socket.h
r5996 r6139 39 39 ~ServerSocket(); 40 40 bool listen( unsigned int port ); 41 NetworkSocket getNewSocket( void ); 41 NetworkSocket* getNewSocket( void ); 42 void close(); 43 inline bool isOk(){ return listenSocket!=NULL; } 42 44 }; 43 45 -
trunk/src/lib/network/synchronizeable.cc
r5997 r6139 15 15 */ 16 16 17 #define DEBUG_MODULE_NETWORK 18 17 19 #include "synchronizeable.h" 18 20 #include "netdefs.h" 21 #include "network_manager.h" 22 #include "network_stream.h" 19 23 20 24 … … 25 29 { 26 30 27 //owner = ?; 28 //hostID = ?; 31 owner = 0; 32 hostID = NetworkManager::getInstance()->getHostID(); 33 uniqueID = -1; 29 34 //state = ?; 30 35 … … 44 49 */ 45 50 Synchronizeable::~Synchronizeable() 46 {} 51 { 52 if ( this->networkStream ) 53 this->networkStream->disconnectSynchronizeable(*this); 54 } 47 55 48 56 /** … … 50 58 */ 51 59 void Synchronizeable::writeBytes(const byte* data, int length) 52 {} 60 { 61 PRINTF(1)("Synchronizeable::writeBytes was called\n"); 62 } 53 63 54 64 /** 55 65 * read data from NetworkStream 56 66 */ 57 int Synchronizeable::readBytes(byte* data) 58 {} 67 int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever) 68 { 69 PRINTF(1)("Synchronizeable::readBytes was called\n"); 70 } 59 71 60 72 … … 65 77 void Synchronizeable::readDebug() const 66 78 {} 67 68 69 70 79 71 80 … … 111 120 return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC; 112 121 } 122 123 124 -
trunk/src/lib/network/synchronizeable.h
r5997 r6139 19 19 #define STATE_OUTOFSYNC 2 20 20 21 class NetworkStream; 22 21 23 22 24 class Synchronizeable : virtual public BaseObject … … 29 31 30 32 virtual void writeBytes(const byte* data, int length); 31 virtual int readBytes(byte* data );33 virtual int readBytes(byte* data, int maxLength, int * reciever); 32 34 virtual void writeDebug() const; 33 35 virtual void readDebug() const; 34 35 36 37 38 39 void setIsServer(bool isServer); 40 void setIsOutOfSync(bool outOfSync); 36 37 void setIsServer( bool isServer ); 38 void setIsOutOfSync( bool outOfSync ); 41 39 bool isServer(); 42 40 bool isOutOfSync(); 41 void setUniqueID( int id ){ uniqueID = id; } 42 int getUniqueID() const { return uniqueID; }; 43 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 44 45 inline int getOwner(){ return owner; } 46 inline void setOwner(int owner){ this->owner = owner; } 47 48 inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } 43 49 44 50 private: 45 51 46 52 int uniqueID; 47 48 49 53 54 55 50 56 //static std::vector<Synchronizeable*> classList; 51 57 int owner; … … 54 60 std::list<int> synchronizeRequests; 55 61 62 NetworkStream* networkStream; 63 56 64 }; 57 65 #endif /* _SYNCHRONIZEABLE_H */
Note: See TracChangeset
for help on using the changeset viewer.