Changeset 7954 in orxonox.OLD for trunk/src/lib/network/handshake.cc
- Timestamp:
- May 29, 2006, 3:28:41 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/handshake.cc
r6815 r7954 24 24 #include <cassert> 25 25 26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId )26 Handshake::Handshake( bool server, int clientId, int networkGameManagerId, int messageManagerId ) 27 27 : Synchronizeable() 28 28 { … … 31 31 32 32 this->setIsServer(server); 33 this->clientId = clientId; 34 this->networkGameManagerId = networkGameManagerId; 35 this->state = 0; 36 this->isOk = false; 37 this->setOwner(0); 33 34 orxId_handler = registerVarId( new SynchronizeableInt( &localState.orxId, &remoteState.orxId, "orxonoxId", PERMISSION_ALL ) ); 35 version_handler = registerVarId( new SynchronizeableInt( &localState.version, &remoteState.version, "version", PERMISSION_ALL ) ); 36 netManId_handler = registerVarId( new SynchronizeableInt( &localState.networkManagerId, &remoteState.networkManagerId, "networkManagerId", PERMISSION_ALL ) ); 37 msgManId_handler = registerVarId( new SynchronizeableInt( &localState.messageManagerId, &remoteState.messageManagerId, "messageManagerId", PERMISSION_ALL ) ); 38 hostId_handler = registerVarId( new SynchronizeableInt( &localState.hostId, &remoteState.hostId, "hostId", PERMISSION_ALL ) ); 39 completed_handler = registerVarId( new SynchronizeableInt( &localState.completed, &remoteState.completed, "completed", PERMISSION_ALL ) ); 40 error_handler = registerVarId( new SynchronizeableInt( &localState.error, &remoteState.error, "error", PERMISSION_ALL ) ); 41 errorString_handler = registerVarId( new SynchronizeableString( &localState.errorString, &remoteState.errorString, "errorString", PERMISSION_ALL ) ); 42 43 candel_id = registerVarId( new SynchronizeableInt( &localState.canDel, &remoteState.canDel, "canDel", PERMISSION_ALL ) ); 44 45 localState.completed = 0; 46 localState.error = 0; 47 localState.errorString = ""; 48 localState.hostId = clientId; 49 localState.networkManagerId = networkGameManagerId; 50 this->localState.messageManagerId = messageManagerId; 51 localState.orxId = _ORXONOX_ID; 52 localState.version = _ORXONOX_VERSION; 53 localState.canDel = 0; 54 55 remoteState.completed = 0; 56 remoteState.error = 0; 57 remoteState.errorString = ""; 58 remoteState.hostId = -1; 59 remoteState.networkManagerId = -1; 60 remoteState.messageManagerId = -1; 61 remoteState.orxId = 0; 62 remoteState.version = 0; 63 remoteState.canDel = 0; 38 64 39 65 this->setSynchronized(true); 40 PRINTF( 5)("Handshake created clientId = %d\n", clientId);66 PRINTF(0)("Handshake created clientId = %d\n", clientId); 41 67 } 42 68 43 int Handshake::writeBytes( const byte * data, int length, int sender) 69 /** 70 * handler for changes in synced vars 71 * @param id id's which have changed 72 */ 73 void Handshake::varChangeHandler( std::list< int > & id ) 44 74 { 45 PRINTF(5)("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); 75 for ( std::list<int>::iterator it = id.begin(); it != id.end(); it++ ) 76 { 77 if ( *it == orxId_handler ) 78 { 79 if ( remoteState.orxId != _ORXONOX_ID ) 80 { 81 localState.error = 1; 82 localState.completed = 1; 83 localState.errorString = "Seems not to be orxonox!"; 84 continue; 85 } 46 86 47 SYNCHELP_READ_BEGIN(); 87 } 88 89 if ( *it == version_handler ) 90 { 91 if ( remoteState.version != _ORXONOX_VERSION ) 92 { 93 localState.error = 2; 94 localState.completed = 1; 95 localState.errorString = "Versions of server and client do not match!"; 96 continue; 97 } 48 98 49 if ( hasState( HS_COMPLETED ) ) 50 return 0; 51 52 if ( !hasState( HS_RECVD_INIT ) ) 53 { 54 if ( length != _INITIAL_DATA_LENGTH ) 99 } 100 101 if ( *it == candel_id ) 55 102 { 56 PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH); 57 setState( HS_COMPLETED ); 58 return 0; 103 PRINTF(0)("handshake finished candel changed\n"); 59 104 } 60 61 if ( strncmp((char*)data, _INITIAL_DATA, length) ) 62 { 63 PRINTF(0)("initial packed does not match\n"); 64 setState( HS_COMPLETED ); 65 return length; 66 } 67 68 setState( HS_RECVD_INIT ); 69 PRINTF(0)("got valid initial packet from client %d\n", clientId); 70 return length; 105 71 106 } 72 73 if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) ) 74 { 75 if ( length != _ORXONOX_VERSION_LENGTH ) 76 { 77 PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH); 78 setState( HS_COMPLETED ); 79 return 0; 80 } 81 82 if ( strncmp((char*)data, _ORXONOX_VERSION, length) ) 83 { 84 PRINTF(0)("versions do not match\n"); 85 setState( HS_COMPLETED ); 86 return length; 87 } 88 89 setState( HS_RECVD_VER ); 90 91 PRINTF(0)("client %d's version does match\n", clientId); 92 return length; 93 } 94 95 if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) ) 96 { 97 /*if ( length != INTSIZE+INTSIZE ) 98 { 99 PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, INTSIZE+INTSIZE); 100 setState( HS_COMPLETED ); 101 return 0; 102 }*/ 103 104 setState( HS_COMPLETED ); 105 setState( HS_RECVD_HID ); 106 this->isOk = true; 107 SYNCHELP_READ_INT( this->newHostId, NWT_HS_HOST_ID ); 108 SYNCHELP_READ_INT( this->newNetworkGameManagerId, NWT_HS_NGM_ID ); 109 110 if ( newHostId == 0 ) 111 { 112 setState( HS_WAS_REJECT ); 113 isOk = false; 114 PRINTF(0)("Server did not accept handshake!\n"); 115 } 116 else 117 { 118 PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId); 119 } 120 return SYNCHELP_READ_N; 121 } 122 107 108 if ( 109 remoteState.orxId == _ORXONOX_ID && 110 remoteState.version == _ORXONOX_VERSION 111 ) 112 localState.completed = 1; 123 113 } 124 114 125 int Handshake::readBytes( byte * data, int maxLength, int * reciever )126 {127 PRINTF(5)("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);128 129 SYNCHELP_WRITE_BEGIN();130 131 if ( hasState( HS_COMPLETED ) )132 return 0;133 134 if ( !hasState( HS_SENT_INIT ) )135 {136 if ( maxLength < _INITIAL_DATA_LENGTH )137 {138 PRINTF(0)("buffer too small for _INITIAL_DATA");139 setState( HS_COMPLETED );140 return 0;141 }142 143 setState( HS_SENT_INIT );144 memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);145 if ( this->isServer() )146 *reciever = clientId;147 return _INITIAL_DATA_LENGTH;148 }149 150 if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )151 {152 if ( maxLength < _ORXONOX_VERSION_LENGTH )153 {154 PRINTF(0)("buffer too small for version number");155 setState( HS_COMPLETED );156 return 0;157 }158 159 setState( HS_SENT_VER );160 memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);161 if ( this->isServer() )162 *reciever = clientId;163 return _ORXONOX_VERSION_LENGTH;164 }165 166 if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )167 {168 if ( maxLength < 2 )169 {170 PRINTF(0)("buffer too small for ID");171 setState( HS_COMPLETED );172 return 0;173 }174 175 setState( HS_SENT_HID );176 setState( HS_COMPLETED );177 178 if ( hasState( HS_DO_REJECT ) )179 {180 isOk = false;181 //memcpy(data, (byte*)0, 4);182 SYNCHELP_WRITE_INT( 0, NWT_HS_HOST_ID);183 SYNCHELP_WRITE_INT( 0, NWT_HS_NGM_ID);184 }185 else186 {187 isOk = true;188 //memcpy(data, &clientId, 4);189 SYNCHELP_WRITE_INT( clientId, NWT_HS_HOST_ID );190 SYNCHELP_WRITE_INT( networkGameManagerId, NWT_HS_NGM_ID );191 }192 *reciever = clientId;193 return SYNCHELP_WRITE_N;194 }195 196 return 0;197 }198 199 void Handshake::writeDebug( ) const200 {201 }202 203 void Handshake::readDebug( ) const204 {205 }
Note: See TracChangeset
for help on using the changeset viewer.