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