Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: added some simulated network delay - this helps a lot for debugging :D

File size: 3.7 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
52
53NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
54  : DataStream()
55{
56  this->init();
57  this->networkSocket = new NetworkSocket(address);
58  this->networkProtocol = new NetworkProtocol();
59  this->synchronizeables = &sync;
60  this->connectionMonitor = new ConnectionMonitor();
61}
62
63
64NetworkStream::NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type)
65  : DataStream()
66{
67  this->init();
68  this->networkSocket = new NetworkSocket();
69  this->networkSocket->listen(port);
70  this->networkProtocol = new NetworkProtocol();
71  this->synchronizeables = &sync;
72  this->connectionMonitor = new ConnectionMonitor();
73}
74
75
76void NetworkStream::init()
77{
78  /* set the class id for the base object */
79  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
80}
81
82
83NetworkStream::~NetworkStream()
84{
85
86 networkSocket->disconnectServer();
87
88 if( this->networkSocket)
89   delete networkSocket;
90
91 delete connectionMonitor;
92 delete networkProtocol;
93}
94
95void NetworkStream::processData()
96{
97  int ret = 0;
98
99  /* DOWNSTREAM */
100  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
101  PRINT(0)("============= DOWNSTREAM:===============\n");
102  /* first of all read the synchronizeable's data: */
103  ret = this->synchronizeables->readBytes((byte*)downBuffer);
104
105  /* send the received data to connectionMonitor */
106  this->connectionMonitor->processPacket((byte*)downBuffer, ret);
107
108  ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE,
109                                            *(this->synchronizeables), 12);
110
111  /* pass the data to the network socket */
112  ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);
113  /* check if there was an error */
114  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
115
116
117
118  /* UPSTREAM */
119  ret = 0;
120  PRINT(0)("============== UPSTREAM:================\n");
121  /* first of all read  data from networkSocket*/
122  ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */);
123  /* error checking: data read? */
124  if( ret != 11 /*PACKAGE_SIZE + sizeof(Header)*/) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");}
125  else
126  {
127    /* send the received data to connectionMonitor */
128    this->connectionMonitor->processPacket((byte*)upBuffer, ret);
129
130    /* extract Header */
131    Header header = this->networkProtocol->extractHeader((byte*) upBuffer , ret);
132
133    /* now pass the data to the sync object */
134    this->synchronizeables->writeBytes((byte*)upBuffer, header.length);
135  }
136
137}
Note: See TracBrowser for help on using the repository browser.