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 */ |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | |
---|
38 | #define PACKAGE_SIZE 256 |
---|
39 | |
---|
40 | |
---|
41 | NetworkStream::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 | |
---|
53 | NetworkStream::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 | |
---|
65 | NetworkStream::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 | |
---|
77 | void NetworkStream::init() |
---|
78 | { |
---|
79 | /* set the class id for the base object */ |
---|
80 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | NetworkStream::~NetworkStream() |
---|
85 | { |
---|
86 | |
---|
87 | networkSocket->disconnectServer(); |
---|
88 | |
---|
89 | delete networkSocket; |
---|
90 | delete connectionMonitor; |
---|
91 | delete networkProtocol; |
---|
92 | } |
---|
93 | |
---|
94 | void 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 | while(ret == 0) |
---|
124 | ret = this->networkSocket->readBlock((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header)); |
---|
125 | /* error checking: data read? */ |
---|
126 | if( ret != PACKAGE_SIZE + sizeof(Header)) { PRINTF(0)("Error while reading data from the NetworkSocket\n");} |
---|
127 | |
---|
128 | /* send the received data to connectionMonitor */ |
---|
129 | this->connectionMonitor->processPacket((byte*)upBuffer, PACKAGE_SIZE + sizeof(Header)); |
---|
130 | |
---|
131 | /* extract Header */ |
---|
132 | this->networkProtocol->extractHeader((byte*) upBuffer , PACKAGE_SIZE + sizeof(Header)); |
---|
133 | |
---|
134 | /* now pass the data to the sync object */ |
---|
135 | this->synchronizeables->writeBytes((byte*)upBuffer, PACKAGE_SIZE); |
---|
136 | |
---|
137 | } |
---|