Changeset 6053 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Dec 11, 2005, 6:01:04 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/handshake.cc
r6043 r6053 27 27 this->setClassID(CL_HANDSHAKE, "Handshake"); 28 28 29 this->se rver = server;29 this->setIsServer(server); 30 30 this->clientId = clientId; 31 this->state = HS_SEND_INIT;31 this->state = 0; 32 32 this->isOk = false; 33 this->setOwner(0); 34 35 PRINTF(0)("Handshake created clientId = %d\n", clientId); 33 36 } 34 37 35 38 void Handshake::writeBytes( const byte * data, int length ) 36 39 { 37 if ( state == HS_RECV_INIT ) 40 PRINTF(0)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state); 41 42 if ( hasState( HS_COMPLETED ) ) 43 return; 44 45 if ( !hasState( HS_RECVD_INIT ) ) 38 46 { 39 47 if ( length != _INITIAL_DATA_LENGTH ) 40 48 { 41 PRINTF( 1)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);42 s tate = HS_COMPLETED;49 PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH); 50 setState( HS_COMPLETED ); 43 51 return; 44 52 } … … 46 54 if ( strncmp((char*)data, _INITIAL_DATA, length) ) 47 55 { 48 PRINTF( 1)("initial packed does not match\n");49 s tate = HS_COMPLETED;56 PRINTF(0)("initial packed does not match\n"); 57 setState( HS_COMPLETED ); 50 58 return; 51 59 } 52 60 53 s tate = HS_SEND_VERSION;61 setState( HS_RECVD_INIT ); 54 62 PRINTF(0)("got valid initial packet from client %d\n", clientId); 55 63 return; 56 64 } 57 65 58 if ( state == HS_RECV_VERSION)66 if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) ) 59 67 { 60 68 if ( length != _ORXONOX_VERSION_LENGTH ) 61 69 { 62 PRINTF( 1)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);63 s tate = HS_COMPLETED;70 PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH); 71 setState( HS_COMPLETED ); 64 72 return; 65 73 } … … 67 75 if ( strncmp((char*)data, _ORXONOX_VERSION, length) ) 68 76 { 69 PRINTF( 1)("versions do not match\n");70 s tate = HS_COMPLETED;77 PRINTF(0)("versions do not match\n"); 78 setState( HS_COMPLETED ); 71 79 return; 72 80 } 73 81 74 if ( this->isServer() ) 75 state = HS_SEND_ID; 76 else 77 state = HS_RECV_ID; 82 setState( HS_RECVD_VER ); 78 83 79 84 PRINTF(0)("client %d's version does match\n", clientId); … … 81 86 } 82 87 83 if ( state == HS_RECV_ID)88 if ( hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) ) 84 89 { 85 if ( length != _ORXONOX_VERSION_LENGTH)90 if ( length != 4 ) 86 91 { 87 PRINTF( 1)("hostID packet has wrong size %d instead of %d\n", length, 4);88 s tate = HS_COMPLETED;92 PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4); 93 setState( HS_COMPLETED ); 89 94 return; 90 95 } 91 96 92 this->state = HS_COMPLETED; 97 setState( HS_COMPLETED ); 98 setState( HS_RECVD_HID ); 93 99 this->isOk = true; 94 100 memcpy(&(this->newHostId), data, 4); … … 99 105 } 100 106 101 int Handshake::readBytes( byte * data, int maxLength, int &reciever )107 int Handshake::readBytes( byte * data, int maxLength, int * reciever ) 102 108 { 103 if ( state == HS_SEND_INIT ) 109 PRINTF(0)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state); 110 111 if ( hasState( HS_COMPLETED ) ) 112 return 0; 113 114 if ( !hasState( HS_SENT_INIT ) ) 104 115 { 105 116 if ( maxLength < _INITIAL_DATA_LENGTH ) 106 117 { 107 PRINTF( 1)("buffer too small for _INITIAL_DATA");108 s tate = HS_COMPLETED;118 PRINTF(0)("buffer too small for _INITIAL_DATA"); 119 setState( HS_COMPLETED ); 109 120 return 0; 110 121 } 111 122 112 s tate = HS_RECV_INIT;123 setState( HS_SENT_INIT ); 113 124 memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH); 114 125 if ( this->isServer() ) 115 reciever = clientId;126 *reciever = clientId; 116 127 return _INITIAL_DATA_LENGTH; 117 128 } 118 129 119 if ( state == HS_SEND_VERSION)130 if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) ) 120 131 { 121 132 if ( maxLength < _ORXONOX_VERSION_LENGTH ) 122 133 { 123 PRINTF( 1)("buffer too small for version number");124 s tate = HS_COMPLETED;134 PRINTF(0)("buffer too small for version number"); 135 setState( HS_COMPLETED ); 125 136 return 0; 126 137 } 127 138 128 s tate = HS_RECV_VERSION;139 setState( HS_SENT_VER ); 129 140 memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH); 130 141 if ( this->isServer() ) 131 reciever = clientId;142 *reciever = clientId; 132 143 return _ORXONOX_VERSION_LENGTH; 133 144 } 134 145 135 if ( state == HS_SEND_ID)146 if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) ) 136 147 { 137 148 if ( maxLength < 4 ) 138 149 { 139 PRINTF( 1)("buffer too small for ID");140 s tate = HS_COMPLETED;150 PRINTF(0)("buffer too small for ID"); 151 setState( HS_COMPLETED ); 141 152 return 0; 142 153 } 143 154 144 state = HS_COMPLETED; 155 setState( HS_SENT_HID ); 156 setState( HS_COMPLETED ); 157 isOk = true; 145 158 memcpy(data, (byte*)&clientId, 4); 146 159 if ( this->isServer() ) 147 reciever = clientId;160 *reciever = clientId; 148 161 return 4; 149 162 } -
branches/network/src/lib/network/handshake.h
r6043 r6053 17 17 18 18 typedef enum HandshakeState { 19 HS_SEN D_INIT = 0,20 HS_RECV _INIT,21 HS_SEN D_VERSION,22 HS_RECV _VERSION,23 HS_SEN D_ID,24 HS_RECV _ID,25 HS_COMPLETED ,19 HS_SENT_INIT = 0x00000001, 20 HS_RECVD_INIT = 0x00000002, 21 HS_SENT_VER = 0x00000004, 22 HS_RECVD_VER = 0x00000008, 23 HS_SENT_HID = 0x00000010, 24 HS_RECVD_HID = 0x00000020, 25 HS_COMPLETED = 0X00000040, 26 26 27 27 NUM_STATES … … 32 32 public: 33 33 Handshake(bool server, int clientId = 0); 34 inline bool completed(){ return state == HS_COMPLETED; }34 inline bool completed(){ return hasState( HS_COMPLETED ); } 35 35 inline bool ok(){ return isOk; } 36 36 inline int getHostId(){ return newHostId; } 37 37 38 38 virtual void writeBytes(const byte* data, int length); 39 virtual int readBytes(byte* data, int maxLength, int &reciever);39 virtual int readBytes(byte* data, int maxLength, int * reciever); 40 40 virtual void writeDebug() const; 41 41 virtual void readDebug() const; … … 43 43 private: 44 44 int state; 45 int server;46 45 int clientId; 47 46 int newHostId; 48 47 bool isOk; 48 49 inline bool hasState( int a ){ return (state & a) == a; } 50 inline void setState( int a ){ state = state | a; } 51 inline void unsetState( int a ){ state = state & (~a); } 49 52 50 53 }; -
branches/network/src/lib/network/network_stream.cc
r6043 r6053 29 29 #include "list.h" 30 30 #include "debug.h" 31 #include "class_list.h" 31 32 32 33 /* include your own header */ … … 73 74 this->connectionMonitor = new ConnectionMonitor(); 74 75 this->networkSockets.push_back( NULL ); 76 this->handshakes.push_back( NULL ); 75 77 this->bActive = true; 76 78 } … … 128 130 void NetworkStream::processData() 129 131 { 130 //PRINTF(0)("processData()\n");132 PRINTF(0)("processData()\n"); 131 133 if ( this->type == NET_SERVER ) 132 134 this->updateConnectionList(); 133 135 134 for ( HandshakeVector::iterator it = handshakes.begin(); it!=handshakes.end(); it++)135 { 136 if ( *it)137 { 138 if ( (*it)->completed() )136 for (int i = 0; i<handshakes.size(); i++) 137 { 138 if ( handshakes[i] ) 139 { 140 if ( handshakes[i]->completed() ) 139 141 { 140 if ( (*it)->ok() ) 141 { 142 NetworkManager::getInstance()->setHostID( (*it)->getHostId() ); 143 myHostId = NetworkManager::getInstance()->getHostID(); 144 delete *it; 145 *it = NULL; 142 if ( handshakes[i]->ok() ) 143 { 144 if ( type != NET_SERVER ) 145 { 146 NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); 147 myHostId = NetworkManager::getInstance()->getHostID(); 148 } 146 149 PRINT(0)("handshake finished\n"); 147 150 } … … 149 152 { 150 153 PRINT(1)("handshake failed!\n"); 151 delete *it; 152 *it = NULL; 154 networkSockets[i]->disconnectServer(); 153 155 //TODO: handle error 154 156 } … … 165 167 for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) 166 168 { 167 if ( (*it)->getOwner() == myHostId ) 168 { 169 PRINTF(0)("sync: %s\n", (*it)->getName()); 170 reciever = 0; 169 if ( (*it) && (*it)->getOwner() == myHostId ) 170 { 171 171 do { 172 dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, reciever); 172 reciever = 0; 173 dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); 174 175 PRINTF(0)("reciever = %d\n", reciever); 173 176 174 177 if ( dataLength<=0 ){ … … 179 182 PRINTF(0)("read %d bytes\n", dataLength); 180 183 181 dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); 184 if (ClassList::exists((*it))) 185 { 186 PRINTF(0)("before cast\n"); 187 dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); 188 PRINTF(0)("after cast\n"); 189 } 190 else 191 { 192 PRINTF(0)("instance does not exist anymore\n"); 193 //synchronizeables.remove(it); 194 } 182 195 183 196 if ( dataLength<=0 ) … … 188 201 if ( networkSockets[reciever] ) 189 202 { 190 PRINTF(0)("write %d bytes to socket \n", dataLength);203 PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); 191 204 networkSockets[reciever]->writePacket(downBuffer, dataLength); 192 205 } … … 198 211 else 199 212 { 200 for ( int i = 1; i<networkSockets.size(); i++)213 for ( int i = 0; i<networkSockets.size(); i++) 201 214 { 202 215 if ( networkSockets[i] ) 216 { 217 PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); 203 218 networkSockets[i]->writePacket(downBuffer, dataLength); 219 } 204 220 } 205 221 } … … 211 227 /* UPSTREAM */ 212 228 213 for ( int i = 1; i<networkSockets.size(); i++)229 for ( int i = 0; i<networkSockets.size(); i++) 214 230 { 215 231 if ( networkSockets[i] ) -
branches/network/src/lib/network/synchronizeable.cc
r6043 r6053 14 14 co-programmer: Benjamin Wuest 15 15 */ 16 17 #define DEBUG_MODULE_NETWORK 16 18 17 19 #include "synchronizeable.h" … … 51 53 */ 52 54 void Synchronizeable::writeBytes(const byte* data, int length) 53 {} 55 { 56 PRINTF(1)("Synchronizeable::writeBytes was called\n"); 57 } 54 58 55 59 /** 56 60 * read data from NetworkStream 57 61 */ 58 int Synchronizeable::readBytes(byte* data, int maxLength, int& reciever) 59 {} 62 int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever) 63 { 64 PRINTF(1)("Synchronizeable::readBytes was called\n"); 65 } 60 66 61 67 -
branches/network/src/lib/network/synchronizeable.h
r6043 r6053 29 29 30 30 virtual void writeBytes(const byte* data, int length); 31 virtual int readBytes(byte* data, int maxLength, int &reciever);31 virtual int readBytes(byte* data, int maxLength, int * reciever); 32 32 virtual void writeDebug() const; 33 33 virtual void readDebug() const;
Note: See TracChangeset
for help on using the changeset viewer.