Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: testing env. got segfault in the code

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
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, 9999 );
58   // this->networkSocket->connectToServer(address, 88);
59  this->networkProtocol = new NetworkProtocol();
60  this->synchronizeables = &sync;
61  this->connectionMonitor = new ConnectionMonitor();
62}
63
64
65NetworkStream::NetworkStream(Synchronizeable& sync, int port, NodeType type) 
66  : DataStream()
67{
68  this->init();
69  this->networkSocket = new NetworkSocket(/* address, type */);
70  this->networkSocket->listen(9999);
71  this->networkProtocol = new NetworkProtocol();
72  this->synchronizeables = &sync;
73  this->connectionMonitor = new ConnectionMonitor();
74}
75
76
77void NetworkStream::init()
78{
79  /* set the class id for the base object */
80  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
81}
82
83
84NetworkStream::~NetworkStream()
85{
86
87 networkSocket->disconnectServer();
88
89 delete networkSocket;
90 delete connectionMonitor;
91 delete networkProtocol;
92}
93
94void NetworkStream::processData()
95{
96
97 
98  int ret = 0;
99
100  /* DOWNSTREAM */
101
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 + sizeof(Header));
107
108  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, PACKAGE_SIZE + sizeof(Header));
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 
120  ret = 0; 
121
122  /* first of all read  data from networkSocket*/
123  ret = this->networkSocket->readBlock((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header));
124  /* error checking: data read? */
125  if( ret != PACKAGE_SIZE + sizeof(Header)) { PRINTF(0)("Error while reading data from the NetworkSocket\n");}
126
127  /* send the received data to connectionMonitor */
128  this->connectionMonitor->processPacket((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header));
129
130  /* extract Header */
131  this->networkProtocol->extractHeader((byte*) upBuffer , PACKAGE_SIZE + sizeof(Header));
132   
133  /* now pass the data to the sync object */
134  this->synchronizeables->writeBytes((byte*)upBuffer, PACKAGE_SIZE);
135
136}
Note: See TracBrowser for help on using the repository browser.