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: claudio |
---|
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 | |
---|
23 | #include "base_object.h" |
---|
24 | #include "network_protocol.h" |
---|
25 | #include "network_socket.h" |
---|
26 | #include "connection_monitor.h" |
---|
27 | #include "synchronizeable.h" |
---|
28 | #include "network_manager.h" |
---|
29 | #include "list.h" |
---|
30 | #include "debug.h" |
---|
31 | #include "class_list.h" |
---|
32 | |
---|
33 | /* include your own header */ |
---|
34 | #include "network_stream.h" |
---|
35 | |
---|
36 | /* probably unnecessary */ |
---|
37 | using namespace std; |
---|
38 | |
---|
39 | |
---|
40 | #define PACKAGE_SIZE 256 |
---|
41 | |
---|
42 | |
---|
43 | NetworkStream::NetworkStream() |
---|
44 | : DataStream() |
---|
45 | { |
---|
46 | this->init(); |
---|
47 | /* initialize the references */ |
---|
48 | this->type = NET_CLIENT; |
---|
49 | this->networkProtocol = new NetworkProtocol(); |
---|
50 | this->connectionMonitor = new ConnectionMonitor(); |
---|
51 | } |
---|
52 | |
---|
53 | NetworkStream::NetworkStream(IPaddress& address) |
---|
54 | { |
---|
55 | this->type = NET_CLIENT; |
---|
56 | this->init(); |
---|
57 | this->networkSockets.push_back(new NetworkSocket(address)); |
---|
58 | this->networkProtocol = new NetworkProtocol(); |
---|
59 | this->connectionMonitor = new ConnectionMonitor(); |
---|
60 | |
---|
61 | Handshake* hs = new Handshake(false); |
---|
62 | this->handshakes.push_back(hs); |
---|
63 | this->connectSynchronizeable(*hs); |
---|
64 | PRINTF(0)("NetworkStream: %s\n", hs->getName()); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | NetworkStream::NetworkStream(unsigned int port) |
---|
69 | { |
---|
70 | this->type = NET_SERVER; |
---|
71 | this->init(); |
---|
72 | this->serverSocket = new ServerSocket(port); |
---|
73 | this->networkProtocol = new NetworkProtocol(); |
---|
74 | this->connectionMonitor = new ConnectionMonitor(); |
---|
75 | this->networkSockets.push_back( NULL ); |
---|
76 | this->handshakes.push_back( NULL ); |
---|
77 | this->bActive = true; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | void NetworkStream::init() |
---|
82 | { |
---|
83 | /* set the class id for the base object */ |
---|
84 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
85 | this->bActive = false; |
---|
86 | this->serverSocket = NULL; |
---|
87 | myHostId = 0; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | NetworkStream::~NetworkStream() |
---|
92 | { |
---|
93 | if ( this->serverSocket ) |
---|
94 | { |
---|
95 | serverSocket->close(); |
---|
96 | delete serverSocket; |
---|
97 | } |
---|
98 | |
---|
99 | for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) |
---|
100 | { |
---|
101 | if ( *i ) |
---|
102 | { |
---|
103 | (*i)->disconnectServer(); |
---|
104 | (*i)->destroy(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++) |
---|
109 | { |
---|
110 | if ( *i ) |
---|
111 | { |
---|
112 | delete (*i); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | delete connectionMonitor; |
---|
117 | delete networkProtocol; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
122 | { |
---|
123 | this->synchronizeables.push_back(&sync); |
---|
124 | |
---|
125 | if( this->networkSockets.size()>0 ) |
---|
126 | this->bActive = true; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void NetworkStream::processData() |
---|
131 | { |
---|
132 | if ( this->type == NET_SERVER ) |
---|
133 | this->updateConnectionList(); |
---|
134 | |
---|
135 | for (int i = 0; i<handshakes.size(); i++) |
---|
136 | { |
---|
137 | if ( handshakes[i] ) |
---|
138 | { |
---|
139 | if ( handshakes[i]->completed() ) |
---|
140 | { |
---|
141 | if ( handshakes[i]->ok() ) |
---|
142 | { |
---|
143 | if ( type != NET_SERVER ) |
---|
144 | { |
---|
145 | NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() ); |
---|
146 | myHostId = NetworkManager::getInstance()->getHostID(); |
---|
147 | } |
---|
148 | PRINT(0)("handshake finished\n"); |
---|
149 | //TODO: replace handshake by entitymanager |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | PRINT(1)("handshake failed!\n"); |
---|
154 | networkSockets[i]->disconnectServer(); |
---|
155 | //TODO: handle error |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | /* DOWNSTREAM */ |
---|
163 | |
---|
164 | int dataLength; |
---|
165 | int reciever; |
---|
166 | Header header; |
---|
167 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
168 | { |
---|
169 | //TODO: remove items from synchronizeables if they dont exist |
---|
170 | if ( (*it)!=NULL && ClassList::exists(*it) && (*it)->getOwner() == myHostId ) |
---|
171 | { |
---|
172 | do { |
---|
173 | reciever = 0; |
---|
174 | dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever); |
---|
175 | |
---|
176 | |
---|
177 | if ( dataLength<=0 ){ |
---|
178 | reciever = 0; |
---|
179 | continue; |
---|
180 | } |
---|
181 | |
---|
182 | dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it))); |
---|
183 | |
---|
184 | if ( dataLength<=0 ) |
---|
185 | continue; |
---|
186 | |
---|
187 | if ( reciever!=0 ) |
---|
188 | { |
---|
189 | if ( networkSockets[reciever] != NULL ) |
---|
190 | { |
---|
191 | PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); |
---|
192 | networkSockets[reciever]->writePacket(downBuffer, dataLength); |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | PRINTF(1)("networkSockets[reciever] == NULL\n"); |
---|
197 | } |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
202 | { |
---|
203 | if ( networkSockets[i] != NULL ) |
---|
204 | { |
---|
205 | PRINTF(0)("write %d bytes to socket %d\n", dataLength, reciever); |
---|
206 | networkSockets[i]->writePacket(downBuffer, dataLength); |
---|
207 | } |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | } while( reciever!=0 ); |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | PRINTF(0)("synchronizeables == NULL"); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | /* UPSTREAM */ |
---|
220 | |
---|
221 | for ( int i = 0; i<networkSockets.size(); i++) |
---|
222 | { |
---|
223 | if ( networkSockets[i] ) |
---|
224 | { |
---|
225 | do { |
---|
226 | dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE); |
---|
227 | |
---|
228 | if ( dataLength<=0 ) |
---|
229 | continue; |
---|
230 | |
---|
231 | PRINTF(0)("read %d bytes from socket\n", dataLength); |
---|
232 | header = networkProtocol->extractHeader(upBuffer, dataLength); |
---|
233 | dataLength -= sizeof(header); |
---|
234 | |
---|
235 | if ( dataLength != header.length ) |
---|
236 | { |
---|
237 | PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length); |
---|
238 | continue; |
---|
239 | } |
---|
240 | |
---|
241 | for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++) |
---|
242 | { |
---|
243 | if ( *it && (*it)->getUniqueID()==header.uniqueID ) |
---|
244 | (*it)->writeBytes(upBuffer+sizeof(header), dataLength); |
---|
245 | } |
---|
246 | |
---|
247 | } while ( dataLength>0 ); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | #if 0 |
---|
252 | int dataLength = 0; |
---|
253 | |
---|
254 | /* DOWNSTREAM */ |
---|
255 | printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); |
---|
256 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
257 | /* first of all read the synchronizeable's data: */ |
---|
258 | if( this->isServer()) |
---|
259 | dataLength = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
260 | |
---|
261 | if( dataLength > 0) |
---|
262 | { |
---|
263 | /* send the received data to connectionMonitor */ |
---|
264 | this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); |
---|
265 | |
---|
266 | dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, |
---|
267 | *(this->synchronizeables), 12/* some random number (no real id)*/); |
---|
268 | |
---|
269 | /* pass the data to the network socket */ |
---|
270 | // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); |
---|
271 | /* check if there was an error */ |
---|
272 | if( dataLength == -1) |
---|
273 | { |
---|
274 | PRINTF(0)("Error in writing data to the NetworkSocket\n"); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /* UPSTREAM */ |
---|
280 | dataLength = 0; |
---|
281 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
282 | /* first of all read the next Orxonox Network Header */ |
---|
283 | |
---|
284 | if( this->state == NET_REC_HEADER) |
---|
285 | { |
---|
286 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); |
---|
287 | if( dataLength == sizeof(Header)) |
---|
288 | { |
---|
289 | this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); |
---|
290 | printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", |
---|
291 | this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, |
---|
292 | this->packetHeader.receiverID, this->packetHeader.length); |
---|
293 | /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ |
---|
294 | |
---|
295 | this->state = NET_REC_DATA; |
---|
296 | } |
---|
297 | } |
---|
298 | if( this->state == NET_REC_DATA) |
---|
299 | { |
---|
300 | /* now read the data */ |
---|
301 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); |
---|
302 | /* check if the data is available and process it if so */ |
---|
303 | if( dataLength == this->packetHeader.length) |
---|
304 | { |
---|
305 | printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); |
---|
306 | /* send the received data to connectionMonitor */ |
---|
307 | this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); |
---|
308 | /* now pass the data to the sync object */ |
---|
309 | if( !this->isServer()) |
---|
310 | this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); |
---|
311 | |
---|
312 | this->state = NET_REC_HEADER; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | #endif |
---|
317 | } |
---|
318 | |
---|
319 | void NetworkStream::updateConnectionList( ) |
---|
320 | { |
---|
321 | //check for new connections |
---|
322 | |
---|
323 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
324 | |
---|
325 | if ( tempNetworkSocket ) |
---|
326 | { |
---|
327 | int clientId; |
---|
328 | if ( freeSocketSlots.size() >0 ) |
---|
329 | { |
---|
330 | clientId = freeSocketSlots.back(); |
---|
331 | freeSocketSlots.pop_back(); |
---|
332 | networkSockets[clientId] = tempNetworkSocket; |
---|
333 | handshakes[clientId] = new Handshake(true, clientId); |
---|
334 | } else |
---|
335 | { |
---|
336 | clientId = networkSockets.size(); |
---|
337 | networkSockets.push_back(tempNetworkSocket); |
---|
338 | handshakes.push_back(new Handshake(true, clientId)); |
---|
339 | } |
---|
340 | |
---|
341 | PRINTF(0)("New Client: %d\n", clientId); |
---|
342 | |
---|
343 | this->connectSynchronizeable(*handshakes[clientId]); |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | //check if connections are ok else remove them |
---|
348 | for ( int i = 1; i<networkSockets.size(); i++) |
---|
349 | { |
---|
350 | if ( networkSockets[i] && !networkSockets[i]->isOk() ) |
---|
351 | { |
---|
352 | //TODO: tell EntityManager that this player left the game |
---|
353 | PRINTF(0)("Client is gone: %d\n", i); |
---|
354 | |
---|
355 | //delete networkSockets[i]; |
---|
356 | networkSockets[i]->disconnectServer(); |
---|
357 | networkSockets[i]->destroy(); |
---|
358 | networkSockets[i] = NULL; |
---|
359 | //TODO: delete handshake from synchronizeable list so i can delete it |
---|
360 | delete handshakes[i]; |
---|
361 | handshakes[i] = NULL; |
---|
362 | |
---|
363 | if ( i == networkSockets.size()-1 ) |
---|
364 | { |
---|
365 | networkSockets.pop_back(); |
---|
366 | handshakes.pop_back(); |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | freeSocketSlots.push_back(i); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | } |
---|
377 | |
---|
378 | |
---|