Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6220 was 6220, checked in by rennerc, 18 years ago

network_stream: server now sends the entity list to clients

File size: 10.0 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#include "list.h"
31#include "debug.h"
32#include "class_list.h"
33
34/* include your own header */
35#include "network_stream.h"
36
37/* probably unnecessary */
38using namespace std;
39
40
41#define PACKAGE_SIZE  256
42
43
44NetworkStream::NetworkStream()
45    : DataStream()
46{
47  this->init();
48  /* initialize the references */
49  this->type = NET_CLIENT;
50  this->networkProtocol = new NetworkProtocol();
51  this->connectionMonitor = new ConnectionMonitor();
52}
53
54NetworkStream::NetworkStream(IPaddress& address)
55{
56  this->type = NET_CLIENT;
57  this->init();
58  this->networkSockets.push_back(new NetworkSocket(address));
59  this->networkProtocol = new NetworkProtocol();
60  this->connectionMonitor = new ConnectionMonitor();
61
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());
67}
68
69
70NetworkStream::NetworkStream(unsigned int port)
71{
72  this->type = NET_SERVER;
73  this->init();
74  this->serverSocket = new ServerSocket(port);
75  this->networkProtocol = new NetworkProtocol();
76  this->connectionMonitor = new ConnectionMonitor();
77  this->networkSockets.push_back( NULL );
78  this->handshakes.push_back( NULL );
79  this->bActive = true;
80  this->networkGameManager = new NetworkGameManager();
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) );
85
86  this->setMaxConnections( 10 );
87}
88
89
90void NetworkStream::init()
91{
92  /* set the class id for the base object */
93  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
94  this->bActive = false;
95  this->serverSocket = NULL;
96  this->networkGameManager = NULL;
97  myHostId = 0;
98}
99
100
101NetworkStream::~NetworkStream()
102{
103  if ( this->serverSocket )
104  {
105    serverSocket->close();
106    delete serverSocket;
107  }
108
109  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
110  {
111    if ( *i )
112    {
113      (*i)->disconnectServer();
114      (*i)->destroy();
115    }
116  }
117
118  for (HandshakeVector::iterator i = handshakes.begin(); i!=handshakes.end(); i++)
119  {
120    if ( *i )
121    {
122      delete (*i);
123    }
124  }
125
126  delete connectionMonitor;
127  delete networkProtocol;
128}
129
130
131void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
132{
133  this->synchronizeables.push_back(&sync);
134  sync.setNetworkStream( this );
135
136  if( this->networkSockets.size()>0 )
137    this->bActive = true;
138}
139
140void NetworkStream::disconnectSynchronizeable(Synchronizeable& sync)
141{
142  this->synchronizeables.remove(&sync);
143
144  if( this->networkSockets.size()<=0 )
145    this->bActive = false;
146}
147
148
149void NetworkStream::processData()
150{
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");
158
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();
182
183            this->networkGameManager = new NetworkGameManager();
184            this->networkGameManager->setUniqueID( handshakes[i]->getNetworkGameManagerId() );
185            this->connectSynchronizeable( *(this->networkGameManager) );
186          }
187          else
188          {
189            this->networkGameManager->sendEntityList( i );
190          }
191          PRINT(0)("handshake finished\n");
192
193
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
210  /* DOWNSTREAM */
211
212  int dataLength;
213  int reciever;
214  Header header;
215  for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
216  {
217    if ( (*it)!=NULL && (*it)->getOwner() == myHostId )
218    {
219      do {
220        reciever = 0;
221        dataLength = (*it)->readBytes(downBuffer, DATA_STREAM_BUFFER_SIZE, &reciever);
222
223
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;
232        if ( header->synchronizeableID < this->maxConnections )
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    }
264    else
265    {
266      PRINTF(0)("synchronizeables == NULL");
267    }
268  }
269
270  /* UPSTREAM */
271
272  for ( int i = 0; i<networkSockets.size(); i++)
273  {
274    if ( networkSockets[i] )
275    {
276      do {
277        dataLength = networkSockets[i]->readPacket(upBuffer, DATA_STREAM_BUFFER_SIZE);
278
279        if ( dataLength<=0 )
280          continue;
281
282        PRINTF(5)("read %d bytes from socket\n", dataLength);
283        header = networkProtocol->extractHeader(upBuffer, dataLength);
284        dataLength -= sizeof(header);
285
286        if ( dataLength != header.length )
287        {
288          PRINTF(1)("packetsize in header and real packetsize do not match! %d:%d\n", dataLength, header.length);
289          continue;
290        }
291
292        if ( header.synchronizeableID == 0 )
293        {
294          header.synchronizeableID = i;
295        }
296
297        for (SynchronizeableList::iterator it = synchronizeables.begin(); it!=synchronizeables.end(); it++)
298        {
299          if ( *it && (*it)->getUniqueID()==header.synchronizeableID )
300            (*it)->writeBytes(upBuffer+sizeof(header), dataLength, i);
301        }
302
303      } while ( dataLength>0 );
304    }
305  }
306}
307
308void NetworkStream::updateConnectionList( )
309{
310  //check for new connections
311
312  NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket();
313
314  if ( tempNetworkSocket )
315  {
316    int clientId;
317    if ( freeSocketSlots.size() >0 )
318    {
319      clientId = freeSocketSlots.back();
320      freeSocketSlots.pop_back();
321      networkSockets[clientId] = tempNetworkSocket;
322      handshakes[clientId] = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
323      handshakes[clientId]->setUniqueID(clientId);
324    } else
325    {
326      clientId = networkSockets.size();
327      networkSockets.push_back(tempNetworkSocket);
328      Handshake* tHs = new Handshake(true, clientId, this->networkGameManager->getUniqueID());
329      tHs->setUniqueID(clientId);
330      handshakes.push_back(tHs);
331    }
332
333    if ( clientId > this->maxConnections )
334    {
335      handshakes[clientId]->doReject();
336      PRINTF(0)("Will reject client %d because there are to many connections!\n", clientId);
337    }
338    else
339
340    PRINTF(0)("New Client: %d\n", clientId);
341
342    this->connectSynchronizeable(*handshakes[clientId]);
343  }
344
345
346  //check if connections are ok else remove them
347  for ( int i = 1; i<networkSockets.size(); i++)
348  {
349    if ( networkSockets[i] && !networkSockets[i]->isOk() )
350    {
351      //TODO: tell EntityManager that this player left the game
352      PRINTF(0)("Client is gone: %d\n", i);
353
354      //delete networkSockets[i];
355      networkSockets[i]->disconnectServer();
356      networkSockets[i]->destroy();
357      networkSockets[i] = NULL;
358      //TODO: delete handshake from synchronizeable list so i can delete it
359      if ( handshakes[i] )
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
378void NetworkStream::setMaxConnections( int n )
379{
380  if ( !this->isServer() )
381  {
382    PRINTF(1)("Cannot set maxConnections because I am no server.\n");
383  }
384  if ( this->networkSockets.size() > 1 )
385  {
386    PRINTF(1)("Cannot set maxConnections because there are already %d connections.\n", this->networkSockets.size());
387    return;
388  }
389
390  if ( n > MAX_CONNECTIONS )
391  {
392    PRINTF(1)("Cannot set maxConnectiosn to %d because of hardcoded limit %d\n", n, MAX_CONNECTIONS);
393    return;
394  }
395
396  this->maxConnections = n;
397  this->networkGameManager->setUniqueID( n+2 );
398}
399
400
Note: See TracBrowser for help on using the repository browser.