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