Changeset 6139 in orxonox.OLD for trunk/src/lib/network/network_socket.cc
- Timestamp:
- Dec 16, 2005, 6:45:32 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 {
Note: See TracChangeset
for help on using the changeset viewer.