Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

NetworkStream should now accept net connections

File size: 6.1 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 "list.h"
29#include "debug.h"
30
31/* include your own header */
32#include "network_stream.h"
33
34/* probably unnecessary */
35using namespace std;
36
37
38#define PACKAGE_SIZE  256
39
40
41NetworkStream::NetworkStream()
42    : DataStream()
43{
44  this->init();
45  /* initialize the references */
46  this->type = NET_CLIENT;
47  this->networkProtocol = new NetworkProtocol();
48  this->connectionMonitor = new ConnectionMonitor();
49}
50
51NetworkStream::NetworkStream(IPaddress& address)
52{
53  this->type = NET_CLIENT;
54  this->init();
55  this->networkSockets.push_back(new NetworkSocket(address));
56  this->networkProtocol = new NetworkProtocol();
57  this->connectionMonitor = new ConnectionMonitor();
58}
59
60
61NetworkStream::NetworkStream(unsigned int port)
62{
63  this->type = NET_SERVER;
64  this->init();
65  this->serverSocket = new ServerSocket(port);
66  this->networkProtocol = new NetworkProtocol();
67  this->connectionMonitor = new ConnectionMonitor();
68  this->networkSockets.push_back( NULL );
69  this->bActive = true;
70}
71
72
73void NetworkStream::init()
74{
75  /* set the class id for the base object */
76  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
77  this->bActive = false;
78  this->serverSocket = NULL;
79}
80
81
82NetworkStream::~NetworkStream()
83{
84  if ( this->serverSocket )
85  {
86    serverSocket->close();
87    delete serverSocket;
88  }
89
90  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
91  {
92    if ( *i )
93    {
94      (*i)->disconnectServer();
95      delete *i;
96    }
97  }
98
99  delete connectionMonitor;
100  delete networkProtocol;
101}
102
103
104void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
105{
106  this->synchronizeables.push_back(&sync);
107
108  if( this->networkSockets.size()>0 )
109    this->bActive = true;
110}
111
112
113void NetworkStream::processData()
114{
115  printf("processData()");
116  if ( this->type == NET_SERVER )
117    this->updateConnectionList();
118
119#if 0
120  int dataLength = 0;
121
122  /* DOWNSTREAM */
123  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
124  PRINT(0)("============= DOWNSTREAM:===============\n");
125  /* first of all read the synchronizeable's data: */
126  if( this->isServer())
127    dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
128
129  if( dataLength > 0)
130  {
131    /* send the received data to connectionMonitor */
132    this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
133
134    dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
135                 *(this->synchronizeables), 12/* some random number (no real id)*/);
136
137    /* pass the data to the network socket */
138 //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
139    /* check if there was an error */
140    if( dataLength == -1)
141    {
142      PRINTF(0)("Error in writing data to the NetworkSocket\n");
143    }
144  }
145
146
147  /* UPSTREAM */
148  dataLength = 0;
149  PRINT(0)("============== UPSTREAM:================\n");
150  /* first of all read the next Orxonox Network Header */
151
152  if( this->state == NET_REC_HEADER)
153  {
154//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
155    if( dataLength == sizeof(Header))
156    {
157      this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
158      printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
159             this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
160             this->packetHeader.receiverID, this->packetHeader.length);
161      /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
162
163      this->state = NET_REC_DATA;
164    }
165  }
166  if( this->state == NET_REC_DATA)
167  {
168    /* now read the data */
169//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
170    /* check if the data is available and process it if so */
171    if( dataLength == this->packetHeader.length)
172    {
173      printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
174      /* send the received data to connectionMonitor */
175      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
176      /* now pass the data to the sync object */
177      if( !this->isServer())
178        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
179
180      this->state = NET_REC_HEADER;
181    }
182  }
183
184#endif
185}
186
187void NetworkStream::updateConnectionList( )
188{
189  //check for new connections
190  NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket();
191
192  if ( tempNetworkSocket )
193  {
194    int clientId;
195    if ( freeSocketSlots.size() >0 )
196    {
197      clientId = freeSocketSlots.back();
198      freeSocketSlots.pop_back();
199      networkSockets[clientId] = tempNetworkSocket;
200    } else
201    {
202      networkSockets.push_back(tempNetworkSocket);
203      clientId = networkSockets.size();
204    }
205
206    PRINTF(0)("New Client: %s", clientId);
207    //TODO: start handshake
208    //new Handshake(true, clientId);
209  }
210
211
212  //check if connections are ok else remove them
213  for ( int i = 1; i<networkSockets.size(); i++)
214  {
215    if ( networkSockets[i] && !networkSockets[i]->isOk() )
216    {
217      //TODO: tell EntityManager that this player left the game
218
219      delete networkSockets[i];
220      networkSockets[i] = NULL;
221
222      if ( i == networkSockets.size()-1 )
223      {
224        networkSockets.pop_back();
225      }
226      else
227      {
228        freeSocketSlots.push_back(i);
229      }
230    }
231  }
232
233}
Note: See TracBrowser for help on using the repository browser.