Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5829 was 5829, checked in by patrick, 18 years ago

network: much work on multiplayability, does not yet work

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