| [3214] | 1 | /* | 
|---|
|  | 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
|  | 3 | *                    > www.orxonox.net < | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * | 
|---|
|  | 6 | *   License notice: | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This program is free software; you can redistribute it and/or | 
|---|
|  | 9 | *   modify it under the terms of the GNU General Public License | 
|---|
|  | 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
|  | 11 | *   of the License, or (at your option) any later version. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
|  | 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 16 | *   GNU General Public License for more details. | 
|---|
|  | 17 | * | 
|---|
|  | 18 | *   You should have received a copy of the GNU General Public License | 
|---|
|  | 19 | *   along with this program; if not, write to the Free Software | 
|---|
|  | 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
|  | 21 | * | 
|---|
|  | 22 | *   Author: | 
|---|
|  | 23 | *      Oliver Scheuss | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "ServerConnection.h" | 
|---|
|  | 30 |  | 
|---|
|  | 31 | #include <cassert> | 
|---|
|  | 32 | #include <string> | 
|---|
| [5929] | 33 | #define WIN32_LEAN_AND_MEAN | 
|---|
| [3214] | 34 | #include <enet/enet.h> | 
|---|
|  | 35 |  | 
|---|
| [8858] | 36 | #include "util/Output.h" | 
|---|
| [8327] | 37 | #include <util/Sleep.h> | 
|---|
|  | 38 | // #include "ClientInformation.h" | 
|---|
| [3214] | 39 |  | 
|---|
|  | 40 | namespace orxonox | 
|---|
|  | 41 | { | 
|---|
|  | 42 |  | 
|---|
|  | 43 | ServerConnection::ServerConnection(): | 
|---|
|  | 44 | bListening_(false) | 
|---|
|  | 45 | { | 
|---|
|  | 46 | this->bindAddress_ = new ENetAddress(); | 
|---|
| [8327] | 47 | //     memset(this->bindAddress_, 0, sizeof(ENetAddress)); | 
|---|
| [3214] | 48 | this->bindAddress_->host = ENET_HOST_ANY; | 
|---|
|  | 49 | this->bindAddress_->port = NETWORK_PORT; | 
|---|
| [8327] | 50 | this->bindAddress_->scopeID = 0; | 
|---|
| [3214] | 51 | } | 
|---|
|  | 52 |  | 
|---|
| [7801] | 53 | ServerConnection::~ServerConnection() | 
|---|
|  | 54 | { | 
|---|
| [3214] | 55 | if ( this->bListening_ ) | 
|---|
|  | 56 | closeListener(); | 
|---|
|  | 57 | delete this->bindAddress_; | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
| [7801] | 60 | void ServerConnection::setBindAddress( const std::string& bindAddress ) | 
|---|
|  | 61 | { | 
|---|
| [7459] | 62 | if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) | 
|---|
| [8858] | 63 | orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; | 
|---|
| [3214] | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | void ServerConnection::setPort( unsigned int port ) { | 
|---|
|  | 67 | this->bindAddress_->port = port; | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [7801] | 70 | bool ServerConnection::openListener() | 
|---|
|  | 71 | { | 
|---|
|  | 72 | // create host | 
|---|
|  | 73 | this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); | 
|---|
|  | 74 |  | 
|---|
| [11071] | 75 | if ( this->host_ == nullptr ) | 
|---|
| [7459] | 76 | { | 
|---|
| [11071] | 77 | orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl; | 
|---|
| [7459] | 78 | return false; | 
|---|
|  | 79 | } | 
|---|
| [7801] | 80 |  | 
|---|
|  | 81 | // enable compression | 
|---|
|  | 82 | this->enableCompression(); | 
|---|
| [7459] | 83 | assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); | 
|---|
|  | 84 | if (this->host_->socket4 == ENET_SOCKET_NULL) | 
|---|
| [8858] | 85 | orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl; | 
|---|
| [7459] | 86 | else if (this->host_->socket6 == ENET_SOCKET_NULL) | 
|---|
| [8858] | 87 | orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl; | 
|---|
| [3214] | 88 | else | 
|---|
| [8858] | 89 | orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl; | 
|---|
| [7801] | 90 |  | 
|---|
|  | 91 | // start communication thread | 
|---|
|  | 92 | Connection::startCommunicationThread(); | 
|---|
| [7459] | 93 |  | 
|---|
|  | 94 | return true; | 
|---|
| [3214] | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [7801] | 97 | bool ServerConnection::closeListener() | 
|---|
|  | 98 | { | 
|---|
| [3214] | 99 | this->bListening_=false; | 
|---|
|  | 100 | disconnectClients(); | 
|---|
| [7801] | 101 | Connection::stopCommunicationThread(); | 
|---|
| [3214] | 102 | enet_host_destroy(this->host_); | 
|---|
|  | 103 | return true; | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
| [7801] | 106 | void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) | 
|---|
|  | 107 | { | 
|---|
| [8327] | 108 | if ( clientID == NETWORK_PEER_ID_BROADCAST ) | 
|---|
| [3214] | 109 | { | 
|---|
| [7801] | 110 | broadcastPacket(packet, channelID); | 
|---|
| [3214] | 111 | } | 
|---|
|  | 112 | else | 
|---|
|  | 113 | { | 
|---|
| [8327] | 114 | //       ClientInformation *temp = ClientInformation::findClient(clientID); | 
|---|
|  | 115 | //       if(!temp){ | 
|---|
| [8858] | 116 | //         orxout(internal_warning, context::network) << "C.Man: addPacket findClient failed" << endl; | 
|---|
| [8327] | 117 | //       } | 
|---|
|  | 118 | Connection::addPacket(packet, clientID, channelID); | 
|---|
| [3214] | 119 | } | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
| [8327] | 122 | //   void ServerConnection::disconnectClient(ClientInformation *client) | 
|---|
|  | 123 | //   { | 
|---|
|  | 124 | //     Connection::disconnectPeer( client->getPeer() ); | 
|---|
|  | 125 | //   } | 
|---|
| [6417] | 126 |  | 
|---|
| [7801] | 127 | void ServerConnection::disconnectClient(int clientID) | 
|---|
|  | 128 | { | 
|---|
| [8327] | 129 | //     ClientInformation *client = ClientInformation::findClient(clientID); | 
|---|
|  | 130 | //     if(client) | 
|---|
| [8358] | 131 | Connection::disconnectPeer(clientID); | 
|---|
| [3214] | 132 | } | 
|---|
|  | 133 |  | 
|---|
| [7801] | 134 | void ServerConnection::disconnectClients() | 
|---|
|  | 135 | { | 
|---|
| [8327] | 136 | Connection::disconnectPeers(); | 
|---|
|  | 137 | Connection::waitOutgoingQueue(); | 
|---|
| [3214] | 138 | return; | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 |  | 
|---|
| [8327] | 142 | //   int ServerConnection::getClientID(ENetPeer* peer) | 
|---|
|  | 143 | //   { | 
|---|
|  | 144 | //     return getClientID(&(peer->address)); | 
|---|
|  | 145 | //   } | 
|---|
| [3214] | 146 |  | 
|---|
| [8327] | 147 | //   int ServerConnection::getClientID(ENetAddress* address) | 
|---|
|  | 148 | //   { | 
|---|
|  | 149 | //     return ClientInformation::findClient(address)->getID(); | 
|---|
|  | 150 | //   } | 
|---|
|  | 151 | // | 
|---|
|  | 152 | //   ENetPeer *ServerConnection::getClientPeer(int clientID) | 
|---|
|  | 153 | //   { | 
|---|
|  | 154 | //     return ClientInformation::findClient(clientID)->getPeer(); | 
|---|
|  | 155 | //   } | 
|---|
| [3214] | 156 |  | 
|---|
|  | 157 |  | 
|---|
|  | 158 | } | 
|---|