| 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 |
|---|
| 13 | co-programmer: ... |
|---|
| 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 | |
|---|
| 33 | |
|---|
| 34 | /* using namespace std is default, this needs to be here */ |
|---|
| 35 | using namespace std; |
|---|
| 36 | |
|---|
| 37 | SHELL_COMMAND(debug, NetworkManager, debug); |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | NetworkManager* NetworkManager::singletonRef = NULL; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * standard constructor |
|---|
| 44 | */ |
|---|
| 45 | NetworkManager::NetworkManager() |
|---|
| 46 | { |
|---|
| 47 | /* set the class id for the base object */ |
|---|
| 48 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
|---|
| 49 | PRINTF(0)("START\n"); |
|---|
| 50 | |
|---|
| 51 | /* initialize the references */ |
|---|
| 52 | this->netStreamList = NULL; |
|---|
| 53 | this->syncList = NULL; |
|---|
| 54 | this->defaultSyncStream = NULL; |
|---|
| 55 | this->sharedNetworkData = SharedNetworkData::getInstance(); |
|---|
| 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 | |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * initializes the network manager |
|---|
| 77 | */ |
|---|
| 78 | void NetworkManager::initialize() |
|---|
| 79 | { |
|---|
| 80 | /* get the synchronizeable list from the class list */ |
|---|
| 81 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
|---|
| 82 | PRINTF(0)("NetworkManager initzalized\n"); |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * shutsdown the network manager |
|---|
| 89 | */ |
|---|
| 90 | void NetworkManager::shutdown() |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * creates a connection from one object to a host |
|---|
| 98 | * @param hostName: the name of the destination host |
|---|
| 99 | */ |
|---|
| 100 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
|---|
| 101 | { |
|---|
| 102 | this->defaultSyncStream = new NetworkStream( name, port ); |
|---|
| 103 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
|---|
| 104 | this->defaultSyncStream->startHandshake(); |
|---|
| 105 | return 1; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 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 | { |
|---|
| 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"); |
|---|
| 121 | SDL_Delay(20); |
|---|
| 122 | return 1; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
|---|
| 127 | { |
|---|
| 128 | if( this->defaultSyncStream) |
|---|
| 129 | this->defaultSyncStream->connectSynchronizeable(sync); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * sync the network |
|---|
| 135 | * @param dtS: the seceonds elapsed since the last synchronize call |
|---|
| 136 | */ |
|---|
| 137 | void NetworkManager::synchronize( float dtS) |
|---|
| 138 | { |
|---|
| 139 | this->elapsedTime += dtS; |
|---|
| 140 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
|---|
| 141 | return; |
|---|
| 142 | this->elapsedTime = 0.0f; |
|---|
| 143 | |
|---|
| 144 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
|---|
| 145 | { |
|---|
| 146 | std::list<BaseObject*>::const_iterator stream; |
|---|
| 147 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
|---|
| 148 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
|---|
| 149 | static_cast<NetworkStream*>(*stream)->processData(); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * debug output |
|---|
| 157 | */ |
|---|
| 158 | void NetworkManager::debug() |
|---|
| 159 | { |
|---|
| 160 | PRINT(0)("=================Network::debug()=========\n"); |
|---|
| 161 | this->defaultSyncStream->debug(); |
|---|
| 162 | PRINT(0)("===========================================\n"); |
|---|
| 163 | } |
|---|