Changeset 6090 in orxonox.OLD for branches/network/src/lib/network/network_stream.cc
- Timestamp:
- Dec 13, 2005, 4:29:32 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note: See TracChangeset
for help on using the changeset viewer.