Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5987 in orxonox.OLD for branches/network/src/lib


Ignore:
Timestamp:
Dec 8, 2005, 8:13:22 AM (19 years ago)
Author:
rennerc
Message:

network_socket: removed listen function
server_socket: created serversocket
network_unit_test: replaced sockettest for new client/socket model

Location:
branches/network/src/lib/network
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/Makefile.am

    r5822 r5987  
    1010                      network_stream.cc \
    1111                      data_stream.cc \
    12                       network_protocol.cc
     12                      network_protocol.cc \
     13                      server_socket.cc
    1314
    1415
     
    2021                 network_stream.h \
    2122                 data_stream.h \
    22                  network_protocol.h
     23                 network_protocol.h \
     24                 server_socket.h
    2325
  • branches/network/src/lib/network/network_socket.cc

    r5829 r5987  
    4747
    4848
     49NetworkSocket::NetworkSocket( TCPsocket sock )
     50{
     51  this->init();
     52  this->tcpSocket = sock;
     53
     54  SDL_CreateThread(thread_read, (void*)this);
     55  SDL_CreateThread(thread_write, (void*)this);
     56}
     57
    4958void NetworkSocket::init()
    5059{
     
    7584}
    7685
     86
     87
    7788/**
    7889 * Default destructor
     
    8596  PRINTF(5)("SDL_net shutdown\n");
    8697
    87   _isListening = false;
    88 
    8998  SDL_DestroyMutex(incomingBufferMutex);
    9099  SDL_DestroyMutex(outgoingBufferMutex);
     
    113122  }
    114123
    115   _isListening = false;
    116 
    117124  SDL_CreateThread(thread_read, (void*)this);
    118125  SDL_CreateThread(thread_write, (void*)this);
    119126}
    120127
    121 /**
    122  * Tells the NetworkSocket to listen on a specific port for incoming connections.
    123  * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.
    124  * @param port
    125  */
    126 void NetworkSocket::listen(unsigned int port)
    127 {
    128   _isListening = true;
    129   //check if not already connected or listening
    130   if (tcpSocket)
    131   {
    132     PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");
    133     _isListening = false;
    134     return;
    135   }
    136 
    137   IPaddress ip;
    138 
    139   if (SDLNet_ResolveHost(&ip, NULL, port)==-1)
    140   {
    141     PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    142     _isListening = false;
    143     return;
    144   }
    145 
    146   tcpSocket = SDLNet_TCP_Open(&ip);
    147 
    148   if (!tcpSocket)
    149   {
    150     PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    151     _isListening = false;
    152     return;
    153   }
    154 
    155   SDL_CreateThread(thread_listen, (void*)this);
    156   SDL_CreateThread(thread_write, (void*)this);
    157 }
    158128
    159129/**
     
    184154  PRINTF(0)("NetworkSocket::writeBytes()\n");
    185155#ifdef _USE_OUTGOING_BUFFER
    186 
    187   //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);
    188 //   if (length>_OUTGOING_BUFFER_SIZE)
    189 //   {
    190 //     int res = 0;
    191 //     int n = length / _OUTGOING_BUFFER_SIZE;
    192 //     if (length % _OUTGOING_BUFFER_SIZE != 0)
    193 //       n++;
    194 // //     printf("n=%d\n", n);
    195 //     SDL_Delay(500);
    196 //     for (int i = 0; i<n; i++)
    197 //     {
    198 // //       printf("i=%d\n", i);
    199 //       if (i==n-1)
    200 //       {
    201 //         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);
    202 // //         printf("res = %d\n", res);
    203 //       }
    204 //       else
    205 //       {
    206 //         res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);
    207 // //         printf("res = %d\n", res);
    208 //       }
    209 //     }
    210 //     return res;
    211 //   }
    212156
    213157#define min(a,b) (a<b)?a:b
     
    256200int NetworkSocket::readBytes(byte * data, int length)
    257201{
     202  PRINTF(0)("NetworkSocket::readBytes()\n");
    258203  if (data==NULL)
    259204    return 0;
     
    299244}
    300245
    301 /**
    302  * used to create a thread to listen
    303  * will call thrad_read when established connection
    304  * @param data: pointer to NetwortSocket
    305  */
    306 int NetworkSocket::thread_listen( void * data )
    307 {
    308   NetworkSocket * self = (NetworkSocket*)data;
    309   self->_isListening = true;
    310   TCPsocket tempsocket;
    311 
    312   tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
    313 
    314   while (!tempsocket && !self->terminateThread)
    315   {
    316     tempsocket = SDLNet_TCP_Accept(self->tcpSocket);
    317     SDL_Delay(_MSECONDS_SLEEP_LISTEN);
    318   }
    319 
    320   SDL_mutexP(self->socketMutex);
    321   SDLNet_TCP_Close(self->tcpSocket);
    322   self->tcpSocket = NULL;
    323 
    324   if (!tempsocket)
    325   {
    326     printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
    327     //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
    328     SDL_mutexV(self->socketMutex);
    329     self->_isListening = false;
    330     return -1;
    331   }
    332 
    333   self->tcpSocket = tempsocket;
    334 
    335   SDL_mutexV(self->socketMutex);
    336 
    337   self->_isListening = false;
    338   return thread_read(data);
    339 }
    340246
    341247/**
     
    357263
    358264    //if buffer is full
    359     if (nbytestoread<=0 || self->_isListening)
     265    if (nbytestoread<=0)
    360266    {
    361267      SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER);
     
    408314
    409315    //if buffer is full
    410     if (nbytestowrite<=0 || self->_isListening)
     316    if (nbytestowrite<=0)
    411317    {
    412318      SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER);
     
    445351}
    446352
     353bool NetworkSocket::writePacket( byte * data, int length )
     354{
     355  PRINTF(0)("NetworkSocket::writePacket()\n");
     356  if (length>255)
     357  {
     358    PRINTF(1)("Packet length > 255!\n");
     359    return false;
     360  }
     361
     362  byte blen = length;
     363
     364  writeBytes(&blen, 1);
     365  writeBytes(data, length);
     366}
     367
     368int NetworkSocket::readPacket( byte * data, int maxLength )
     369{
     370  PRINTF(0)("NetworkSocket::readPacket()\n");
     371  if (incomingBufferLength<1)
     372  {
     373    return 0;
     374  }
     375
     376  byte blen = incomingBuffer[0];
     377
     378  if (blen>maxLength)
     379  {
     380    PRINTF(1)("Buffersize is too small (%d) for packet (%d)\n", maxLength, blen);
     381    return 0;
     382  }
     383
     384  if (blen>incomingBufferLength)
     385  {
     386    return 0;
     387  }
     388
     389  readBytes(&blen, 1);
     390  int res = readBytes(data, blen);
     391
     392  if (res!=blen)
     393    return -1;
     394  else
     395    return blen;
     396
     397}
     398
     399
  • branches/network/src/lib/network/network_socket.h

    r5829 r5987  
    1818//sleep if outgoing buffer is empty
    1919#define _MSECONDS_SLEEP_EMPTY_BUFFER 10
    20 //sleep when waiting for connections
    21 #define _MSECONDS_SLEEP_LISTEN 100
     20
    2221
    2322/* contains memmove and memcpy */
     
    6463  bool terminateThread;
    6564
    66   static int thread_listen(void * data);
    6765  static int thread_read(void * data);
    6866#ifdef _USE_OUTGOING_BUFFER
     
    7068#endif
    7169
    72   bool _isListening;
     70  int writeBytes(byte * data, int length);
     71  int readBytes(byte * data, int length);
     72  int readBlock(byte * data, int length);
     73
     74  void init();
    7375
    7476public:
     
    7678  NetworkSocket();
    7779  NetworkSocket(IPaddress ip);
     80  NetworkSocket(TCPsocket sock);
    7881  ~NetworkSocket();
    7982
    8083  void connectToServer(IPaddress ip);
    81   void listen(unsigned int port);
    8284  void disconnectServer();
    83   int writeBytes(byte * data, int length);
    84   int readBytes(byte * data, int length);
    85   int readBlock(byte * data, int length);
    8685
    87 private:
    88   void init();
     86  bool writePacket(byte * data, int length);
     87  int readPacket(byte * data, int maxLength);
    8988
    9089};
  • branches/network/src/lib/network/network_stream.cc

    r5833 r5987  
    6767  this->init();
    6868  this->networkSocket = new NetworkSocket();
    69   this->networkSocket->listen(port);
     69//  this->networkSocket->listen(port);
    7070  this->networkProtocol = new NetworkProtocol();
    7171  this->synchronizeables = NULL;
     
    9393  this->init();
    9494  this->networkSocket = new NetworkSocket();
    95   this->networkSocket->listen(port);
     95//  this->networkSocket->listen(port);
    9696  this->networkProtocol = new NetworkProtocol();
    9797  this->synchronizeables = &sync;
     
    151151
    152152    /* pass the data to the network socket */
    153     dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
     153 //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
    154154    /* check if there was an error */
    155155    if( dataLength == -1)
     
    167167  if( this->state == NET_REC_HEADER)
    168168  {
    169     dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
     169//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
    170170    if( dataLength == sizeof(Header))
    171171    {
     
    182182  {
    183183    /* now read the data */
    184     dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
     184//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
    185185    /* check if the data is available and process it if so */
    186186    if( dataLength == this->packetHeader.length)
Note: See TracChangeset for help on using the changeset viewer.