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