Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5630 in orxonox.OLD for branches


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

network_socket: uses now a thread to send

Location:
branches/network/src
Files:
3 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
  • branches/network/src/lib/network/network_socket.h

    r5628 r5630  
    88#define _NETWORK_SOCKET
    99
     10//if you want to use outgoing buffer define _USE_OUTGOING_BUFFER
     11#define _USE_OUTGOING_BUFFER
     12
    1013#define _INCOMING_BUFFER_SIZE 10240
     14#define _OUTGOING_BUFFER_SIZE 302400
    1115#define _LOCAL_BUFFER_SIZE 1024
     16//sleep if incoming buffer is full
    1217#define _MSECONDS_SLEEP_FULL_BUFFER 10
     18//sleep if outgoing buffer is empty
     19#define _MSECONDS_SLEEP_EMPTY_BUFFER 10
    1320
    1421/* contains memmove and memcpy */
     
    3643//  UDPsocket udpSocket;
    3744
    38   byte buf[_INCOMING_BUFFER_SIZE];
    39   int bufferlength;
     45  byte incomingBuffer[_INCOMING_BUFFER_SIZE];
     46#ifdef _USE_OUTGOING_BUFFER
     47  byte outgoingBuffer[_OUTGOING_BUFFER_SIZE];
     48#endif
     49  int incomingBufferLength;
     50#ifdef _USE_OUTGOING_BUFFER
     51  int outgoingBufferLength;
     52#endif
    4053
    41   SDL_mutex * mutex;
    42   SDL_mutex * socketmutex;
     54  SDL_mutex * incomingBufferMutex;
     55#ifdef _USE_OUTGOING_BUFFER
     56  SDL_mutex * outgoingBufferMutex;
     57#endif
     58  SDL_mutex * socketMutex;
    4359  bool terminateThread;
    4460
    4561  static int thread_listen(void * data);
    4662  static int thread_read(void * data);
     63#ifdef _USE_OUTGOING_BUFFER
     64  static int thread_write(void * data);
     65#endif
     66
     67  bool _isListening;
    4768
    4869public:
  • branches/network/src/subprojects/network/network_unit_test.cc

    r5628 r5630  
    2727  SDLNet_ResolveHost(&ip, "127.0.0.1", 9999);
    2828  server.listen(9999);
     29  SDL_Delay(20);
    2930  client.connectToServer(ip, 9999);
    3031  char buf[1024];
     
    3738
    3839  int n;
    39         char * str1 = "client to server";
     40  char * str1 = "client to server";
    4041  char * str2 = "server to client";
    4142  n = client.writeBytes((byte*)str1, strlen(str1)+1);
    42         printf("%d bytes send from client\n", n);
     43  printf("%d bytes send from client\n", n);
    4344  n = server.writeBytes((byte*)str2, strlen(str2)+1);
    44         printf("%d bytes send from server\n", n);
     45  printf("%d bytes send from server\n", n);
    4546  SDL_Delay(1000);
    46  
     47
    4748  printf("read from server\n");
    4849  n = server.readBytes((byte*)buf, 1024);
     
    6364  printf("testing a bigger amount of data\n");
    6465
    65 #define _N_ELEMENTS 212992
    66   char sendbuf[_N_ELEMENTS];
    67   char recvbuf[_N_ELEMENTS];
     66#define _N_ELEMENTS 212994
     67  char sendbuf[_N_ELEMENTS+1];
     68  char recvbuf[_N_ELEMENTS+1];
     69  sendbuf[_N_ELEMENTS] = '\0';
     70  recvbuf[_N_ELEMENTS] = '\0';
    6871
    6972  for (int i = 0; i<_N_ELEMENTS; i++)
    70     sendbuf[i] = i%30 + 30;
     73    sendbuf[i] = i%26 + 65;
    7174
    7275  printf("write\n");
    73   client.writeBytes((byte*)sendbuf, _N_ELEMENTS);
     76  printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS));
    7477
    75   SDL_Delay(500);
     78  SDL_Delay(50);
    7679
    7780  printf("read\n");
     
    8285  {
    8386    SDL_Delay(10);
    84     printf("read\n");
     87    //printf("read\n");
    8588    nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset);
    8689    offset += nbytes;
    87     printf("nbytes=%d, offset=%d\n", nbytes, offset);
     90    //printf("nbytes=%d, offset=%d\n", nbytes, offset);
    8891  }
    8992
    9093  printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset);
     94
     95  //printf("%s\n%s\n", sendbuf, recvbuf);
     96
     97  for (int i = 0; i<_N_ELEMENTS; i++)
     98  {
     99    if (sendbuf[i]!=recvbuf[i])
     100    {
     101      printf("byte %d is the first difference\n", i+1);
     102      break;
     103    }
     104  }
    91105
    92106  return 0;
Note: See TracChangeset for help on using the changeset viewer.