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->type = NET_CLIENT; |
---|
47 | this->networkProtocol = new NetworkProtocol(); |
---|
48 | this->connectionMonitor = new ConnectionMonitor(); |
---|
49 | } |
---|
50 | |
---|
51 | NetworkStream::NetworkStream(IPaddress& address) |
---|
52 | { |
---|
53 | this->type = NET_CLIENT; |
---|
54 | this->init(); |
---|
55 | this->networkSockets.push_back(new NetworkSocket(address)); |
---|
56 | this->networkProtocol = new NetworkProtocol(); |
---|
57 | this->connectionMonitor = new ConnectionMonitor(); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | NetworkStream::NetworkStream(unsigned int port) |
---|
62 | { |
---|
63 | this->type = NET_SERVER; |
---|
64 | this->init(); |
---|
65 | this->serverSocket = new ServerSocket(port); |
---|
66 | this->networkProtocol = new NetworkProtocol(); |
---|
67 | this->connectionMonitor = new ConnectionMonitor(); |
---|
68 | this->networkSockets.push_back( NULL ); |
---|
69 | this->bActive = true; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | void NetworkStream::init() |
---|
74 | { |
---|
75 | /* set the class id for the base object */ |
---|
76 | this->setClassID(CL_NETWORK_STREAM, "NetworkStream"); |
---|
77 | this->bActive = false; |
---|
78 | this->serverSocket = NULL; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | NetworkStream::~NetworkStream() |
---|
83 | { |
---|
84 | if ( this->serverSocket ) |
---|
85 | { |
---|
86 | serverSocket->close(); |
---|
87 | delete serverSocket; |
---|
88 | } |
---|
89 | |
---|
90 | for (NetworkSocketVector::iterator i = networkSockets.begin(); i!=networkSockets.end(); i++) |
---|
91 | { |
---|
92 | if ( *i ) |
---|
93 | { |
---|
94 | (*i)->disconnectServer(); |
---|
95 | (*i)->destroy(); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | delete connectionMonitor; |
---|
100 | delete networkProtocol; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void NetworkStream::connectSynchronizeable(Synchronizeable& sync) |
---|
105 | { |
---|
106 | this->synchronizeables.push_back(&sync); |
---|
107 | |
---|
108 | if( this->networkSockets.size()>0 ) |
---|
109 | this->bActive = true; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void NetworkStream::processData() |
---|
114 | { |
---|
115 | //PRINTF(0)("processData()\n"); |
---|
116 | if ( this->type == NET_SERVER ) |
---|
117 | this->updateConnectionList(); |
---|
118 | |
---|
119 | #if 0 |
---|
120 | int dataLength = 0; |
---|
121 | |
---|
122 | /* DOWNSTREAM */ |
---|
123 | printf("\nSynchronizeable: %s\n", this->synchronizeables->getName()); |
---|
124 | PRINT(0)("============= DOWNSTREAM:===============\n"); |
---|
125 | /* first of all read the synchronizeable's data: */ |
---|
126 | if( this->isServer()) |
---|
127 | dataLength = this->synchronizeables->readBytes((byte*)downBuffer); |
---|
128 | |
---|
129 | if( dataLength > 0) |
---|
130 | { |
---|
131 | /* send the received data to connectionMonitor */ |
---|
132 | this->connectionMonitor->processPacket((byte*)downBuffer, dataLength); |
---|
133 | |
---|
134 | dataLength = this->networkProtocol->createHeader((byte*)downBuffer, dataLength, DATA_STREAM_BUFFER_SIZE, |
---|
135 | *(this->synchronizeables), 12/* some random number (no real id)*/); |
---|
136 | |
---|
137 | /* pass the data to the network socket */ |
---|
138 | // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); |
---|
139 | /* check if there was an error */ |
---|
140 | if( dataLength == -1) |
---|
141 | { |
---|
142 | PRINTF(0)("Error in writing data to the NetworkSocket\n"); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /* UPSTREAM */ |
---|
148 | dataLength = 0; |
---|
149 | PRINT(0)("============== UPSTREAM:================\n"); |
---|
150 | /* first of all read the next Orxonox Network Header */ |
---|
151 | |
---|
152 | if( this->state == NET_REC_HEADER) |
---|
153 | { |
---|
154 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); |
---|
155 | if( dataLength == sizeof(Header)) |
---|
156 | { |
---|
157 | this->packetHeader = this->networkProtocol->extractHeader((byte*) upBuffer , dataLength); |
---|
158 | printf("NetworkStream::processData() - Got Header: Protocol %u, Version: %u, Sender: %u, Receiver: %u, Length: %u\n", |
---|
159 | this->packetHeader.protocol, this->packetHeader.version, this->packetHeader.senderID, |
---|
160 | this->packetHeader.receiverID, this->packetHeader.length); |
---|
161 | /* FIXME: what if it was no this->packetHeader? catch? eg: the protocol identifier, receiver id*/ |
---|
162 | |
---|
163 | this->state = NET_REC_DATA; |
---|
164 | } |
---|
165 | } |
---|
166 | if( this->state == NET_REC_DATA) |
---|
167 | { |
---|
168 | /* now read the data */ |
---|
169 | // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); |
---|
170 | /* check if the data is available and process it if so */ |
---|
171 | if( dataLength == this->packetHeader.length) |
---|
172 | { |
---|
173 | printf("NetworkStream::processData() - Got Data: %i bytes\n", dataLength); |
---|
174 | /* send the received data to connectionMonitor */ |
---|
175 | this->connectionMonitor->processPacket((byte*)upBuffer, this->packetHeader.length); |
---|
176 | /* now pass the data to the sync object */ |
---|
177 | if( !this->isServer()) |
---|
178 | this->synchronizeables->writeBytes((byte*)upBuffer, this->packetHeader.length); |
---|
179 | |
---|
180 | this->state = NET_REC_HEADER; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | #endif |
---|
185 | } |
---|
186 | |
---|
187 | void NetworkStream::updateConnectionList( ) |
---|
188 | { |
---|
189 | //check for new connections |
---|
190 | |
---|
191 | NetworkSocket* tempNetworkSocket = serverSocket->getNewSocket(); |
---|
192 | |
---|
193 | if ( tempNetworkSocket ) |
---|
194 | { |
---|
195 | int clientId; |
---|
196 | if ( freeSocketSlots.size() >0 ) |
---|
197 | { |
---|
198 | clientId = freeSocketSlots.back(); |
---|
199 | freeSocketSlots.pop_back(); |
---|
200 | networkSockets[clientId] = tempNetworkSocket; |
---|
201 | } else |
---|
202 | { |
---|
203 | clientId = networkSockets.size(); |
---|
204 | networkSockets.push_back(tempNetworkSocket); |
---|
205 | } |
---|
206 | |
---|
207 | PRINTF(0)("New Client: %d\n", clientId); |
---|
208 | //TODO: start handshake |
---|
209 | //new Handshake(true, clientId); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | //check if connections are ok else remove them |
---|
214 | for ( int i = 1; i<networkSockets.size(); i++) |
---|
215 | { |
---|
216 | if ( networkSockets[i] && !networkSockets[i]->isOk() ) |
---|
217 | { |
---|
218 | //TODO: tell EntityManager that this player left the game |
---|
219 | PRINTF(0)("Client is gone: %d\n", i); |
---|
220 | |
---|
221 | //delete networkSockets[i]; |
---|
222 | networkSockets[i]->destroy(); |
---|
223 | networkSockets[i] = NULL; |
---|
224 | |
---|
225 | if ( i == networkSockets.size()-1 ) |
---|
226 | { |
---|
227 | networkSockets.pop_back(); |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | freeSocketSlots.push_back(i); |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | } |
---|