Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 18, 2005, 2:21:30 PM (18 years ago)
Author:
rennerc
Message:

network_socket: uses now a thread to send

File:
1 edited

Legend:

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

    r5628 r5630  
    3838
    3939  tcpSocket = NULL;
    40   bufferlength = 0;
    41 
    42   mutex = SDL_CreateMutex();
    43   socketmutex = SDL_CreateMutex();
     40  incomingBufferLength = 0;
     41  outgoingBufferLength = 0;
     42
     43  incomingBufferMutex = SDL_CreateMutex();
     44  outgoingBufferMutex = SDL_CreateMutex();
     45  socketMutex = SDL_CreateMutex();
    4446  terminateThread = false;
    4547
     
    6870  PRINTF(5)("SDL_net shutdown\n");
    6971
    70   SDL_DestroyMutex(mutex);
    71   SDL_DestroyMutex(socketmutex);
     72  _isListening = false;
     73
     74  SDL_DestroyMutex(incomingBufferMutex);
     75  SDL_DestroyMutex(outgoingBufferMutex);
     76  SDL_DestroyMutex(socketMutex);
    7277}
    7378
     
    9499  }
    95100
     101  _isListening = false;
     102
    96103  SDL_CreateThread(thread_read, (void*)this);
     104  SDL_CreateThread(thread_write, (void*)this);
    97105}
    98106
     
    104112void NetworkSocket::listen(unsigned int port)
    105113{
     114  _isListening = true;
    106115  //check if not already connected or listening
    107116  if (tcpSocket)
    108117  {
    109118    PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");
     119    _isListening = false;
    110120    return;
    111121  }
     
    116126  {
    117127    PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
     128    _isListening = false;
    118129    return;
    119130  }
     
    124135  {
    125136    PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
     137    _isListening = false;
    126138    return;
    127139  }
    128140
    129141  SDL_CreateThread(thread_listen, (void*)this);
     142  SDL_CreateThread(thread_write, (void*)this);
    130143}
    131144
     
    138151  /* Close the connection */
    139152
    140   SDL_mutexP(socketmutex);
     153  SDL_mutexP(socketMutex);
    141154  SDLNet_TCP_Close(tcpSocket);
    142155  tcpSocket = NULL;
    143   SDL_mutexV(socketmutex);
     156  SDL_mutexV(socketMutex);
    144157
    145158}
     
    156169int NetworkSocket::writeBytes(byte * data, int length)
    157170{
    158   SDL_mutexP(socketmutex);
     171#ifdef _USE_OUTGOING_BUFFER
     172
     173  //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);
     174  if (length>_OUTGOING_BUFFER_SIZE)
     175  {
     176    int res = 0;
     177    int n = length / _OUTGOING_BUFFER_SIZE;
     178    if (length % _OUTGOING_BUFFER_SIZE != 0)
     179      n++;
     180//     printf("n=%d\n", n);
     181    SDL_Delay(500);
     182    for (int i = 0; i<n; i++)
     183    {
     184//       printf("i=%d\n", i);
     185      if (i==n-1)
     186      {
     187        res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);
     188//         printf("res = %d\n", res);
     189      }
     190      else
     191      {
     192        res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);
     193//         printf("res = %d\n", res);
     194      }
     195    }
     196    return res;
     197  }
     198
     199#define min(a,b) (a<b)?a:b
     200  int nbytes = min(_OUTGOING_BUFFER_SIZE - outgoingBufferLength, length);
     201#undef min
     202
     203  if (!tcpSocket || data==NULL || nbytes<=0)
     204    return 0;
     205
     206  SDL_mutexP(outgoingBufferMutex);
     207
     208  memcpy(outgoingBuffer + outgoingBufferLength, data, nbytes);
     209  outgoingBufferLength += nbytes;
     210
     211  SDL_mutexV(outgoingBufferMutex);
     212
     213  return nbytes;
     214#else
     215  SDL_mutexP(socketMutex);
    159216
    160217  if (!tcpSocket || data==NULL)
     
    163220  int res = SDLNet_TCP_Send(tcpSocket, data, length);
    164221
    165   SDL_mutexV(socketmutex);
     222  SDL_mutexV(socketMutex);
    166223
    167224  if (res<length)
     
    169226
    170227  return res;
     228#endif
    171229}
    172230
     
    186244    return 0;
    187245
    188   int nbytes = (length<bufferlength) ? length : bufferlength;
     246  int nbytes = (length<incomingBufferLength) ? length : incomingBufferLength;
     247
     248  //printf("readBytes: nbytes = %d; length=%d; incomingBufferLength=%d\n", nbytes, length, incomingBufferLength);
    189249
    190250  // just in case ...
     
    195255      return 0;
    196256
    197   SDL_mutexP(mutex);
    198 
    199   memcpy(data, buf, nbytes);
     257  SDL_mutexP(incomingBufferMutex);
     258
     259  memcpy(data, incomingBuffer, nbytes);
    200260
    201261  //important: use memmove because the memory areas may overlap
    202   memmove(buf, buf+nbytes, bufferlength-nbytes);
    203   bufferlength -= nbytes;
    204 
    205   SDL_mutexV(mutex);
     262  memmove(incomingBuffer, incomingBuffer+nbytes, incomingBufferLength-nbytes);
     263  incomingBufferLength -= nbytes;
     264
     265  SDL_mutexV(incomingBufferMutex);
    206266
    207267  return nbytes;
     
    216276{
    217277  NetworkSocket * self = (NetworkSocket*)data;
     278  self->_isListening = true;
    218279  TCPsocket tempsocket;
    219280
    220281  tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
    221282
    222   SDL_mutexP(self->socketmutex);
     283  while (!tempsocket && !self->terminateThread)
     284    tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
     285
     286  SDL_mutexP(self->socketMutex);
    223287  SDLNet_TCP_Close(self->tcpSocket);
    224288  self->tcpSocket = NULL;
     
    227291  {
    228292    PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
    229     SDL_mutexV(self->socketmutex);
     293    //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
     294    SDL_mutexV(self->socketMutex);
     295    self->_isListening = false;
    230296    return -1;
    231297  }
     
    233299  self->tcpSocket = tempsocket;
    234300
    235   SDL_mutexV(self->socketmutex);
    236 
     301  SDL_mutexV(self->socketMutex);
     302
     303  self->_isListening = false;
    237304  return thread_read(data);
    238305}
     
    252319  {
    253320#define min(a,b) (a<b)?a:b
    254     nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE);
     321    nbytestoread = min(_INCOMING_BUFFER_SIZE - self->incomingBufferLength, _LOCAL_BUFFER_SIZE);
     322#undef min
    255323
    256324    //if buffer is full
    257     if (nbytestoread<=0)
     325    if (nbytestoread<=0 || self->_isListening)
    258326    {
    259327      SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
     
    263331    nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread);
    264332
    265     SDL_mutexP(self->mutex);
     333    SDL_mutexP(self->incomingBufferMutex);
    266334
    267335    if (nbytesread<=0)
     
    269337      PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
    270338
    271       SDL_mutexP(self->socketmutex);
     339      SDL_mutexP(self->socketMutex);
    272340
    273341      SDLNet_TCP_Close(self->tcpSocket);
    274342      self->tcpSocket = NULL;
    275343
    276       SDL_mutexV(self->socketmutex);
    277       SDL_mutexV(self->mutex);
     344      SDL_mutexV(self->socketMutex);
     345      SDL_mutexV(self->incomingBufferMutex);
    278346      return -1;
    279347    }
    280348
    281     memcpy(self->buf+self->bufferlength, buffer, nbytesread);
    282     self->bufferlength += nbytesread;
    283 
    284     SDL_mutexV(self->mutex);
     349    //printf("thread_read: nbytesread=%d\n", nbytesread);
     350
     351    memcpy(self->incomingBuffer+self->incomingBufferLength, buffer, nbytesread);
     352    self->incomingBufferLength += nbytesread;
     353
     354    SDL_mutexV(self->incomingBufferMutex);
    285355  }
    286356
     
    288358}
    289359
    290 
    291 
     360int NetworkSocket::thread_write( void * data )
     361{
     362  int nbyteswrite = 0;
     363  int nbytestowrite = 0;
     364  char buffer[_LOCAL_BUFFER_SIZE];
     365  NetworkSocket * self = (NetworkSocket*)data;
     366
     367  while (!self->terminateThread)
     368  {
     369#define min(a,b) (a<b)?a:b
     370    nbytestowrite = min(self->outgoingBufferLength, _LOCAL_BUFFER_SIZE);
     371#undef min
     372
     373    //printf("thread_write nbytes=%d listening=%d\n", nbytestowrite, (int)self->_isListening);
     374
     375    //if buffer is full
     376    if (nbytestowrite<=0 || self->_isListening)
     377    {
     378      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
     379      continue;
     380    }
     381
     382    SDL_mutexP(self->outgoingBufferMutex);
     383
     384    //printf("a\n");
     385
     386    memcpy(buffer, self->outgoingBuffer, nbytestowrite);
     387    self->outgoingBufferLength -= nbytestowrite;
     388    memmove(self->outgoingBuffer, self->outgoingBuffer+nbytestowrite, self->outgoingBufferLength);
     389
     390    SDL_mutexV(self->outgoingBufferMutex);
     391
     392    nbyteswrite = SDLNet_TCP_Send(self->tcpSocket, buffer, nbytestowrite);
     393
     394    if (nbyteswrite<=0)
     395    {
     396      PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());
     397
     398      SDL_mutexP(self->socketMutex);
     399
     400      SDLNet_TCP_Close(self->tcpSocket);
     401      self->tcpSocket = NULL;
     402
     403      SDL_mutexV(self->socketMutex);
     404      return -1;
     405    }
     406
     407  }
     408
     409  return 0;
     410}
     411
Note: See TracChangeset for help on using the changeset viewer.