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