[5530] | 1 | /* |
---|
[5520] | 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 |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
[5525] | 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
[5530] | 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
[5525] | 19 | */ |
---|
[5530] | 20 | #define DEBUG_MODULE_NETWORK |
---|
[5525] | 21 | |
---|
[5572] | 22 | #include "class_list.h" |
---|
[5605] | 23 | #include "debug.h" |
---|
[6695] | 24 | #include "shell_command.h" |
---|
[5566] | 25 | |
---|
[5525] | 26 | /* include your own header */ |
---|
[5520] | 27 | #include "network_manager.h" |
---|
[6695] | 28 | #include "shared_network_data.h" |
---|
| 29 | #include "network_stream.h" |
---|
[7954] | 30 | #include "preferences.h" |
---|
| 31 | #include "network_log.h" |
---|
[8147] | 32 | #include "network_game_manager.h" |
---|
[5520] | 33 | |
---|
[5530] | 34 | |
---|
[5525] | 35 | /* using namespace std is default, this needs to be here */ |
---|
[5520] | 36 | using namespace std; |
---|
| 37 | |
---|
[6695] | 38 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
[5520] | 39 | |
---|
[6695] | 40 | |
---|
[5996] | 41 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
[5520] | 42 | |
---|
| 43 | /** |
---|
| 44 | * standard constructor |
---|
| 45 | */ |
---|
| 46 | NetworkManager::NetworkManager() |
---|
[5566] | 47 | { |
---|
[5572] | 48 | /* set the class id for the base object */ |
---|
[5575] | 49 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
[6341] | 50 | PRINTF(0)("START\n"); |
---|
[5804] | 51 | |
---|
[5572] | 52 | /* initialize the references */ |
---|
[8228] | 53 | this->networkStream = NULL; |
---|
[6695] | 54 | this->sharedNetworkData = SharedNetworkData::getInstance(); |
---|
[7954] | 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 ); |
---|
[6341] | 62 | |
---|
[5605] | 63 | PRINTF(0)("NetworkManager created\n"); |
---|
[5566] | 64 | } |
---|
[5520] | 65 | |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * standard deconstructor |
---|
| 69 | */ |
---|
| 70 | NetworkManager::~NetworkManager() |
---|
[8228] | 71 | { |
---|
| 72 | if ( this->networkStream ) |
---|
| 73 | { |
---|
| 74 | delete this->networkStream; |
---|
| 75 | this->networkStream = NULL; |
---|
| 76 | } |
---|
| 77 | } |
---|
[5520] | 78 | |
---|
[5522] | 79 | |
---|
| 80 | /** |
---|
| 81 | * initializes the network manager |
---|
| 82 | */ |
---|
| 83 | void NetworkManager::initialize() |
---|
[5572] | 84 | { |
---|
[5605] | 85 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
[5572] | 86 | } |
---|
[5522] | 87 | |
---|
[5530] | 88 | |
---|
[5522] | 89 | /** |
---|
| 90 | * shutsdown the network manager |
---|
| 91 | */ |
---|
| 92 | void NetworkManager::shutdown() |
---|
[5578] | 93 | { |
---|
[5522] | 94 | |
---|
[5578] | 95 | } |
---|
[5522] | 96 | |
---|
[5578] | 97 | |
---|
[5522] | 98 | /** |
---|
| 99 | * creates a connection from one object to a host |
---|
[5647] | 100 | * @param hostName: the name of the destination host |
---|
| 101 | */ |
---|
[7256] | 102 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
---|
[5996] | 103 | { |
---|
[8228] | 104 | this->networkStream = new NetworkStream( name, port ); |
---|
| 105 | this->sharedNetworkData->setDefaultSyncStream(this->networkStream); |
---|
| 106 | this->networkStream->startHandshake(); |
---|
[5996] | 107 | return 1; |
---|
| 108 | } |
---|
[5647] | 109 | |
---|
[5996] | 110 | |
---|
[5647] | 111 | /** |
---|
[5996] | 112 | * creates a new NetworkStream of server type |
---|
| 113 | * @param port: number of the TCP port |
---|
| 114 | */ |
---|
| 115 | int NetworkManager::createServer(unsigned int port) |
---|
| 116 | { |
---|
[6695] | 117 | this->sharedNetworkData->setHostID(0); |
---|
| 118 | this->sharedNetworkData->setGameServer(true); |
---|
[8228] | 119 | this->networkStream = new NetworkStream(port); |
---|
| 120 | this->sharedNetworkData->setDefaultSyncStream(this->networkStream); |
---|
| 121 | this->networkStream->createNetworkGameManager(); |
---|
[6695] | 122 | PRINTF(0)("CREATE SERVER\n"); |
---|
[5996] | 123 | SDL_Delay(20); |
---|
| 124 | return 1; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
| 129 | { |
---|
[8228] | 130 | if( this->networkStream) |
---|
| 131 | this->networkStream->connectSynchronizeable(sync); |
---|
[5996] | 132 | } |
---|
[5522] | 133 | |
---|
[5996] | 134 | |
---|
[5522] | 135 | /** |
---|
| 136 | * sync the network |
---|
[7954] | 137 | * @param dtS: the seceonds elapsed since the last synchronize call |
---|
[5522] | 138 | */ |
---|
[7954] | 139 | void NetworkManager::synchronize( float dtS) |
---|
[5578] | 140 | { |
---|
[7954] | 141 | this->elapsedTime += dtS; |
---|
| 142 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
---|
| 143 | return; |
---|
| 144 | this->elapsedTime = 0.0f; |
---|
| 145 | |
---|
[8708] | 146 | // if ( networkStream->isActive() ) |
---|
[8228] | 147 | networkStream->processData(); |
---|
[8147] | 148 | |
---|
| 149 | NetworkGameManager::getInstance()->tick( this->elapsedTime ); |
---|
[5578] | 150 | } |
---|
[5997] | 151 | |
---|
[6695] | 152 | |
---|
| 153 | |
---|
[5997] | 154 | /** |
---|
[6695] | 155 | * debug output |
---|
[5997] | 156 | */ |
---|
[6695] | 157 | void NetworkManager::debug() |
---|
[5997] | 158 | { |
---|
[6695] | 159 | PRINT(0)("=================Network::debug()=========\n"); |
---|
[8228] | 160 | this->networkStream->debug(); |
---|
[6695] | 161 | PRINT(0)("===========================================\n"); |
---|
[5997] | 162 | } |
---|