Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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