| 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 | using namespace std; | 
|---|
| 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->sharedNetworkData = SharedNetworkData::getInstance(); | 
|---|
| 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 ); | 
|---|
| 62 |  | 
|---|
| 63 |   PRINTF(0)("NetworkManager created\n"); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | /** | 
|---|
| 68 |  *  standard deconstructor | 
|---|
| 69 |  */ | 
|---|
| 70 | NetworkManager::~NetworkManager() | 
|---|
| 71 | { | 
|---|
| 72 |   if ( this->networkStream ) | 
|---|
| 73 |   { | 
|---|
| 74 |     delete this->networkStream; | 
|---|
| 75 |     this->networkStream = NULL; | 
|---|
| 76 |   } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 |  *  initializes the network manager | 
|---|
| 82 |  */ | 
|---|
| 83 | void NetworkManager::initialize() | 
|---|
| 84 | { | 
|---|
| 85 |   PRINTF(0)("NetworkManager initzalized\n"); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 |  *  shutsdown the network manager | 
|---|
| 91 |  */ | 
|---|
| 92 | void NetworkManager::shutdown() | 
|---|
| 93 | { | 
|---|
| 94 |  | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 |  *  creates a connection from one object to a host | 
|---|
| 100 |  * @param hostName: the name of the destination host | 
|---|
| 101 |  */ | 
|---|
| 102 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) | 
|---|
| 103 | { | 
|---|
| 104 |   this->networkStream = new NetworkStream( name, port ); | 
|---|
| 105 |   this->sharedNetworkData->setDefaultSyncStream(this->networkStream); | 
|---|
| 106 |   this->networkStream->startHandshake(); | 
|---|
| 107 |   return 1; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 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 | { | 
|---|
| 117 |   this->sharedNetworkData->setHostID(0); | 
|---|
| 118 |   this->sharedNetworkData->setGameServer(true); | 
|---|
| 119 |   this->networkStream = new NetworkStream(port); | 
|---|
| 120 |   this->sharedNetworkData->setDefaultSyncStream(this->networkStream); | 
|---|
| 121 |   this->networkStream->createNetworkGameManager(); | 
|---|
| 122 |   PRINTF(0)("CREATE SERVER\n"); | 
|---|
| 123 |   SDL_Delay(20); | 
|---|
| 124 |   return 1; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) | 
|---|
| 129 | { | 
|---|
| 130 |   if( this->networkStream) | 
|---|
| 131 |     this->networkStream->connectSynchronizeable(sync); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 | /** | 
|---|
| 136 |  *  sync the network | 
|---|
| 137 |  *  @param dtS: the seceonds elapsed since the last synchronize call | 
|---|
| 138 |  */ | 
|---|
| 139 | void NetworkManager::synchronize( float dtS) | 
|---|
| 140 | { | 
|---|
| 141 |   this->elapsedTime += dtS; | 
|---|
| 142 |   if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY)) | 
|---|
| 143 |     return; | 
|---|
| 144 |   this->elapsedTime = 0.0f; | 
|---|
| 145 |  | 
|---|
| 146 |   if ( networkStream->isActive() ) | 
|---|
| 147 |     networkStream->processData(); | 
|---|
| 148 |    | 
|---|
| 149 |   NetworkGameManager::getInstance()->tick( this->elapsedTime ); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 | /** | 
|---|
| 155 |  * debug output | 
|---|
| 156 |  */ | 
|---|
| 157 |  void NetworkManager::debug() | 
|---|
| 158 | { | 
|---|
| 159 |   PRINT(0)("=================Network::debug()=========\n"); | 
|---|
| 160 |   this->networkStream->debug(); | 
|---|
| 161 |   PRINT(0)("===========================================\n"); | 
|---|
| 162 | } | 
|---|