Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network_stream: added possibility to connect to >1 hosts

File size: 4.9 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}
69
70
71void NetworkStream::init()
72{
73  /* set the class id for the base object */
74  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
75  this->bActive = false;
76  this->serverSocket = NULL;
77}
78
79
80NetworkStream::~NetworkStream()
81{
82  if ( this->serverSocket )
83  {
84    serverSocket->close();
85    delete serverSocket;
86  }
87
88  for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++)
89  {
90    if ( *i )
91    {
92      (*i)->disconnectServer();
93      delete *i;
94    }
95  }
96
97  delete connectionMonitor;
98  delete networkProtocol;
99}
100
101
102void NetworkStream::connectSynchronizeable(Synchronizeable& sync)
103{
104  this->synchronizeables.push_back(&sync);
105
106  if( this->networkSockets.size()>0 )
107    this->bActive = true;
108}
109
110
111void NetworkStream::processData()
112{
113#if 0
114  int dataLength = 0;
115
116  /* DOWNSTREAM */
117  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
118  PRINT(0)("============= DOWNSTREAM:===============\n");
119  /* first of all read the synchronizeable's data: */
120  if( this->isServer())
121    dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
122
123  if( dataLength > 0)
124  {
125    /* send the received data to connectionMonitor */
126    this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
127
128    dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
129                 *(this->synchronizeables), 12/* some random number (no real id)*/);
130
131    /* pass the data to the network socket */
132 //   dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
133    /* check if there was an error */
134    if( dataLength == -1)
135    {
136      PRINTF(0)("Error in writing data to the NetworkSocket\n");
137    }
138  }
139
140
141  /* UPSTREAM */
142  dataLength = 0;
143  PRINT(0)("============== UPSTREAM:================\n");
144  /* first of all read the next Orxonox Network Header */
145
146  if( this->state == NET_REC_HEADER)
147  {
148//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
149    if( dataLength == sizeof(Header))
150    {
151      this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
152      printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
153             this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
154             this->packetHeader.receiverID, this->packetHeader.length);
155      /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
156
157      this->state = NET_REC_DATA;
158    }
159  }
160  if( this->state == NET_REC_DATA)
161  {
162    /* now read the data */
163//    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
164    /* check if the data is available and process it if so */
165    if( dataLength == this->packetHeader.length)
166    {
167      printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength);
168      /* send the received data to connectionMonitor */
169      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
170      /* now pass the data to the sync object */
171      if( !this->isServer())
172        this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
173
174      this->state = NET_REC_HEADER;
175    }
176  }
177
178#endif
179}
Note: See TracBrowser for help on using the repository browser.