Changeset 6053 in orxonox.OLD for branches/network/src/lib/network/handshake.cc
- Timestamp:
- Dec 11, 2005, 6:01:04 PM (18 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.