Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2005, 10:14:48 PM (18 years ago)
Author:
patrick
Message:

The NetworkSocket now uses a real network protocol: packet size is read out dynamicaly: therefore the NetworkStream now has an internal state that
checks if its reading header or body data

File:
1 edited

Legend:

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

    r5805 r5809  
    7878  /* set the class id for the base object */
    7979  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
     80  this->state = NET_REC_HEADER;
    8081}
    8182
     
    9596void NetworkStream::processData()
    9697{
    97   int ret = 0;
     98  int dataLength = 0;
    9899
    99100  /* DOWNSTREAM */
     
    101102  PRINT(0)("============= DOWNSTREAM:===============\n");
    102103  /* first of all read the synchronizeable's data: */
    103   ret = this->synchronizeables->readBytes((byte*)downBuffer);
     104  dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
    104105
    105106  /* send the received data to connectionMonitor */
    106   this->connectionMonitor->processPacket((byte*)downBuffer, ret);
     107  this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
    107108
    108   ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE,
    109                                             *(this->synchronizeables), 12);
     109  dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
     110      *(this->synchronizeables), 12/* some random number (no real id)*/);
    110111
    111112  /* pass the data to the network socket */
    112   ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);
     113  dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
    113114  /* check if there was an error */
    114   if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
     115  if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
    115116
    116117
    117118
    118119  /* UPSTREAM */
    119   ret = 0;
     120  dataLength = 0;
    120121  PRINT(0)("============== UPSTREAM:================\n");
    121   /* first of all read  data from networkSocket*/
    122   ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */);
    123   /* error checking: data read? */
    124   if( ret != 11 /*PACKAGE_SIZE + sizeof(Header)*/) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");}
    125   else
     122  /* first of all read the next Orxonox Network Header */
     123
     124  switch( this->state)
    126125  {
    127     /* send the received data to connectionMonitor */
    128     this->connectionMonitor->processPacket((byte*)upBuffer, ret);
    129126
    130     /* extract Header */
    131     Header header = this->networkProtocol->extractHeader((byte*) upBuffer , ret);
     127    case NET_REC_HEADER:
     128      dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
     129      if( dataLength == sizeof(Header))
     130      {
     131        this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
     132        printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
     133               this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
     134               this->packetHeader.receiverID, this->packetHeader.length);
     135        /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
    132136
    133     /* now pass the data to the sync object */
    134     this->synchronizeables->writeBytes((byte*)upBuffer, header.length);
     137        this->state = NET_REC_DATA;
     138      }
     139      break;
     140
     141    case NET_REC_DATA:
     142      /* now read the data */
     143      dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
     144      /* check if the data is available and process it if so */
     145      if( dataLength == this->packetHeader.length)
     146      {
     147        printf("NetworkStream::processData() - Got Data: \n");
     148        /* send the received data to connectionMonitor */
     149        this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
     150        /* now pass the data to the sync object */
     151        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
     152
     153        this->state = NET_REC_HEADER;
     154      }
     155      break;
     156
     157    default:
     158      break;
    135159  }
    136160
     161
    137162}
Note: See TracChangeset for help on using the changeset viewer.