Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5957 was 5822, checked in by bensch, 19 years ago

orxonox/trunk: merged branches/network to the trunk
merged with command:
svn merge -r 5505:HEAD branches/network trunk

conflicts resolved in favor of the trunk (as always :))
also fixed a typo in the #include "SDL_tread.h"

File size: 4.5 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(unsigned int port, Synchronizeable& sync, 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  this->state = NET_REC_HEADER;
81}
82
83
84NetworkStream::~NetworkStream()
85{
86
87 networkSocket->disconnectServer();
88
89 if( this->networkSocket)
90   delete networkSocket;
91
92 delete connectionMonitor;
93 delete networkProtocol;
94}
95
96void NetworkStream::processData()
97{
98  int dataLength = 0;
99
100  /* DOWNSTREAM */
101  printf("\nSynchronizeable: %s\n", this->synchronizeables->getName());
102  PRINT(0)("============= DOWNSTREAM:===============\n");
103  /* first of all read the synchronizeable's data: */
104  dataLength = this->synchronizeables->readBytes((byte*)downBuffer);
105
106  /* send the received data to connectionMonitor */
107  this->connectionMonitor->processPacket((byte*)downBuffer, dataLength);
108
109  dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE,
110      *(this->synchronizeables), 12/* some random number (no real id)*/);
111
112  /* pass the data to the network socket */
113  dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);
114  /* check if there was an error */
115  if( dataLength == -1) { PRINTF(0)("Error in writing data to the NetworkSocket\n");}
116
117
118
119  /* UPSTREAM */
120  dataLength = 0;
121  PRINT(0)("============== UPSTREAM:================\n");
122  /* first of all read the next Orxonox Network Header */
123
124  if( this->state == NET_REC_HEADER)
125  {
126    dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));
127    if( dataLength == sizeof(Header))
128    {
129      this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength);
130      printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n",
131             this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID,
132             this->packetHeader.receiverID, this->packetHeader.length);
133      /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/
134
135      this->state = NET_REC_DATA;
136    }
137  }
138  if( this->state == NET_REC_DATA)
139  {
140    /* now read the data */
141    dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);
142    /* check if the data is available and process it if so */
143    if( dataLength == this->packetHeader.length)
144    {
145      printf("NetworkStream::processData() - Got Data: \n");
146      /* send the received data to connectionMonitor */
147      this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length);
148      /* now pass the data to the sync object */
149      this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length);
150
151      this->state = NET_REC_HEADER;
152    }
153  }
154
155
156}
Note: See TracBrowser for help on using the repository browser.