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: Patrick Boenzli (patrick@orxonox.ethz.ch) |
---|
13 | co-programmer: Christoph Renner (rennerc@ee.ethz.ch) |
---|
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 | #include "class_list.h" |
---|
23 | #include "debug.h" |
---|
24 | #include "shell_command.h" |
---|
25 | |
---|
26 | /* include your own header */ |
---|
27 | #include "network_manager.h" |
---|
28 | #include "shared_network_data.h" |
---|
29 | #include "network_stream.h" |
---|
30 | #include "preferences.h" |
---|
31 | #include "network_log.h" |
---|
32 | #include "network_game_manager.h" |
---|
33 | #include "proxy/proxy_control.h" |
---|
34 | |
---|
35 | |
---|
36 | /* using namespace std is default, this needs to be here */ |
---|
37 | |
---|
38 | |
---|
39 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
40 | SHELL_COMMAND(redirTest, NetworkManager, setRedirectionTest); |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
45 | |
---|
46 | /** |
---|
47 | * standard constructor |
---|
48 | */ |
---|
49 | NetworkManager::NetworkManager() |
---|
50 | { |
---|
51 | /* set the class id for the base object */ |
---|
52 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
53 | PRINTF(0)("START\n"); |
---|
54 | |
---|
55 | /* initialize the references */ |
---|
56 | this->networkStream = NULL; |
---|
57 | this->elapsedTime = 0.0f; |
---|
58 | |
---|
59 | |
---|
60 | int port = Preferences::getInstance()->getInt( "network", "telnetport", 0 ); |
---|
61 | |
---|
62 | if ( port > 0 ) |
---|
63 | NetworkLog::getInstance()->listen( port ); |
---|
64 | |
---|
65 | PRINTF(0)("NetworkManager created\n"); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * standard deconstructor |
---|
71 | */ |
---|
72 | NetworkManager::~NetworkManager() |
---|
73 | { |
---|
74 | PRINTF(0)("NetworkManager destructor\n"); |
---|
75 | if ( this->networkStream ) |
---|
76 | { |
---|
77 | delete this->networkStream; |
---|
78 | this->networkStream = NULL; |
---|
79 | } |
---|
80 | |
---|
81 | NetworkManager::singletonRef = NULL; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | /** |
---|
86 | * initializes the network manager |
---|
87 | */ |
---|
88 | void NetworkManager::initialize() |
---|
89 | { |
---|
90 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * shutsdown the network manager |
---|
96 | */ |
---|
97 | void NetworkManager::shutdown() |
---|
98 | { |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * creates a new NetworkStream of server type |
---|
106 | * @param clientPort: number of the TCP/UDP port for client connections |
---|
107 | * @param proxyPort: number of the TCP/UDP port for proxy connections |
---|
108 | */ |
---|
109 | int NetworkManager::createMasterServer(unsigned int port) |
---|
110 | { |
---|
111 | // load the network settings |
---|
112 | NetworkSettings::getInstance()->loadData(); |
---|
113 | |
---|
114 | // create the network stream |
---|
115 | this->networkStream = new NetworkStream(NET_MASTER_SERVER); |
---|
116 | this->networkStream->createServer( port, port + 1, port + 2); |
---|
117 | |
---|
118 | // start the network game manager |
---|
119 | this->networkStream->createNetworkGameManager(); |
---|
120 | |
---|
121 | // init the proxy control center |
---|
122 | ProxyControl::getInstance(); |
---|
123 | |
---|
124 | PRINTF(0)("Created Network Master Server\n"); |
---|
125 | SDL_Delay(20); |
---|
126 | return 1; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * creates a new network stream of proxy server type |
---|
131 | * @param port: number of the TCP port |
---|
132 | */ |
---|
133 | int NetworkManager::createProxyServer(unsigned int port) |
---|
134 | { |
---|
135 | // load the network settings |
---|
136 | NetworkSettings::getInstance()->loadData(); |
---|
137 | |
---|
138 | // create the network stream af |
---|
139 | this->networkStream = new NetworkStream(NET_PROXY_SERVER_ACTIVE ); |
---|
140 | // first connect to the master server for synchronization |
---|
141 | this->networkStream->connectToMasterServer(NetworkSettings::getInstance()->getMasterAddr().ipString(), 10000); |
---|
142 | // start the handshake with the master server |
---|
143 | this->networkStream->startHandshake(NET_ID_MASTER_SERVER); |
---|
144 | |
---|
145 | // then start the server |
---|
146 | this->networkStream->createServer( port, port + 1, port + 2); |
---|
147 | |
---|
148 | // and to the other proxy servers also, this would be very nice if its works |
---|
149 | /* put it here....*/ |
---|
150 | |
---|
151 | // init the proxy control center |
---|
152 | ProxyControl::getInstance(); |
---|
153 | |
---|
154 | PRINTF(0)("Created Network Proxy Server\n"); |
---|
155 | SDL_Delay(20); |
---|
156 | return 1; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * creates a connection from one object to a host |
---|
162 | * @param hostName: the name of the destination host |
---|
163 | */ |
---|
164 | int NetworkManager::createClient(const std::string & name, unsigned int port) |
---|
165 | { |
---|
166 | // load the network settings |
---|
167 | NetworkSettings::getInstance()->loadData(); |
---|
168 | |
---|
169 | // create the network stream |
---|
170 | this->networkStream = new NetworkStream(NET_CLIENT); |
---|
171 | // connect to the master server, if a redirection should occure, this is handled in the NetworkStream itself |
---|
172 | this->networkStream->connectToMasterServer( name, port); |
---|
173 | |
---|
174 | |
---|
175 | // and start the handshake |
---|
176 | this->networkStream->startHandshake(); |
---|
177 | // create the proxy control |
---|
178 | ProxyControl::getInstance(); |
---|
179 | |
---|
180 | PRINTF(0)("Created Network Client\n"); |
---|
181 | return 1; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | /** |
---|
186 | * reconnects this client to another server |
---|
187 | * @param address new server address |
---|
188 | */ |
---|
189 | void NetworkManager::reconnectToServer(IP address) |
---|
190 | { |
---|
191 | PRINTF(0)("Rec. reconnection command\n"); |
---|
192 | this->networkStream->reconnectToServer(address); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /** |
---|
197 | * connects a synchronizeable to the network stream |
---|
198 | * @param sync: synchronizeable to connect |
---|
199 | */ |
---|
200 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
201 | { |
---|
202 | if( this->networkStream) |
---|
203 | this->networkStream->connectSynchronizeable(sync); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /** |
---|
208 | * sync the network |
---|
209 | * @param dtS: the seceonds elapsed since the last synchronize call |
---|
210 | */ |
---|
211 | void NetworkManager::synchronize( float dtS) |
---|
212 | { |
---|
213 | this->elapsedTime += dtS; |
---|
214 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
---|
215 | return; |
---|
216 | |
---|
217 | this->elapsedTime = 0.0f; |
---|
218 | |
---|
219 | if ( this->networkStream ) |
---|
220 | networkStream->processData(); |
---|
221 | |
---|
222 | NetworkGameManager::getInstance()->tick( this->elapsedTime ); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * debug output |
---|
229 | */ |
---|
230 | void NetworkManager::debug() |
---|
231 | { |
---|
232 | PRINT(0)("=================Network::debug()=========\n"); |
---|
233 | this->networkStream->debug(); |
---|
234 | PRINT(0)("===========================================\n"); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | void NetworkManager::setRedirectionTest() |
---|
239 | { |
---|
240 | this->networkStream->setRedirectionTest(); |
---|
241 | } |
---|
242 | |
---|