Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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

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

Legend:

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

    r5805 r5809  
    4141  /* set the class id for the base object */
    4242  this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol");
    43   this->headerLength = HEADER_LENGTH;
     43  this->headerLength = sizeof(Header);
    4444}
    4545
     
    6565  printf("NetworkProtocol: create header length = %i, bufferLength = %i\n", length, bufferLength);
    6666  //If there isn't enough space for the header return -1
    67   if (length + headerLength > bufferLength)
     67  if (length + this->headerLength > bufferLength)
    6868    return -1;
    6969
     
    7171//     printf("send byte[%i]=%u\n", i, data[i]);
    7272
     73
    7374  //Create space for the header
    7475  for( int i = length - 1; i >= 0; i--)
    75     data[i + headerLength] = data[i];
     76    data[i + this->headerLength] = data[i];
    7677
    77   //Include header
     78  //Now create the header
     79  /* protocol identifier */
    7880  data[0] = 255;
    79   return length + headerLength;
     81  /* version number */
     82  data[1] = 0;
     83  /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
     84  data[2] = (byte)source.getClassID();
     85  /* receiver ID */
     86  data[3] = remoteID;
     87  /* data length*/
     88  data[4] = length;
     89
     90
     91  return length + this->headerLength;
    8092}
    8193
     
    100112  //Extract header
    101113  Header h;
     114  //&h = data;
     115
    102116  h.protocol = data[0];
     117  /* version number */
     118  h.version = data[1];
     119  /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
     120  h.senderID = data[2];
     121  /* receiver ID */
     122  h.receiverID = data[3];
     123  /* data length*/
     124  h.length = data[4];
    103125
    104   h.length = length - headerLength;
    105   h.data = data;
    106126
    107127//   for(int i = 0; i < length; i++)
     
    109129
    110130  //Remove header
    111   for (int i = headerLength; i < length; i++)
    112     data[i - headerLength] = data[i];
     131//   for (int i = headerLength; i < length; i++)
     132//     data[i - headerLength] = data[i];
    113133
    114134  return h;
  • branches/network/src/lib/network/network_protocol.h

    r5738 r5809  
    2020  byte receiverID;
    2121  byte length;
    22   byte* data;
    2322};
    2423
  • 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}
  • branches/network/src/lib/network/network_stream.h

    r5804 r5809  
    88
    99#include "data_stream.h"
     10#include "network_protocol.h"
    1011
    1112class Synchronizeable;
     
    1314class ConnectionMonitor;
    1415class NetworkProtocol;
     16
     17
     18//<! The state of the NetworkStream
     19typedef enum NetStat {
     20  NET_REC_HEADER = 0,                          //!< Waiting for header
     21  NET_REC_DATA,                                //!< Waiting for data
     22
     23  NUM_STATES                                   //!< Number of states
     24};
     25
    1526
    1627class NetworkStream : public DataStream
     
    2132  NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type);
    2233  NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type);
    23 
    2434  ~NetworkStream();
    2535
     
    3747   NetworkSocket* networkSocket;
    3848   int                    type;
     49   int                    state;
     50   Header                 packetHeader;
    3951};
    4052#endif /* _NETWORK_STREAM */
Note: See TracChangeset for help on using the changeset viewer.