Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/network/network_stream.cc @ 6722

Last change on this file since 6722 was 6634, checked in by bensch, 19 years ago

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

File size: 10.7 KB
Line 
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 "network_game_manager.h"
30
31#include "debug.h"
32#include "class_list.h"
33#include <algorithm>
34
35/* include your own header */
36#include "network_stream.h"
37
38/* probably unnecessary */
39using namespace std;
40
41
42#define PACKAGE_SIZE  256
43
44
45NetworkStream::NetworkStream()
46    : DataStream()
47{
48  this->init();
49  /* initialize the references */
50  this->type = NET_CLIENT;
51  this->networkProtocol = new NetworkProtocol();
52  this->connectionMonitor = new ConnectionMonitor();
53}
54
55NetworkStream::NetworkStream(IPaddress& address)
56{
57  this->type = NET_CLIENT;
58  this->init();
59  this->networkSockets.push_back(new NetworkSocket(address));
60  this->networkProtocol = new NetworkProtocol();
61  this->connectionMonitor = new ConnectionMonitor();
62
63  Handshake* hs = new Handshake(false);
64  hs->setUniqueID( 0 );
65  this->handshakes.push_back(hs);
66  this->connectSynchronizeable(*hs);
67  PRINTF(0)("NetworkStream: %s\n", hs->getName());
68  this->maxConnections = 1;
69}
70
71
72NetworkStream::NetworkStream(unsigned int port)
73{
74  this->type = NET_SERVER;
75  this->init();
76  this->serverSocket = new ServerSocket(port);
77  this->networkProtocol = new NetworkProtocol();
78  this->connectionMonitor = new ConnectionMonitor();
79  this->networkSockets.push_back( NULL );
80  this->networkSockets[0] = NULL; //TODO: remove this
81  this->handshakes.push_back( NULL );
82  this->bActive = true;
83  this->networkGameManager = NetworkGameManager::getInstance();
84  // setUniqueID( maxCon+2 ) because we need one id for every handshake
85  // and one for handshake to reject client maxCon+1
86  this->networkGameManager->setUniqueID( this->maxConnections+2 );
87  this->connectSynchronizeable( *(this->networkGameManager) );
88
89  this->setMaxConnections( 10 );
90}
91
92
93void NetworkStream::init()
94{
95  /* set the class id for the base object */
96  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
97  this->bActive = false;
98  this->serverSocket = NULL;
99  this->networkGameManager = NULL;
100  myHostId = 0;
101}
102
103
104NetworkStream::~NetworkStream()
105{
106  if ( this->serverSocket )
107  {
108    serverSocket->close();
109    delete serverSocket;
110  }
111
112  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
113  {
114    if ( *i )
115    {
116      (*i)->disconnectServer();
117      (*i)->destroy();
118    }
119  }
120
121  for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
122  {
123    if ( *i )
124    {
125      delete (*i);
126    }
127  }
128
129  delete connectionMonitor;
130  delete networkProtocol;
131}
132
133
134void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
135{
136  this->synchronizeables.push_back(&sync);
137  sync.setNetworkStream( this );
138
139  if( this->networkSockets.size()>0 )
140    this->bActive = true;
141}
142
143void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync)
144{
145  // removing the Synchronizeable from the List.
146  std::list<Synchronizeable*>::iterator disconnectSynchro = std::find(this->synchronizeables.begin(), this->synchronizeables.end(), &sync);
147  if (disconnectSynchro != this->synchronizeables.end())
148    this->synchronizeables.erase(disconnectSynchro);
149
150  if( this->networkSockets.size()<=0 )
151    this->bActive = false;
152}
153
154
155void NetworkStream::processData()
156{
157  if ( this->type == NET_SERVER )
158    this->updateConnectionList();
159  else
160  {
161    if ( networkSockets[0] && !networkSockets[0]->isOk() )
162    {
163      PRINTF(1)("lost connection to server\n");
164
165      //delete networkSockets[i];
166      networkSockets[0]->disconnectServer();
167      networkSockets[0]->destroy();
168      networkSockets[0] = NULL;
169
170      if ( handshakes[0] )
171        delete handshakes[0];
172      handshakes[0] = NULL;
173    }
174  }
175
176  for (int i = 0; i<handshakes.size(); i++)
177  {
178    if ( handshakes[i] )
179    {
180      if ( handshakes[i]->completed() )
181      {
182        if ( handshakes[i]->ok() )
183        {
184          if ( type != NET_SERVER )
185          {
186            NetworkManager::getInstance()->setHostID( handshakes[i]->getHostId() );
187            myHostId = NetworkManager::getInstance()->getHostID();
188
189            this->networkGameManager = NetworkGameManager::getInstance();
190            this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() );
191            this->connectSynchronizeable( *(this->networkGameManager) );
192          }
193          else
194          {
195
196          }
197          PRINT(0)("handshake finished id=%d\n", handshakes[i]->getNetworkGameManagerId());
198
199
200          delete handshakes[i];
201          handshakes[i] = NULL;
202        }
203        else
204        {
205          PRINT(1)("handshake failed!\n");
206          networkSockets[i]->disconnectServer();
207          delete handshakes[i];
208          handshakes[i] = NULL;
209          //TODO: handle error
210        }
211      }
212    }
213  }
214
215
216  /* DOWNSTREAM */
217
218  int dataLength;
219  int reciever;
220  Header header;
221  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
222  {
223    if ( (*it)!=NULL /*&& (*it)->getOwner() == myHostId*/ )
224    {
225      do {
226        reciever = 0;
227        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
228
229
230        if ( dataLength<=0 ){
231          reciever = 0;
232          continue;
233        }
234
235        dataLength = networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, static_cast<const Synchronizeable&>(*(*it)));
236
237        Header* header = (Header*)downBuffer;
238        if ( header->synchronizeableID < this->maxConnections+2 )
239        {
240          //if ( !isServer() ) PRINTF(0)("RESET UNIQUEID FROM %d TO 0 maxCon=%d\n", header->synchronizeableID, this->maxConnections);
241          header->synchronizeableID = 0;
242        }
243        else
244        {
245          //if ( !isServer() ) PRINTF(0)("UNIQUEID=%d\n", header->synchronizeableID);
246        }
247
248        if ( dataLength<=0 )
249          continue;
250
251        if ( reciever!=0 )
252        {
253          if ( networkSockets[reciever] != NULL )
254          {
255            PRINTF(5)("write %d bytes to socket %d\n", dataLength, reciever);
256            networkSockets[reciever]->writePacket(downBuffer, dataLength);
257          }
258          else
259          {
260            PRINTF(1)("networkSockets[reciever] == NULL\n");
261          }
262        }
263        else
264        {
265          for ( int i = 0; i<networkSockets.size(); i++)
266          {
267            if ( networkSockets[i] != NULL )
268            {
269              PRINTF(5)("write %d bytes to socket %d\n", dataLength, i);
270              networkSockets[i]->writePacket(downBuffer, dataLength);
271            }
272          }
273        }
274
275      } while( reciever!=0 );
276    }
277  }
278
279  /* UPSTREAM */
280
281  for ( int i = 0; i<networkSockets.size(); i++)
282  {
283    if ( networkSockets[i] )
284    {
285      do {
286        dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE);
287
288        if ( dataLength<=0 )
289          continue;
290
291        header = networkProtocol->extractHeader(upBuffer, dataLength);
292        dataLength -= sizeof(header);
293
294        PRINTF(5)("read %d bytes from socket uniqueID = %d\n", dataLength, header.synchronizeableID);
295
296        if ( dataLength != header.length )
297        {
298          PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length);
299          continue;
300        }
301
302        if ( header.synchronizeableID == 0 )
303        {
304          header.synchronizeableID = i;
305        }
306
307        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
308        {
309          if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
310          {
311            if ( (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i) != header.length )
312            {
313              PRINTF(1)("%s did not read all the data id = %d!\n", (*it)->getClassName(), (*it)->getUniqueID());
314              break;
315            }
316            continue;
317          }
318        }
319
320      } while ( dataLength>0 );
321    }
322  }
323}
324
325void NetworkStream::updateConnectionList( )
326{
327  //check for new connections
328
329  NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket();
330
331  if ( tempNetworkSocket )
332  {
333    int clientId;
334    if ( freeSocketSlots.size() >0 )
335    {
336      clientId = freeSocketSlots.back();
337      freeSocketSlots.pop_back();
338      networkSockets[clientId] = tempNetworkSocket;
339      handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
340      handshakes[clientId]->setUniqueID(clientId);
341    } else
342    {
343      clientId = networkSockets.size();
344      networkSockets.push_back(tempNetworkSocket);
345      Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
346      tHs->setUniqueID(clientId);
347      handshakes.push_back(tHs);
348    }
349
350    if ( clientId > this->maxConnections )
351    {
352      handshakes[clientId]->doReject();
353      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
354    }
355    else
356
357    PRINTF(0)("New Client: %d\n", clientId);
358
359    this->connectSynchronizeable(*handshakes[clientId]);
360  }
361
362
363  //check if connections are ok else remove them
364  for ( int i = 1; i<networkSockets.size(); i++)
365  {
366    if ( networkSockets[i] && !networkSockets[i]->isOk() )
367    {
368      //TODO: tell EntityManager that this player left the game
369      PRINTF(0)("Client is gone: %d\n", i);
370
371      //delete networkSockets[i];
372      networkSockets[i]->disconnectServer();
373      networkSockets[i]->destroy();
374      networkSockets[i] = NULL;
375
376      if ( handshakes[i] )
377        delete handshakes[i];
378      handshakes[i] = NULL;
379
380      if ( i == networkSockets.size()-1 )
381      {
382        networkSockets.pop_back();
383        handshakes.pop_back();
384      }
385      else
386      {
387        freeSocketSlots.push_back(i);
388      }
389    }
390  }
391
392
393}
394
395void NetworkStream::setMaxConnections( int n )
396{
397  if ( !this->isServer() )
398  {
399    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
400  }
401  if ( this->networkSockets.size() > 1 )
402  {
403    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
404    return;
405  }
406
407  if ( n > MAX_CONNECTIONS )
408  {
409    PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS);
410    return;
411  }
412
413  this->maxConnections = n;
414  this->networkGameManager->setUniqueID( n+2 );
415}
416
417
Note: See TracBrowser for help on using the repository browser.