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