[6043] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: christoph |
---|
| 13 | co-programmer: |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 19 | */ |
---|
| 20 | #define DEBUG_MODULE_NETWORK |
---|
| 21 | |
---|
| 22 | #include "handshake.h" |
---|
| 23 | |
---|
[6116] | 24 | Handshake::Handshake( bool server, int clientId, int networkGameManagerId ) |
---|
[6695] | 25 | : Synchronizeable() |
---|
[6043] | 26 | { |
---|
| 27 | /* set the class id for the base object */ |
---|
| 28 | this->setClassID(CL_HANDSHAKE, "Handshake"); |
---|
| 29 | |
---|
[6053] | 30 | this->setIsServer(server); |
---|
[6043] | 31 | this->clientId = clientId; |
---|
[6116] | 32 | this->networkGameManagerId = networkGameManagerId; |
---|
[6053] | 33 | this->state = 0; |
---|
[6043] | 34 | this->isOk = false; |
---|
[6053] | 35 | this->setOwner(0); |
---|
| 36 | |
---|
[6695] | 37 | this->setSynchronized(true); |
---|
[6090] | 38 | PRINTF(5)("Handshake created clientId = %d\n", clientId); |
---|
[6043] | 39 | } |
---|
| 40 | |
---|
[6341] | 41 | int Handshake::writeBytes( const byte * data, int length, int sender) |
---|
[6043] | 42 | { |
---|
[6062] | 43 | 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); |
---|
[6053] | 44 | |
---|
[6341] | 45 | SYNCHELP_READ_BEGIN(); |
---|
| 46 | |
---|
[6053] | 47 | if ( hasState( HS_COMPLETED ) ) |
---|
[6341] | 48 | return 0; |
---|
[6053] | 49 | |
---|
| 50 | if ( !hasState( HS_RECVD_INIT ) ) |
---|
[6043] | 51 | { |
---|
| 52 | if ( length != _INITIAL_DATA_LENGTH ) |
---|
| 53 | { |
---|
[6053] | 54 | PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH); |
---|
| 55 | setState( HS_COMPLETED ); |
---|
[6341] | 56 | return 0; |
---|
[6043] | 57 | } |
---|
| 58 | |
---|
| 59 | if ( strncmp((char*)data, _INITIAL_DATA, length) ) |
---|
| 60 | { |
---|
[6053] | 61 | PRINTF(0)("initial packed does not match\n"); |
---|
| 62 | setState( HS_COMPLETED ); |
---|
[6341] | 63 | return length; |
---|
[6043] | 64 | } |
---|
| 65 | |
---|
[6053] | 66 | setState( HS_RECVD_INIT ); |
---|
[6043] | 67 | PRINTF(0)("got valid initial packet from client %d\n", clientId); |
---|
[6341] | 68 | return length; |
---|
[6043] | 69 | } |
---|
| 70 | |
---|
[6053] | 71 | if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) ) |
---|
[6043] | 72 | { |
---|
| 73 | if ( length != _ORXONOX_VERSION_LENGTH ) |
---|
| 74 | { |
---|
[6053] | 75 | PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH); |
---|
| 76 | setState( HS_COMPLETED ); |
---|
[6341] | 77 | return 0; |
---|
[6043] | 78 | } |
---|
| 79 | |
---|
| 80 | if ( strncmp((char*)data, _ORXONOX_VERSION, length) ) |
---|
| 81 | { |
---|
[6053] | 82 | PRINTF(0)("versions do not match\n"); |
---|
| 83 | setState( HS_COMPLETED ); |
---|
[6341] | 84 | return length; |
---|
[6043] | 85 | } |
---|
| 86 | |
---|
[6053] | 87 | setState( HS_RECVD_VER ); |
---|
[6043] | 88 | |
---|
| 89 | PRINTF(0)("client %d's version does match\n", clientId); |
---|
[6341] | 90 | return length; |
---|
[6043] | 91 | } |
---|
| 92 | |
---|
[6062] | 93 | if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) ) |
---|
[6043] | 94 | { |
---|
[6341] | 95 | if ( length != INTSIZE+INTSIZE ) |
---|
[6043] | 96 | { |
---|
[6090] | 97 | PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1); |
---|
[6053] | 98 | setState( HS_COMPLETED ); |
---|
[6341] | 99 | return 0; |
---|
[6043] | 100 | } |
---|
| 101 | |
---|
[6053] | 102 | setState( HS_COMPLETED ); |
---|
| 103 | setState( HS_RECVD_HID ); |
---|
[6043] | 104 | this->isOk = true; |
---|
[6341] | 105 | SYNCHELP_READ_INT( this->newHostId ); |
---|
| 106 | SYNCHELP_READ_INT( this->newNetworkGameManagerId ); |
---|
[6090] | 107 | |
---|
| 108 | if ( newHostId == 0 ) |
---|
| 109 | { |
---|
| 110 | setState( HS_WAS_REJECT ); |
---|
| 111 | isOk = false; |
---|
| 112 | PRINTF(0)("Server did not accept handshake!\n"); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
[6116] | 116 | PRINTF(0)("got my hostID: %d and networkGameManagerId: %d\n", newHostId, newNetworkGameManagerId); |
---|
[6090] | 117 | } |
---|
[6341] | 118 | return SYNCHELP_READ_N; |
---|
[6043] | 119 | } |
---|
| 120 | |
---|
| 121 | } |
---|
| 122 | |
---|
[6053] | 123 | int Handshake::readBytes( byte * data, int maxLength, int * reciever ) |
---|
[6043] | 124 | { |
---|
[6062] | 125 | 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); |
---|
[6053] | 126 | |
---|
[6341] | 127 | SYNCHELP_WRITE_BEGIN(); |
---|
| 128 | |
---|
[6053] | 129 | if ( hasState( HS_COMPLETED ) ) |
---|
| 130 | return 0; |
---|
| 131 | |
---|
| 132 | if ( !hasState( HS_SENT_INIT ) ) |
---|
[6043] | 133 | { |
---|
| 134 | if ( maxLength < _INITIAL_DATA_LENGTH ) |
---|
| 135 | { |
---|
[6053] | 136 | PRINTF(0)("buffer too small for _INITIAL_DATA"); |
---|
| 137 | setState( HS_COMPLETED ); |
---|
[6043] | 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
[6053] | 141 | setState( HS_SENT_INIT ); |
---|
[6043] | 142 | memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH); |
---|
| 143 | if ( this->isServer() ) |
---|
[6053] | 144 | *reciever = clientId; |
---|
[6043] | 145 | return _INITIAL_DATA_LENGTH; |
---|
| 146 | } |
---|
| 147 | |
---|
[6053] | 148 | if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) ) |
---|
[6043] | 149 | { |
---|
| 150 | if ( maxLength < _ORXONOX_VERSION_LENGTH ) |
---|
| 151 | { |
---|
[6053] | 152 | PRINTF(0)("buffer too small for version number"); |
---|
| 153 | setState( HS_COMPLETED ); |
---|
[6043] | 154 | return 0; |
---|
| 155 | } |
---|
| 156 | |
---|
[6053] | 157 | setState( HS_SENT_VER ); |
---|
[6043] | 158 | memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH); |
---|
| 159 | if ( this->isServer() ) |
---|
[6053] | 160 | *reciever = clientId; |
---|
[6043] | 161 | return _ORXONOX_VERSION_LENGTH; |
---|
| 162 | } |
---|
| 163 | |
---|
[6053] | 164 | if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) ) |
---|
[6043] | 165 | { |
---|
[6115] | 166 | if ( maxLength < 2 ) |
---|
[6043] | 167 | { |
---|
[6053] | 168 | PRINTF(0)("buffer too small for ID"); |
---|
| 169 | setState( HS_COMPLETED ); |
---|
[6043] | 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
[6053] | 173 | setState( HS_SENT_HID ); |
---|
| 174 | setState( HS_COMPLETED ); |
---|
[6090] | 175 | |
---|
| 176 | if ( hasState( HS_DO_REJECT ) ) |
---|
| 177 | { |
---|
| 178 | isOk = false; |
---|
| 179 | //memcpy(data, (byte*)0, 4); |
---|
[6341] | 180 | SYNCHELP_WRITE_INT( 0 ); |
---|
| 181 | SYNCHELP_WRITE_INT( 0 ); |
---|
[6090] | 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | isOk = true; |
---|
| 186 | //memcpy(data, &clientId, 4); |
---|
[6341] | 187 | SYNCHELP_WRITE_INT( clientId ); |
---|
| 188 | SYNCHELP_WRITE_INT( networkGameManagerId ); |
---|
[6090] | 189 | } |
---|
| 190 | *reciever = clientId; |
---|
[6341] | 191 | return SYNCHELP_WRITE_N; |
---|
[6043] | 192 | } |
---|
| 193 | |
---|
| 194 | return 0; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | void Handshake::writeDebug( ) const |
---|
| 198 | { |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void Handshake::readDebug( ) const |
---|
| 202 | { |
---|
| 203 | } |
---|