- Timestamp:
- Nov 28, 2005, 10:14:48 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/network_protocol.cc
r5805 r5809 41 41 /* set the class id for the base object */ 42 42 this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol"); 43 this->headerLength = HEADER_LENGTH;43 this->headerLength = sizeof(Header); 44 44 } 45 45 … … 65 65 printf("NetworkProtocol: create header length = %i, bufferLength = %i\n", length, bufferLength); 66 66 //If there isn't enough space for the header return -1 67 if (length + headerLength > bufferLength)67 if (length + this->headerLength > bufferLength) 68 68 return -1; 69 69 … … 71 71 // printf("send byte[%i]=%u\n", i, data[i]); 72 72 73 73 74 //Create space for the header 74 75 for( int i = length - 1; i >= 0; i--) 75 data[i + headerLength] = data[i];76 data[i + this->headerLength] = data[i]; 76 77 77 //Include header 78 //Now create the header 79 /* protocol identifier */ 78 80 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; 80 92 } 81 93 … … 100 112 //Extract header 101 113 Header h; 114 //&h = data; 115 102 116 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]; 103 125 104 h.length = length - headerLength;105 h.data = data;106 126 107 127 // for(int i = 0; i < length; i++) … … 109 129 110 130 //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]; 113 133 114 134 return h; -
branches/network/src/lib/network/network_protocol.h
r5738 r5809 20 20 byte receiverID; 21 21 byte length; 22 byte* data;23 22 }; 24 23 -
branches/network/src/lib/network/network_stream.cc
r5805 r5809 78 78 /* set the class id for the base object */ 79 79 this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); 80 this->state = NET_REC_HEADER; 80 81 } 81 82 … … 95 96 void NetworkStream::processData() 96 97 { 97 int ret= 0;98 int dataLength = 0; 98 99 99 100 /* DOWNSTREAM */ … … 101 102 PRINT(0)("============= DOWNSTREAM:===============\n"); 102 103 /* first of all read the synchronizeable's data: */ 103 ret= this->synchronizeables->readBytes((byte*)downBuffer);104 dataLength = this->synchronizeables->readBytes((byte*)downBuffer); 104 105 105 106 /* send the received data to connectionMonitor */ 106 this->connectionMonitor->processPacket((byte*)downBuffer, ret);107 this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); 107 108 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)*/); 110 111 111 112 /* pass the data to the network socket */ 112 ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);113 dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 113 114 /* 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");} 115 116 116 117 117 118 118 119 /* UPSTREAM */ 119 ret= 0;120 dataLength = 0; 120 121 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) 126 125 { 127 /* send the received data to connectionMonitor */128 this->connectionMonitor->processPacket((byte*)upBuffer, ret);129 126 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*/ 132 136 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; 135 159 } 136 160 161 137 162 } -
branches/network/src/lib/network/network_stream.h
r5804 r5809 8 8 9 9 #include "data_stream.h" 10 #include "network_protocol.h" 10 11 11 12 class Synchronizeable; … … 13 14 class ConnectionMonitor; 14 15 class NetworkProtocol; 16 17 18 //<! The state of the NetworkStream 19 typedef 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 15 26 16 27 class NetworkStream : public DataStream … … 21 32 NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type); 22 33 NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type); 23 24 34 ~NetworkStream(); 25 35 … … 37 47 NetworkSocket* networkSocket; 38 48 int type; 49 int state; 50 Header packetHeader; 39 51 }; 40 52 #endif /* _NETWORK_STREAM */
Note: See TracChangeset
for help on using the changeset viewer.