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
RevLine 
[5566]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:
[5601]12   main-programmer: claudio
[5800]13   co-programmer:
[5566]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
[5747]22
[5647]23#include "base_object.h"
[5731]24#include "network_protocol.h"
[5647]25#include "network_socket.h"
26#include "connection_monitor.h"
27#include "synchronizeable.h"
28#include "list.h"
[5649]29#include "debug.h"
[5647]30
[5566]31/* include your own header */
32#include "network_stream.h"
33
[5595]34/* probably unnecessary */
[5594]35using namespace std;
36
[5595]37
[5747]38#define PACKAGE_SIZE  256
[5647]39
[5747]40
[5800]41NetworkStream::NetworkStream()
[5649]42  : DataStream()
[5647]43{
44  this->init();
[5648]45  /* initialize the references */
46  this->networkSocket = new NetworkSocket();
[5741]47  this->networkProtocol = new NetworkProtocol();
[5723]48  this->synchronizeables = NULL;
[5648]49  this->connectionMonitor = new ConnectionMonitor();
[5647]50}
51
52
[5800]53NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type)
[5727]54  : DataStream()
[5647]55{
56  this->init();
[5804]57  this->networkSocket = new NetworkSocket(address);
[5741]58  this->networkProtocol = new NetworkProtocol();
[5723]59  this->synchronizeables = &sync;
[5741]60  this->connectionMonitor = new ConnectionMonitor();
[5647]61}
[5607]62
[5610]63
[5804]64NetworkStream::NetworkStream(Synchronizeable& sync, unsigned int port, NodeType type)
[5727]65  : DataStream()
[5649]66{
67  this->init();
[5804]68  this->networkSocket = new NetworkSocket();
69  this->networkSocket->listen(port);
[5741]70  this->networkProtocol = new NetworkProtocol();
[5723]71  this->synchronizeables = &sync;
[5741]72  this->connectionMonitor = new ConnectionMonitor();
[5649]73}
74
75
[5647]76void NetworkStream::init()
77{
78  /* set the class id for the base object */
79  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
[5594]80}
81
[5647]82
[5566]83NetworkStream::~NetworkStream()
[5598]84{
[5723]85
86 networkSocket->disconnectServer();
87
[5805]88 if( this->networkSocket)
89   delete networkSocket;
90
[5608]91 delete connectionMonitor;
[5743]92 delete networkProtocol;
[5598]93}
94
[5604]95void NetworkStream::processData()
96{
[5615]97  int ret = 0;
[5741]98
[5650]99  /* DOWNSTREAM */
[5804]100  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
[5800]101  PRINT(0)("============= DOWNSTREAM:===============\n");
[5650]102  /* first of all read the synchronizeable's data: */
[5741]103  ret = this->synchronizeables->readBytes((byte*)downBuffer);
[5800]104
[5741]105  /* send the received data to connectionMonitor */
[5802]106  this->connectionMonitor->processPacket((byte*)downBuffer, ret);
[5730]107
[5802]108  ret = this->networkProtocol->createHeader((byte*)downBuffer, ret, DATA_STREAM_BUFFER_SIZE,
109                                            *(this->synchronizeables), 12);
[5800]110
[5650]111  /* pass the data to the network socket */
[5802]112  ret = this->networkSocket->writeBytes((byte*)downBuffer, ret);
[5650]113  /* check if there was an error */
114  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
[5741]115
116
[5800]117
[5741]118  /* UPSTREAM */
[5800]119  ret = 0;
120  PRINT(0)("============== UPSTREAM:================\n");
[5742]121  /* first of all read  data from networkSocket*/
[5802]122  ret = this->networkSocket->readBlock((byte*)upBuffer, 11/* this is very bad: hard coded packet sizes! */);
[5650]123  /* error checking: data read? */
[5804]124  if( ret != 11 /*PACKAGE_SIZE + sizeof(Header)*/) { PRINTF(0)("Error while reading data from the NetworkSocket. Skipping further work\n");}
[5802]125  else
126  {
127    /* send the received data to connectionMonitor */
128    this->connectionMonitor->processPacket((byte*)upBuffer, ret);
[5730]129
[5802]130    /* extract Header */
[5804]131    Header header = this->networkProtocol->extractHeader((byte*) upBuffer , ret);
[5730]132
[5802]133    /* now pass the data to the sync object */
[5804]134    this->synchronizeables->writeBytes((byte*)upBuffer, header.length);
[5802]135  }
[5800]136
[5604]137}
Note: See TracBrowser for help on using the repository browser.