| 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 | #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 | * creates a connection from one object to a host |
|---|
| 102 | * @param hostName: the name of the destination host |
|---|
| 103 | */ |
|---|
| 104 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
|---|
| 105 | { |
|---|
| 106 | this->networkStream = new NetworkStream( name, port ); |
|---|
| 107 | SharedNetworkData::getInstance()->setDefaultSyncStream(this->networkStream); |
|---|
| 108 | this->networkStream->startHandshake(); |
|---|
| 109 | return 1; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * creates a new NetworkStream of server type |
|---|
| 115 | * @param port: number of the TCP port |
|---|
| 116 | */ |
|---|
| 117 | int NetworkManager::createServer(unsigned int port) |
|---|
| 118 | { |
|---|
| 119 | SharedNetworkData::getInstance()->setHostID(0); |
|---|
| 120 | SharedNetworkData::getInstance()->setNodeType(NET_MASTER_SERVER); |
|---|
| 121 | this->networkStream = new NetworkStream(port); |
|---|
| 122 | SharedNetworkData::getInstance()->setDefaultSyncStream(this->networkStream); |
|---|
| 123 | this->networkStream->createNetworkGameManager(); |
|---|
| 124 | PRINTF(0)("CREATE SERVER\n"); |
|---|
| 125 | SDL_Delay(20); |
|---|
| 126 | return 1; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
|---|
| 131 | { |
|---|
| 132 | if( this->networkStream) |
|---|
| 133 | this->networkStream->connectSynchronizeable(sync); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * sync the network |
|---|
| 139 | * @param dtS: the seceonds elapsed since the last synchronize call |
|---|
| 140 | */ |
|---|
| 141 | void NetworkManager::synchronize( float dtS) |
|---|
| 142 | { |
|---|
| 143 | this->elapsedTime += dtS; |
|---|
| 144 | if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) |
|---|
| 145 | return; |
|---|
| 146 | this->elapsedTime = 0.0f; |
|---|
| 147 | |
|---|
| 148 | if ( networkStream ) |
|---|
| 149 | networkStream->processData(); |
|---|
| 150 | |
|---|
| 151 | NetworkGameManager::getInstance()->tick( this->elapsedTime ); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * debug output |
|---|
| 158 | */ |
|---|
| 159 | void NetworkManager::debug() |
|---|
| 160 | { |
|---|
| 161 | PRINT(0)("=================Network::debug()=========\n"); |
|---|
| 162 | this->networkStream->debug(); |
|---|
| 163 | PRINT(0)("===========================================\n"); |
|---|
| 164 | } |
|---|