Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5746 was 5746, checked in by bottac, 19 years ago
File size: 3.6 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#define PACKAGE_SIZE  256
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
39NetworkStream::NetworkStream() 
40  : DataStream()
41{
42  this->init();
43  /* initialize the references */
44  this->networkSocket = new NetworkSocket();
45  this->networkProtocol = new NetworkProtocol();
46  this->synchronizeables = NULL;
47  this->connectionMonitor = new ConnectionMonitor();
48}
49
50
51NetworkStream::NetworkStream(IPaddress& address, Synchronizeable& sync, NodeType type) 
52  : DataStream()
53{
54  this->init();
55  this->networkSocket = new NetworkSocket( address, 9999 );
56   // this->networkSocket->connectToServer(address, 88);
57  this->networkProtocol = new NetworkProtocol();
58  this->synchronizeables = &sync;
59  this->connectionMonitor = new ConnectionMonitor();
60}
61
62
63NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type) 
64  : DataStream()
65{
66  this->init();
67  this->networkSocket = new NetworkSocket(/* address, type */);
68  this->networkSocket->listen(9999);
69  this->networkProtocol = new NetworkProtocol();
70  this->synchronizeables = &sync;
71  this->connectionMonitor = new ConnectionMonitor();
72}
73
74
75void NetworkStream::init()
76{
77  /* set the class id for the base object */
78  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
79}
80
81
82NetworkStream::~NetworkStream()
83{
84
85 networkSocket->disconnectServer();
86
87 delete networkSocket;
88 delete connectionMonitor;
89 delete networkProtocol;
90}
91
92void NetworkStream::processData()
93{
94
95 
96  int ret = 0;
97
98  /* DOWNSTREAM */
99
100  /* first of all read the synchronizeable's data: */
101  ret = this->synchronizeables->readBytes((byte*)downBuffer);
102 
103  /* send the received data to connectionMonitor */
104  this->connectionMonitor->processPacket((byte*)downBuffer, PACKAGE_SIZE);
105
106        this->networkProtocol->createHeader((byte*) downBuffer , PACKAGE_SIZE , 1024,*(this->synchronizeables),12);
107       
108  /* pass the data to the network socket */
109  ret = this->networkSocket->writeBytes((byte*)downBuffer, PACKAGE_SIZE + sizeof(Header));
110  /* check if there was an error */
111  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
112 
113
114
115  /* UPSTREAM */
116 
117  ret = 0; 
118
119  /* first of all read  data from networkSocket*/
120  while(ret == 0)
121  ret = this->networkSocket->readBlock((byte*)upBuffer,PACKAGE_SIZE + sizeof(Header));
122  /* error checking: data read? */
123  if( ret != PACKAGE_SIZE) { PRINTF(0)("Error while reading data from the NetworkSocket\n");}
124
125  /* send the received data to connectionMonitor */
126  this->connectionMonitor->processPacket((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header));
127
128  /* extract Header */
129  this->networkProtocol->extractHeader((byte*) upBuffer , PACKAGE_SIZE + sizeof(Header));
130   
131  /* now pass the data to the sync object */
132  this->synchronizeables->writeBytes((byte*)upBuffer[sizeof(Header)], PACKAGE_SIZE);
133
134}
Note: See TracBrowser for help on using the repository browser.