- Timestamp:
- Dec 11, 2005, 2:07:08 PM (19 years ago)
- Location:
- branches/network/src
- Files:
-
- 2 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/defs/class_id.h
r5996 r6043 220 220 221 221 // network stuff (range from 0x00000b00 to 0x00000bff) 222 CL_DATA_STREAM = 0x00b0 1000,222 CL_DATA_STREAM = 0x00b00b00, 223 223 CL_NETWORK_STREAM = 0x00000b01, 224 224 CL_NETWORK_PROTOCOL = 0x00000b02, … … 226 226 CL_SERVER_SOCKET = 0X00000b04, 227 227 CL_CONNECTION_MONITOR = 0x00000b05, 228 CL_HANDSHAKE = 0x00000b06, 228 229 229 230 -
branches/network/src/lib/network/Makefile.am
r5996 r6043 11 11 data_stream.cc \ 12 12 network_protocol.cc \ 13 server_socket.cc 13 server_socket.cc \ 14 handshake.cc 14 15 15 16 … … 22 23 data_stream.h \ 23 24 network_protocol.h \ 24 server_socket.h 25 server_socket.h \ 26 handshake.h 25 27 -
branches/network/src/lib/network/network_protocol.cc
r5822 r6043 57 57 * @arg bufferLength: the length of the internal buffer 58 58 * @arg source: reference to the source Synchronizeable object 59 * @arg remoteID: id number of the remote Synchronizeable object60 59 * @return: the new data length with header (the header data is included into byte* data) 61 60 * -1 if there isn't enough space in the buffer for the header 62 61 */ 63 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source , unsigned int remoteID)62 int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source) 64 63 { 65 printf("NetworkProtocol:create header length = %i, bufferLength = %i\n", length, bufferLength);64 PRINTF(0)("create header length = %i, bufferLength = %i\n", length, bufferLength); 66 65 //If there isn't enough space for the header return -1 67 66 if (length + this->headerLength > bufferLength) … … 82 81 data[1] = 0; 83 82 /* 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; 83 data[2] = (byte)(source.getUniqueID()); 87 84 /* data length*/ 88 data[ 4] = length;85 data[3] = length; 89 86 90 87 … … 104 101 if (length < headerLength) 105 102 { 106 PRINTF(0)("Received data is to short; it can't contain a header! ");103 PRINTF(0)("Received data is to short; it can't contain a header!\n"); 107 104 Header h; 108 105 h.protocol = 0; … … 117 114 /* version number */ 118 115 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]; 116 /* unique ID */ 117 h.uniqueID = data[2]; 123 118 /* data length*/ 124 h.length = data[ 4];119 h.length = data[3]; 125 120 126 121 -
branches/network/src/lib/network/network_protocol.h
r5822 r6043 17 17 byte protocol; 18 18 byte version; 19 byte senderID; 20 byte receiverID; 19 byte uniqueID; 21 20 byte length; 22 21 }; … … 33 32 ~NetworkProtocol(); 34 33 35 int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source , unsigned int remoteID);34 int createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source); 36 35 Header extractHeader(byte* data, int length); 37 36 -
branches/network/src/lib/network/network_stream.cc
r6039 r6043 26 26 #include "connection_monitor.h" 27 27 #include "synchronizeable.h" 28 #include "network_manager.h" 28 29 #include "list.h" 29 30 #include "debug.h" … … 56 57 this->networkProtocol = new NetworkProtocol(); 57 58 this->connectionMonitor = new ConnectionMonitor(); 59 60 Handshake* hs = new Handshake(false); 61 this->handshakes.push_back(hs); 62 this->connectSynchronizeable(*hs); 63 PRINTF(0)("NetworkStream: %s\n", hs->getName()); 58 64 } 59 65 … … 77 83 this->bActive = false; 78 84 this->serverSocket = NULL; 85 myHostId = 0; 79 86 } 80 87 … … 94 101 (*i)->disconnectServer(); 95 102 (*i)->destroy(); 103 } 104 } 105 106 for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) 107 { 108 if ( *i ) 109 { 110 delete (*i); 96 111 } 97 112 } … … 116 131 if ( this->type == NET_SERVER ) 117 132 this->updateConnectionList(); 133 134 for (HandshakeVector::iterator it = handshakes.begin(); it!=handshakes.end(); it++) 135 { 136 if ( *it ) 137 { 138 if ( (*it)->completed() ) 139 { 140 if ( (*it)->ok() ) 141 { 142 NetworkManager::getInstance()->setHostID( (*it)->getHostId() ); 143 myHostId = NetworkManager::getInstance()->getHostID(); 144 delete *it; 145 *it = NULL; 146 PRINT(0)("handshake finished\n"); 147 } 148 else 149 { 150 PRINT(1)("handshake failed!\n"); 151 delete *it; 152 *it = NULL; 153 //TODO: handle error 154 } 155 } 156 } 157 } 158 159 160 /* DOWNSTREAM */ 161 162 int dataLength; 163 int reciever; 164 Header header; 165 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 166 { 167 if ( (*it)->getOwner() == myHostId ) 168 { 169 PRINTF(0)("sync: %s\n", (*it)->getName()); 170 reciever = 0; 171 do { 172 dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, reciever); 173 174 if ( dataLength<=0 ){ 175 reciever = 0; 176 continue; 177 } 178 179 PRINTF(0)("read %d bytes\n", dataLength); 180 181 dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); 182 183 if ( dataLength<=0 ) 184 continue; 185 186 if ( reciever!=0 ) 187 { 188 if ( networkSockets[reciever] ) 189 { 190 PRINTF(0)("write %d bytes to socket\n", dataLength); 191 networkSockets[reciever]->writePacket(downBuffer, dataLength); 192 } 193 else 194 { 195 PRINTF(1)("networkSockets[reciever] == NULL\n"); 196 } 197 } 198 else 199 { 200 for ( int i = 1; i<networkSockets.size(); i++) 201 { 202 if ( networkSockets[i] ) 203 networkSockets[i]->writePacket(downBuffer, dataLength); 204 } 205 } 206 207 } while( reciever!=0 ); 208 } 209 } 210 211 /* UPSTREAM */ 212 213 for ( int i = 1; i<networkSockets.size(); i++) 214 { 215 if ( networkSockets[i] ) 216 { 217 do { 218 dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); 219 220 if ( dataLength<=0 ) 221 continue; 222 223 PRINTF(0)("read %d bytes from socket\n", dataLength); 224 header = networkProtocol->extractHeader(upBuffer, dataLength); 225 dataLength -= sizeof(header); 226 227 if ( dataLength != header.length ) 228 { 229 PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); 230 continue; 231 } 232 233 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 234 { 235 if ( *it && (*it)->getUniqueID()==header.uniqueID ) 236 (*it)->writeBytes(upBuffer+sizeof(header), dataLength); 237 } 238 239 } while ( dataLength>0 ); 240 } 241 } 118 242 119 243 #if 0 … … 199 323 freeSocketSlots.pop_back(); 200 324 networkSockets[clientId] = tempNetworkSocket; 325 handshakes[clientId] = new Handshake(true, clientId); 201 326 } else 202 327 { 203 328 clientId = networkSockets.size(); 204 329 networkSockets.push_back(tempNetworkSocket); 330 handshakes.push_back(new Handshake(true, clientId)); 205 331 } 206 332 207 333 PRINTF(0)("New Client: %d\n", clientId); 208 //TODO: start handshake 209 //new Handshake(true, clientId);334 335 this->connectSynchronizeable(*handshakes[clientId]); 210 336 } 211 337 … … 223 349 networkSockets[i]->destroy(); 224 350 networkSockets[i] = NULL; 351 delete handshakes[i]; 352 handshakes[i] = NULL; 225 353 226 354 if ( i == networkSockets.size()-1 ) 227 355 { 228 356 networkSockets.pop_back(); 357 handshakes.pop_back(); 229 358 } 230 359 else … … 237 366 238 367 } 368 369 -
branches/network/src/lib/network/network_stream.h
r6018 r6043 13 13 #include "network_protocol.h" 14 14 #include "server_socket.h" 15 #include "handshake.h" 15 16 16 17 class Synchronizeable; … … 21 22 22 23 typedef std::list<Synchronizeable*> SynchronizeableList; 23 typedef std::vector<NetworkSocket*> NetworkSocketVector; 24 typedef std::vector<NetworkSocket*> NetworkSocketVector; 25 typedef std::vector<Handshake*> HandshakeVector; 24 26 25 27 … … 47 49 SynchronizeableList synchronizeables; 48 50 NetworkSocketVector networkSockets; 51 HandshakeVector handshakes; 49 52 ServerSocket* serverSocket; 50 53 int type; … … 53 56 std::list<int> freeSocketSlots; 54 57 58 int myHostId; 59 55 60 void updateConnectionList(); 56 61 }; -
branches/network/src/lib/network/synchronizeable.cc
r5997 r6043 17 17 #include "synchronizeable.h" 18 18 #include "netdefs.h" 19 #include "network_manager.h" 19 20 20 21 … … 26 27 27 28 //owner = ?; 28 //hostID = ?;29 hostID = NetworkManager::getInstance()->getHostID(); 29 30 //state = ?; 30 31 … … 55 56 * read data from NetworkStream 56 57 */ 57 int Synchronizeable::readBytes(byte* data )58 int Synchronizeable::readBytes(byte* data, int maxLength, int& reciever) 58 59 {} 59 60 … … 65 66 void Synchronizeable::readDebug() const 66 67 {} 67 68 69 70 68 71 69 … … 111 109 return this->state & STATE_OUTOFSYNC == STATE_OUTOFSYNC; 112 110 } 111 112 113 -
branches/network/src/lib/network/synchronizeable.h
r5997 r6043 29 29 30 30 virtual void writeBytes(const byte* data, int length); 31 virtual int readBytes(byte* data );31 virtual int readBytes(byte* data, int maxLength, int& reciever); 32 32 virtual void writeDebug() const; 33 33 virtual void readDebug() const; 34 35 36 37 38 39 void setIsServer(bool isServer); 40 void setIsOutOfSync(bool outOfSync); 34 35 void setIsServer( bool isServer ); 36 void setIsOutOfSync( bool outOfSync ); 41 37 bool isServer(); 42 38 bool isOutOfSync(); 39 void setUniqueId( int id ){ uniqueID = id; } 40 int getUniqueID() const { return uniqueID; }; 41 void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); } 42 43 inline int getOwner(){ return owner; } 44 inline void setOwner(int owner){ this->owner = owner; } 43 45 44 46 private: 45 47 46 48 int uniqueID; 47 48 49 49 50 51 50 52 //static std::vector<Synchronizeable*> classList; 51 53 int owner; -
branches/network/src/subprojects/network/network_unit_test.cc
r6026 r6043 190 190 Synchronizeable* ss = new SimpleSync("Server\0"); 191 191 192 netMan->createServer( *ss,port);192 netMan->createServer(/**ss, */port); 193 193 SDL_Delay(20); 194 194 … … 229 229 Synchronizeable* ss = new SimpleSync("Client\0"); 230 230 231 netMan->establishConnection( ip, *ss);231 netMan->establishConnection((const char*)"localhost", port/*,ip, *ss*/); 232 232 233 233 for(;;) -
branches/network/src/subprojects/network/read_sync.cc
r5996 r6043 80 80 * read data from Synchronizeable 81 81 */ 82 int ReadSync::readBytes(byte* data )82 int ReadSync::readBytes(byte* data, int maxLength, int& reciever) 83 83 { 84 84 return 0; -
branches/network/src/subprojects/network/read_sync.h
r5996 r6043 17 17 18 18 virtual void writeBytes(const byte* data, int length); 19 virtual int readBytes(byte* data );19 virtual int readBytes(byte* data, int maxLength, int& reciever); 20 20 21 21 -
branches/network/src/subprojects/network/simple_sync.cc
r5996 r6043 86 86 * read data from Synchronizeable 87 87 */ 88 int SimpleSync::readBytes(byte* data )88 int SimpleSync::readBytes(byte* data, int maxLength, int& reciever) 89 89 { 90 90 PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength); -
branches/network/src/subprojects/network/simple_sync.h
r5996 r6043 17 17 18 18 virtual void writeBytes(const byte* data, int length); 19 virtual int readBytes(byte* data );19 virtual int readBytes(byte* data, int maxLength, int& reciever); 20 20 21 21 -
branches/network/src/subprojects/network/write_sync.cc
r5996 r6043 86 86 * read data from Synchronizeable 87 87 */ 88 int WriteSync::readBytes(byte* data )88 int WriteSync::readBytes(byte* data, int maxLength, int& reciever) 89 89 { 90 90 PRINTF(0)("WriteSync: sent %i bytes of data\n", this->outLength); -
branches/network/src/subprojects/network/write_sync.h
r5996 r6043 17 17 18 18 virtual void writeBytes(const byte* data, int length); 19 virtual int readBytes(byte* data );19 virtual int readBytes(byte* data, int maxLength, int& reciever); 20 20 21 21
Note: See TracChangeset
for help on using the changeset viewer.