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 | |
---|
24 | Handshake::Handshake( bool server, int clientId ) |
---|
25 | { |
---|
26 | /* set the class id for the base object */ |
---|
27 | this->setClassID(CL_HANDSHAKE, "Handshake"); |
---|
28 | |
---|
29 | this->server = server; |
---|
30 | this->clientId = clientId; |
---|
31 | this->state = HS_SEND_INIT; |
---|
32 | this->isOk = false; |
---|
33 | } |
---|
34 | |
---|
35 | void Handshake::writeBytes( const byte * data, int length ) |
---|
36 | { |
---|
37 | if ( state == HS_RECV_INIT ) |
---|
38 | { |
---|
39 | if ( length != _INITIAL_DATA_LENGTH ) |
---|
40 | { |
---|
41 | PRINTF(1)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH); |
---|
42 | state = HS_COMPLETED; |
---|
43 | return; |
---|
44 | } |
---|
45 | |
---|
46 | if ( strncmp((char*)data, _INITIAL_DATA, length) ) |
---|
47 | { |
---|
48 | PRINTF(1)("initial packed does not match\n"); |
---|
49 | state = HS_COMPLETED; |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | state = HS_SEND_VERSION; |
---|
54 | PRINTF(0)("got valid initial packet from client %d\n", clientId); |
---|
55 | return; |
---|
56 | } |
---|
57 | |
---|
58 | if ( state == HS_RECV_VERSION ) |
---|
59 | { |
---|
60 | if ( length != _ORXONOX_VERSION_LENGTH ) |
---|
61 | { |
---|
62 | PRINTF(1)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH); |
---|
63 | state = HS_COMPLETED; |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | if ( strncmp((char*)data, _ORXONOX_VERSION, length) ) |
---|
68 | { |
---|
69 | PRINTF(1)("versions do not match\n"); |
---|
70 | state = HS_COMPLETED; |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | if ( this->isServer() ) |
---|
75 | state = HS_SEND_ID; |
---|
76 | else |
---|
77 | state = HS_RECV_ID; |
---|
78 | |
---|
79 | PRINTF(0)("client %d's version does match\n", clientId); |
---|
80 | return; |
---|
81 | } |
---|
82 | |
---|
83 | if ( state == HS_RECV_ID ) |
---|
84 | { |
---|
85 | if ( length != _ORXONOX_VERSION_LENGTH ) |
---|
86 | { |
---|
87 | PRINTF(1)("hostID packet has wrong size %d instead of %d\n", length, 4); |
---|
88 | state = HS_COMPLETED; |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | this->state = HS_COMPLETED; |
---|
93 | this->isOk = true; |
---|
94 | memcpy(&(this->newHostId), data, 4); |
---|
95 | PRINTF(0)("got my hostID: %d\n", newHostId); |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | int Handshake::readBytes( byte * data, int maxLength, int & reciever ) |
---|
102 | { |
---|
103 | if ( state == HS_SEND_INIT ) |
---|
104 | { |
---|
105 | if ( maxLength < _INITIAL_DATA_LENGTH ) |
---|
106 | { |
---|
107 | PRINTF(1)("buffer too small for _INITIAL_DATA"); |
---|
108 | state = HS_COMPLETED; |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | state = HS_RECV_INIT; |
---|
113 | memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH); |
---|
114 | if ( this->isServer() ) |
---|
115 | reciever = clientId; |
---|
116 | return _INITIAL_DATA_LENGTH; |
---|
117 | } |
---|
118 | |
---|
119 | if ( state == HS_SEND_VERSION ) |
---|
120 | { |
---|
121 | if ( maxLength < _ORXONOX_VERSION_LENGTH ) |
---|
122 | { |
---|
123 | PRINTF(1)("buffer too small for version number"); |
---|
124 | state = HS_COMPLETED; |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | state = HS_RECV_VERSION; |
---|
129 | memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH); |
---|
130 | if ( this->isServer() ) |
---|
131 | reciever = clientId; |
---|
132 | return _ORXONOX_VERSION_LENGTH; |
---|
133 | } |
---|
134 | |
---|
135 | if ( state == HS_SEND_ID ) |
---|
136 | { |
---|
137 | if ( maxLength < 4 ) |
---|
138 | { |
---|
139 | PRINTF(1)("buffer too small for ID"); |
---|
140 | state = HS_COMPLETED; |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | state = HS_COMPLETED; |
---|
145 | memcpy(data, (byte*)&clientId, 4); |
---|
146 | if ( this->isServer() ) |
---|
147 | reciever = clientId; |
---|
148 | return 4; |
---|
149 | } |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | void Handshake::writeDebug( ) const |
---|
155 | { |
---|
156 | } |
---|
157 | |
---|
158 | void Handshake::readDebug( ) const |
---|
159 | { |
---|
160 | } |
---|