[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" |
---|
[5520] | 32 | |
---|
[5530] | 33 | |
---|
[5525] | 34 | /* using namespace std is default, this needs to be here */ |
---|
[5520] | 35 | using namespace std; |
---|
| 36 | |
---|
[6695] | 37 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
[5520] | 38 | |
---|
[6695] | 39 | |
---|
[5996] | 40 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
[5520] | 41 | |
---|
| 42 | /** |
---|
| 43 | * standard constructor |
---|
| 44 | */ |
---|
| 45 | NetworkManager::NetworkManager() |
---|
[5566] | 46 | { |
---|
[5572] | 47 | /* set the class id for the base object */ |
---|
[5575] | 48 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
[6341] | 49 | PRINTF(0)("START\n"); |
---|
[5804] | 50 | |
---|
[5572] | 51 | /* initialize the references */ |
---|
| 52 | this->netStreamList = NULL; |
---|
| 53 | this->syncList = NULL; |
---|
[6695] | 54 | this->defaultSyncStream = NULL; |
---|
| 55 | this->sharedNetworkData = SharedNetworkData::getInstance(); |
---|
[7954] | 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 ); |
---|
[6341] | 63 | |
---|
[5605] | 64 | PRINTF(0)("NetworkManager created\n"); |
---|
[5566] | 65 | } |
---|
[5520] | 66 | |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * standard deconstructor |
---|
| 70 | */ |
---|
| 71 | NetworkManager::~NetworkManager() |
---|
| 72 | {} |
---|
| 73 | |
---|
[5522] | 74 | |
---|
| 75 | /** |
---|
| 76 | * initializes the network manager |
---|
| 77 | */ |
---|
| 78 | void NetworkManager::initialize() |
---|
[5572] | 79 | { |
---|
| 80 | /* get the synchronizeable list from the class list */ |
---|
[5575] | 81 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
[5605] | 82 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
[7954] | 83 | |
---|
[5572] | 84 | } |
---|
[5522] | 85 | |
---|
[5530] | 86 | |
---|
[5522] | 87 | /** |
---|
| 88 | * shutsdown the network manager |
---|
| 89 | */ |
---|
| 90 | void NetworkManager::shutdown() |
---|
[5578] | 91 | { |
---|
[5522] | 92 | |
---|
[5578] | 93 | } |
---|
[5522] | 94 | |
---|
[5578] | 95 | |
---|
[5522] | 96 | /** |
---|
| 97 | * creates a connection from one object to a host |
---|
[5647] | 98 | * @param hostName: the name of the destination host |
---|
| 99 | */ |
---|
[7256] | 100 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
---|
[5996] | 101 | { |
---|
[7954] | 102 | this->defaultSyncStream = new NetworkStream( name, port ); |
---|
[6695] | 103 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
| 104 | this->defaultSyncStream->startHandshake(); |
---|
[5996] | 105 | return 1; |
---|
| 106 | } |
---|
[5647] | 107 | |
---|
[5996] | 108 | |
---|
[5647] | 109 | /** |
---|
[5996] | 110 | * creates a new NetworkStream of server type |
---|
| 111 | * @param port: number of the TCP port |
---|
| 112 | */ |
---|
| 113 | int NetworkManager::createServer(unsigned int port) |
---|
| 114 | { |
---|
[6695] | 115 | this->sharedNetworkData->setHostID(0); |
---|
| 116 | this->sharedNetworkData->setGameServer(true); |
---|
| 117 | this->defaultSyncStream = new NetworkStream(port); |
---|
| 118 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
| 119 | this->defaultSyncStream->createNetworkGameManager(); |
---|
| 120 | PRINTF(0)("CREATE SERVER\n"); |
---|
[5996] | 121 | SDL_Delay(20); |
---|
| 122 | return 1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
| 127 | { |
---|
[6695] | 128 | if( this->defaultSyncStream) |
---|
| 129 | this->defaultSyncStream->connectSynchronizeable(sync); |
---|
[5996] | 130 | } |
---|
[5522] | 131 | |
---|
[5996] | 132 | |
---|
[5522] | 133 | /** |
---|
| 134 | * sync the network |
---|
[7954] | 135 | * @param dtS: the seceonds elapsed since the last synchronize call |
---|
[5522] | 136 | */ |
---|
[7954] | 137 | void NetworkManager::synchronize( float dtS) |
---|
[5578] | 138 | { |
---|
[7954] | 139 | this->elapsedTime += dtS; |
---|
| 140 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
---|
| 141 | return; |
---|
| 142 | this->elapsedTime = 0.0f; |
---|
| 143 | |
---|
[5578] | 144 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
| 145 | { |
---|
[5885] | 146 | std::list<BaseObject*>::const_iterator stream; |
---|
[5822] | 147 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
[5996] | 148 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
| 149 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
[5578] | 150 | } |
---|
| 151 | } |
---|
[5997] | 152 | |
---|
[6695] | 153 | |
---|
| 154 | |
---|
[5997] | 155 | /** |
---|
[6695] | 156 | * debug output |
---|
[5997] | 157 | */ |
---|
[6695] | 158 | void NetworkManager::debug() |
---|
[5997] | 159 | { |
---|
[6695] | 160 | PRINT(0)("=================Network::debug()=========\n"); |
---|
| 161 | this->defaultSyncStream->debug(); |
---|
| 162 | PRINT(0)("===========================================\n"); |
---|
[5997] | 163 | } |
---|