Changeset 6090 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Dec 13, 2005, 4:29:32 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/handshake.cc
r6062 r6090 33 33 this->setOwner(0); 34 34 35 PRINTF( 0)("Handshake created clientId = %d\n", clientId);35 PRINTF(5)("Handshake created clientId = %d\n", clientId); 36 36 } 37 37 … … 88 88 if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) ) 89 89 { 90 if ( length != 4)90 if ( length != 1 ) 91 91 { 92 PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);92 PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1); 93 93 setState( HS_COMPLETED ); 94 94 return; … … 98 98 setState( HS_RECVD_HID ); 99 99 this->isOk = true; 100 memcpy(&(this->newHostId), data, 4); 101 PRINTF(0)("got my hostID: %d\n", newHostId); 100 this->newHostId = data[0]; 101 102 if ( newHostId == 0 ) 103 { 104 setState( HS_WAS_REJECT ); 105 isOk = false; 106 PRINTF(0)("Server did not accept handshake!\n"); 107 } 108 else 109 { 110 PRINTF(0)("got my hostID: %d\n", newHostId); 111 } 102 112 return; 103 113 } … … 155 165 setState( HS_SENT_HID ); 156 166 setState( HS_COMPLETED ); 157 isOk = true; 158 memcpy(data, (byte*)&clientId, 4); 159 if ( this->isServer() ) 160 *reciever = clientId; 161 return 4; 167 168 if ( hasState( HS_DO_REJECT ) ) 169 { 170 isOk = false; 171 //memcpy(data, (byte*)0, 4); 172 data[0] = 0; 173 } 174 else 175 { 176 isOk = true; 177 //memcpy(data, &clientId, 4); 178 data[0] = (byte)clientId; 179 } 180 *reciever = clientId; 181 return 1; 162 182 } 163 183 -
branches/network/src/lib/network/handshake.h
r6053 r6090 23 23 HS_SENT_HID = 0x00000010, 24 24 HS_RECVD_HID = 0x00000020, 25 HS_COMPLETED = 0X00000040, 25 HS_COMPLETED = 0x00000040, 26 27 HS_DO_REJECT = 0x00010000, 28 HS_WAS_REJECT = 0x00020000, 26 29 27 30 NUM_STATES … … 35 38 inline bool ok(){ return isOk; } 36 39 inline int getHostId(){ return newHostId; } 40 41 inline void doReject(){ setState(HS_DO_REJECT); } 37 42 38 43 virtual void writeBytes(const byte* data, int length); -
branches/network/src/lib/network/network_protocol.cc
r6061 r6090 62 62 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source) 63 63 { 64 PRINTF( 0)("create header length = %i, bufferLength = %i\n", length, bufferLength);64 PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength); 65 65 //If there isn't enough space for the header return -1 66 66 if (length + this->headerLength > bufferLength) … … 90 90 Header NetworkProtocol::extractHeader(byte* data, int length) 91 91 { 92 PRINTF( 0)("extract Header\n");92 PRINTF(5)("extract Header\n"); 93 93 //Test if received data can contain a header 94 94 if (length < headerLength) 95 95 { 96 PRINTF( 0)("Received data is to short; it can't contain a header!\n");96 PRINTF(1)("Received data is to short; it can't contain a header!\n"); 97 97 Header h; 98 98 h.length = 0; -
branches/network/src/lib/network/network_stream.cc
r6062 r6090 60 60 61 61 Handshake* hs = new Handshake(false); 62 hs->setUniqueID( 0 ); 62 63 this->handshakes.push_back(hs); 63 64 this->connectSynchronizeable(*hs); … … 76 77 this->handshakes.push_back( NULL ); 77 78 this->bActive = true; 79 80 this->setMaxConnections( 10 ); 78 81 } 79 82 … … 141 144 if ( this->type == NET_SERVER ) 142 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 } 143 162 144 163 for (int i = 0; i<handshakes.size(); i++) … … 164 183 PRINT(1)("handshake failed!\n"); 165 184 networkSockets[i]->disconnectServer(); 185 delete handshakes[i]; 186 handshakes[i] = NULL; 166 187 //TODO: handle error 167 188 } … … 192 213 193 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; 194 220 195 221 if ( dataLength<=0 ) … … 240 266 continue; 241 267 242 PRINTF( 0)("read %d bytes from socket\n", dataLength);268 PRINTF(5)("read %d bytes from socket\n", dataLength); 243 269 header = networkProtocol->extractHeader(upBuffer, dataLength); 244 270 dataLength -= sizeof(header); … … 250 276 } 251 277 278 if ( header.synchronizeableID == 0 ) 279 { 280 header.synchronizeableID = i; 281 } 282 252 283 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 253 284 { … … 259 290 } 260 291 } 261 262 #if 0263 int dataLength = 0;264 265 /* DOWNSTREAM */266 printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());267 PRINT(0)("============= DOWNSTREAM:===============\n");268 /* first of all read the synchronizeable's data: */269 if( this->isServer())270 dataLength = this->synchronizeables->readBytes((byte*)downBuffer);271 272 if( dataLength > 0)273 {274 /* send the received data to connectionMonitor */275 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);276 277 dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,278 *(this->synchronizeables), 12/* some random number (no real id)*/);279 280 /* pass the data to the network socket */281 // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);282 /* check if there was an error */283 if( dataLength == -1)284 {285 PRINTF(0)("Error in writing data to the NetworkSocket\n");286 }287 }288 289 290 /* UPSTREAM */291 dataLength = 0;292 PRINT(0)("============== UPSTREAM:================\n");293 /* first of all read the next Orxonox Network Header */294 295 if( this->state == NET_REC_HEADER)296 {297 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));298 if( dataLength == sizeof(Header))299 {300 this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);301 printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",302 this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,303 this->packetHeader.receiverID, this->packetHeader.length);304 /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/305 306 this->state = NET_REC_DATA;307 }308 }309 if( this->state == NET_REC_DATA)310 {311 /* now read the data */312 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);313 /* check if the data is available and process it if so */314 if( dataLength == this->packetHeader.length)315 {316 printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);317 /* send the received data to connectionMonitor */318 this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);319 /* now pass the data to the sync object */320 if( !this->isServer())321 this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);322 323 this->state = NET_REC_HEADER;324 }325 }326 327 #endif328 292 } 329 293 … … 343 307 networkSockets[clientId] = tempNetworkSocket; 344 308 handshakes[clientId] = new Handshake(true, clientId); 309 handshakes[clientId]->setUniqueID(clientId); 345 310 } else 346 311 { 347 312 clientId = networkSockets.size(); 348 313 networkSockets.push_back(tempNetworkSocket); 349 handshakes.push_back(new Handshake(true, clientId)); 350 } 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 351 325 352 326 PRINTF(0)("New Client: %d\n", clientId); … … 388 362 } 389 363 390 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 -
branches/network/src/lib/network/network_stream.h
r6060 r6090 43 43 inline bool isActive() const { return this->bActive; } 44 44 45 inline int getMaxConnections(){ return maxConnections; } 46 void setMaxConnections( int n ); 47 45 48 virtual void processData(); 46 49 … … 58 61 59 62 int myHostId; 63 int maxConnections; 60 64 61 65 void updateConnectionList(); -
branches/network/src/lib/network/synchronizeable.cc
r6060 r6090 31 31 owner = 0; 32 32 hostID = NetworkManager::getInstance()->getHostID(); 33 uniqueID = -1; 33 34 //state = ?; 34 35 -
branches/network/src/lib/network/synchronizeable.h
r6060 r6090 39 39 bool isServer(); 40 40 bool isOutOfSync(); 41 void setUniqueI d( int id ){ uniqueID = id; }41 void setUniqueID( int id ){ uniqueID = id; } 42 42 int getUniqueID() const { return uniqueID; }; 43 43 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } … … 45 45 inline int getOwner(){ return owner; } 46 46 inline void setOwner(int owner){ this->owner = owner; } 47 47 48 48 inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; } 49 49 … … 59 59 int state; 60 60 std::list<int> synchronizeRequests; 61 61 62 62 NetworkStream* networkStream; 63 63
Note: See TracChangeset
for help on using the changeset viewer.