Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5741 was 5741, checked in by bottac, 18 years ago
File size: 3.4 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, 88 );
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->networkProtocol = new NetworkProtocol();
69  this->synchronizeables = &sync;
70  this->connectionMonitor = new ConnectionMonitor();
71}
72
73
74void NetworkStream::init()
75{
76  /* set the class id for the base object */
77  this->setClassID(CL_NETWORK_STREAM, "NetworkStream");
78}
79
80
81NetworkStream::~NetworkStream()
82{
83
84 networkSocket->disconnectServer();
85
86 delete networkSocket;
87 delete connectionMonitor;
88
89}
90
91void NetworkStream::processData()
92{
93
94 
95  int ret = 0;
96
97  /* DOWNSTREAM */
98
99  /* first of all read the synchronizeable's data: */
100  ret = this->synchronizeables->readBytes((byte*)downBuffer);
101 
102  /* send the received data to connectionMonitor */
103  this->connectionMonitor->processPacket((byte*)downBuffer, PACKAGE_SIZE);
104
105        this->networkProtocol->createHeader((byte*) downBuffer , PACKAGE_SIZE, 1024,*(this->synchronizeables),12);
106       
107  /* pass the data to the network socket */
108  ret = this->networkSocket->writeBytes((byte*)downBuffer, PACKAGE_SIZE);
109  /* check if there was an error */
110  if( ret == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
111 
112
113
114  /* UPSTREAM */
115 
116  ret = 0; 
117
118  while(ret == 0)  ret = this->networkSocket->readBlock((byte*)upBuffer,PACKAGE_SIZE);
119  /* error checking: data read? */
120  if( ret != PACKAGE_SIZE) { PRINTF(0)("Error while reading data from the NetworkSocket\n");}
121
122  /* send the received data to connectionMonitor */
123  this->connectionMonitor->processPacket((byte*)upBuffer, PACKAGE_SIZE);
124
125  /* extract Header */
126  this->networkProtocol->extractHeader((byte*) upBuffer , PACKAGE_SIZE);
127   
128  /* now pass the data to the sync object */
129  this->synchronizeables->writeBytes((byte*)upBuffer[sizeof(Header)], PACKAGE_SIZE);
130
131}
Note: See TracBrowser for help on using the repository browser.