Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6139 in orxonox.OLD for trunk/src/lib/network/network_socket.cc


Ignore:
Timestamp:
Dec 16, 2005, 6:45:32 PM (18 years ago)
Author:
patrick
Message:

trunk: merged branche network with trunk using command: svn merge -r5999:HEAD, conflicts resolved in favor of the trunk bla

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/network_socket.cc

    r5996 r6139  
    2828#include "debug.h"
    2929
    30 
    3130/**
    3231 * Default constructor
     
    5251  this->tcpSocket = sock;
    5352
    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);
    5655}
    5756
     
    6564  outgoingBufferLength = 0;
    6665
     66  readThread = NULL;
     67  writeThread = NULL;
     68
     69
     70  thread_write_running = false;
     71  thread_read_running = false;
     72
    6773  incomingBufferMutex = SDL_CreateMutex();
    6874  outgoingBufferMutex = SDL_CreateMutex();
     75
     76
    6977  socketMutex = SDL_CreateMutex();
    7078  terminateThread = false;
     
    8997 * Default destructor
    9098 */
    91 NetworkSocket::~ NetworkSocket( )
    92 {
     99NetworkSocket::~NetworkSocket( )
     100{
     101  this->terminateThread = true;
    93102  /* Quit SDL_net */
    94103  // NOTE: what if other instances of NetworkSocket running?
     
    99108  SDL_DestroyMutex(outgoingBufferMutex);
    100109  SDL_DestroyMutex(socketMutex);
     110  SDL_DestroyMutex(threadTerminationMutex);
    101111}
    102112
     
    122132  }
    123133
    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);
    126136}
    127137
     
    152162int NetworkSocket::writeBytes(byte * data, int length)
    153163{
    154   PRINTF(0)("NetworkSocket::writeBytes()\n");
     164  PRINTF(5)("NetworkSocket::writeBytes()\n");
    155165#ifdef _USE_OUTGOING_BUFFER
    156166
     
    200210int NetworkSocket::readBytes(byte * data, int length)
    201211{
    202   PRINTF(0)("NetworkSocket::readBytes()\n");
     212  PRINTF(5)("NetworkSocket::readBytes()\n");
    203213  if (data==NULL)
    204214    return 0;
     
    256266  NetworkSocket * self = (NetworkSocket*)data;
    257267
     268  self->thread_read_running = true;
     269
    258270  while (!self->terminateThread)
    259271  {
     
    263275
    264276    //if buffer is full
    265     if (nbytestoread<=0)
     277    if (nbytestoread<=0 || !self->tcpSocket)
    266278    {
    267279      SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
     
    275287    if (nbytesread<=0)
    276288    {
    277       printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
     289      if (nbytesread<0)
     290        printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
    278291
    279292      SDL_mutexP(self->socketMutex);
     
    284297      SDL_mutexV(self->socketMutex);
    285298      SDL_mutexV(self->incomingBufferMutex);
    286       return -1;
     299      continue;
    287300    }
    288301
     
    295308  }
    296309
     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");
    297325  return 0;
    298326}
     
    305333  NetworkSocket * self = (NetworkSocket*)data;
    306334
     335  self->thread_write_running = true;
     336
    307337  while (!self->terminateThread)
    308338  {
     
    314344
    315345    //if buffer is full
    316     if (nbytestowrite<=0)
     346    if (nbytestowrite<=0 || !self->tcpSocket)
    317347    {
    318348      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
     
    342372
    343373      SDL_mutexV(self->socketMutex);
    344       return -1;
     374      continue;
    345375    }
    346376
    347377  }
    348378
    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");
    350394  return 0;
    351395}
     
    353397bool NetworkSocket::writePacket( byte * data, int length )
    354398{
    355   PRINTF(0)("NetworkSocket::writePacket()\n");
     399  PRINTF(5)("NetworkSocket::writePacket()\n");
    356400  if (length>255)
    357401  {
     
    368412int NetworkSocket::readPacket( byte * data, int maxLength )
    369413{
    370   PRINTF(0)("NetworkSocket::readPacket()\n");
     414  PRINTF(5)("NetworkSocket::readPacket()\n");
    371415  if (incomingBufferLength<1)
    372416  {
Note: See TracChangeset for help on using the changeset viewer.