[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 | |
---|
| 22 | |
---|
[5566] | 23 | #include "network_stream.h" |
---|
[5572] | 24 | #include "class_list.h" |
---|
[5566] | 25 | |
---|
[5605] | 26 | #include "debug.h" |
---|
[6658] | 27 | #include "shell_command.h" |
---|
[5566] | 28 | |
---|
[5525] | 29 | /* include your own header */ |
---|
[5520] | 30 | #include "network_manager.h" |
---|
| 31 | |
---|
[5530] | 32 | |
---|
[5525] | 33 | /* using namespace std is default, this needs to be here */ |
---|
[5520] | 34 | using namespace std; |
---|
| 35 | |
---|
[6658] | 36 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
[5520] | 37 | |
---|
[6658] | 38 | |
---|
[5996] | 39 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
[5520] | 40 | |
---|
| 41 | /** |
---|
| 42 | * standard constructor |
---|
| 43 | */ |
---|
| 44 | NetworkManager::NetworkManager() |
---|
[5566] | 45 | { |
---|
[5572] | 46 | /* set the class id for the base object */ |
---|
[5575] | 47 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
[6341] | 48 | PRINTF(0)("START\n"); |
---|
[5804] | 49 | |
---|
[5572] | 50 | /* initialize the references */ |
---|
| 51 | this->netStreamList = NULL; |
---|
| 52 | this->syncList = NULL; |
---|
[6654] | 53 | this->defaultSyncStream = NULL; |
---|
[6341] | 54 | |
---|
[5997] | 55 | this->hostID = -1; |
---|
[6139] | 56 | this->bGameServer = false; |
---|
[5804] | 57 | |
---|
[5605] | 58 | PRINTF(0)("NetworkManager created\n"); |
---|
[5566] | 59 | } |
---|
[5520] | 60 | |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * standard deconstructor |
---|
| 64 | */ |
---|
| 65 | NetworkManager::~NetworkManager() |
---|
| 66 | {} |
---|
| 67 | |
---|
[5522] | 68 | |
---|
| 69 | /** |
---|
| 70 | * initializes the network manager |
---|
| 71 | */ |
---|
| 72 | void NetworkManager::initialize() |
---|
[5572] | 73 | { |
---|
| 74 | /* get the synchronizeable list from the class list */ |
---|
[5575] | 75 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
[5605] | 76 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
[5572] | 77 | } |
---|
[5522] | 78 | |
---|
[5530] | 79 | |
---|
[5522] | 80 | /** |
---|
| 81 | * shutsdown the network manager |
---|
| 82 | */ |
---|
| 83 | void NetworkManager::shutdown() |
---|
[5578] | 84 | { |
---|
[5522] | 85 | |
---|
[5578] | 86 | } |
---|
[5522] | 87 | |
---|
[5578] | 88 | |
---|
[5522] | 89 | /** |
---|
| 90 | * creates a connection from one object to a host |
---|
[5647] | 91 | * @param hostName: the name of the destination host |
---|
| 92 | */ |
---|
[5996] | 93 | int NetworkManager::establishConnection(const char* name, unsigned int port) |
---|
| 94 | { |
---|
| 95 | IPaddress ipAddress; |
---|
| 96 | int error = SDLNet_ResolveHost(&ipAddress, name, port); |
---|
| 97 | if( error == -1) { |
---|
| 98 | printf("\n\nerror on address resolution, program inconsistency\n\n"); |
---|
| 99 | return -1; |
---|
| 100 | } |
---|
[5647] | 101 | |
---|
[6654] | 102 | this->defaultSyncStream = new NetworkStream(ipAddress); |
---|
[6662] | 103 | this->defaultSyncStream->startHandshake(); |
---|
[5996] | 104 | return 1; |
---|
| 105 | } |
---|
[5647] | 106 | |
---|
[5996] | 107 | |
---|
[5647] | 108 | /** |
---|
[5996] | 109 | * creates a new NetworkStream of server type |
---|
| 110 | * @param port: number of the TCP port |
---|
| 111 | */ |
---|
| 112 | int NetworkManager::createServer(unsigned int port) |
---|
| 113 | { |
---|
[6341] | 114 | this->hostID = 0; |
---|
| 115 | this->bGameServer = true; |
---|
[6654] | 116 | this->defaultSyncStream = new NetworkStream(port); |
---|
[6659] | 117 | this->defaultSyncStream->createNetworkGameManager(); |
---|
[6658] | 118 | PRINTF(0)("CREATE SERVER\n"); |
---|
[6139] | 119 | this->bGameServer = true; |
---|
[5996] | 120 | SDL_Delay(20); |
---|
| 121 | return 1; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | /** |
---|
[5647] | 126 | * creates a connection from one object to a host |
---|
[5522] | 127 | * @param address: the address of the destination host |
---|
| 128 | * @param synchronizeable: reference to the sync object |
---|
| 129 | */ |
---|
[5648] | 130 | NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync) |
---|
[5572] | 131 | { |
---|
[5578] | 132 | /* creating a new network stream, it will register itself automaticaly to the class list */ |
---|
[6654] | 133 | this->defaultSyncStream = new NetworkStream(address); |
---|
| 134 | this->defaultSyncStream->connectSynchronizeable(sync); |
---|
[5572] | 135 | } |
---|
[5522] | 136 | |
---|
[5996] | 137 | |
---|
[5649] | 138 | /** |
---|
[5522] | 139 | * teardown a connection |
---|
| 140 | */ |
---|
| 141 | void NetworkManager::shutdownConnection() |
---|
[5605] | 142 | { |
---|
| 143 | PRINTF(0)("Shutdown connection\n"); |
---|
| 144 | } |
---|
[5522] | 145 | |
---|
[5530] | 146 | |
---|
[5996] | 147 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
| 148 | { |
---|
[6658] | 149 | if( this->defaultSyncStream) |
---|
| 150 | this->defaultSyncStream->connectSynchronizeable(sync); |
---|
[5996] | 151 | } |
---|
[5522] | 152 | |
---|
[5996] | 153 | |
---|
[5522] | 154 | /** |
---|
| 155 | * sync the network |
---|
| 156 | */ |
---|
| 157 | void NetworkManager::synchronize() |
---|
[5578] | 158 | { |
---|
| 159 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
| 160 | { |
---|
[5885] | 161 | std::list<BaseObject*>::const_iterator stream; |
---|
[5822] | 162 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
[5996] | 163 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
| 164 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
[5578] | 165 | } |
---|
| 166 | } |
---|
[5997] | 167 | |
---|
| 168 | /** |
---|
| 169 | * Sets the hostID to a specific number |
---|
| 170 | * @param id: The new ID |
---|
| 171 | */ |
---|
| 172 | void NetworkManager::setHostID(int id) |
---|
| 173 | { |
---|
[6341] | 174 | this->hostID = id; |
---|
[5997] | 175 | } |
---|
[6658] | 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * debug output |
---|
| 181 | */ |
---|
| 182 | void NetworkManager::debug() |
---|
| 183 | { |
---|
[6660] | 184 | PRINT(0)("=================Network::debug()=========\n"); |
---|
[6658] | 185 | this->defaultSyncStream->debug(); |
---|
[6660] | 186 | PRINT(0)("===========================================\n"); |
---|
[6658] | 187 | } |
---|