[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 | |
---|
| 36 | #include "util/Debug.h" |
---|
[7878] | 37 | #include <util/Sleep.h> |
---|
[7823] | 38 | // #include "ClientInformation.h" |
---|
[3214] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | |
---|
| 43 | ServerConnection::ServerConnection(): |
---|
| 44 | bListening_(false) |
---|
| 45 | { |
---|
| 46 | this->bindAddress_ = new ENetAddress(); |
---|
[7459] | 47 | memset(this->bindAddress_, 0, sizeof(ENetAddress)); |
---|
[3214] | 48 | this->bindAddress_->host = ENET_HOST_ANY; |
---|
| 49 | this->bindAddress_->port = NETWORK_PORT; |
---|
| 50 | } |
---|
| 51 | |
---|
[7801] | 52 | ServerConnection::~ServerConnection() |
---|
| 53 | { |
---|
[3214] | 54 | if ( this->bListening_ ) |
---|
| 55 | closeListener(); |
---|
| 56 | delete this->bindAddress_; |
---|
| 57 | } |
---|
| 58 | |
---|
[7801] | 59 | void ServerConnection::setBindAddress( const std::string& bindAddress ) |
---|
| 60 | { |
---|
[7459] | 61 | if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) |
---|
| 62 | COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl; |
---|
[3214] | 63 | } |
---|
| 64 | |
---|
| 65 | void ServerConnection::setPort( unsigned int port ) { |
---|
| 66 | this->bindAddress_->port = port; |
---|
| 67 | } |
---|
| 68 | |
---|
[7801] | 69 | bool ServerConnection::openListener() |
---|
| 70 | { |
---|
| 71 | // create host |
---|
| 72 | this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); |
---|
| 73 | |
---|
[3214] | 74 | if ( this->host_ == NULL ) |
---|
[7459] | 75 | { |
---|
| 76 | COUT(1) << "ServerConnection: host_ == NULL" << std::endl; |
---|
| 77 | return false; |
---|
| 78 | } |
---|
[7801] | 79 | |
---|
| 80 | // enable compression |
---|
| 81 | this->enableCompression(); |
---|
[7459] | 82 | assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); |
---|
| 83 | if (this->host_->socket4 == ENET_SOCKET_NULL) |
---|
| 84 | COUT(2) << "Warning: IPv4 Socket failed." << std::endl; |
---|
| 85 | else if (this->host_->socket6 == ENET_SOCKET_NULL) |
---|
| 86 | COUT(2) << "Warning: IPv6 Socket failed." << std::endl; |
---|
[3214] | 87 | else |
---|
[7459] | 88 | COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl; |
---|
[7801] | 89 | |
---|
| 90 | // start communication thread |
---|
| 91 | Connection::startCommunicationThread(); |
---|
[7459] | 92 | |
---|
| 93 | return true; |
---|
[3214] | 94 | } |
---|
| 95 | |
---|
[7801] | 96 | bool ServerConnection::closeListener() |
---|
| 97 | { |
---|
[3214] | 98 | this->bListening_=false; |
---|
| 99 | disconnectClients(); |
---|
[7801] | 100 | Connection::stopCommunicationThread(); |
---|
[3214] | 101 | enet_host_destroy(this->host_); |
---|
| 102 | return true; |
---|
| 103 | } |
---|
| 104 | |
---|
[7801] | 105 | void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) |
---|
| 106 | { |
---|
[7823] | 107 | if ( clientID == NETWORK_PEER_ID_BROADCAST ) |
---|
[3214] | 108 | { |
---|
[7801] | 109 | broadcastPacket(packet, channelID); |
---|
[3214] | 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
[7823] | 113 | // ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
| 114 | // if(!temp){ |
---|
| 115 | // COUT(3) << "C.Man: addPacket findClient failed" << std::endl; |
---|
| 116 | // } |
---|
| 117 | Connection::addPacket(packet, clientID, channelID); |
---|
[3214] | 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[7823] | 121 | // void ServerConnection::disconnectClient(ClientInformation *client) |
---|
| 122 | // { |
---|
| 123 | // Connection::disconnectPeer( client->getPeer() ); |
---|
| 124 | // } |
---|
[6417] | 125 | |
---|
[7801] | 126 | void ServerConnection::disconnectClient(int clientID) |
---|
| 127 | { |
---|
[7823] | 128 | // ClientInformation *client = ClientInformation::findClient(clientID); |
---|
| 129 | // if(client) |
---|
| 130 | ServerConnection::disconnectClient(clientID); |
---|
[3214] | 131 | } |
---|
| 132 | |
---|
[7801] | 133 | void ServerConnection::disconnectClients() |
---|
| 134 | { |
---|
[7823] | 135 | Connection::disconnectPeers(); |
---|
[7878] | 136 | Connection::waitOutgoingQueue(); |
---|
[3214] | 137 | return; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
[7823] | 141 | // int ServerConnection::getClientID(ENetPeer* peer) |
---|
| 142 | // { |
---|
| 143 | // return getClientID(&(peer->address)); |
---|
| 144 | // } |
---|
[3214] | 145 | |
---|
[7823] | 146 | // int ServerConnection::getClientID(ENetAddress* address) |
---|
| 147 | // { |
---|
| 148 | // return ClientInformation::findClient(address)->getID(); |
---|
| 149 | // } |
---|
| 150 | // |
---|
| 151 | // ENetPeer *ServerConnection::getClientPeer(int clientID) |
---|
| 152 | // { |
---|
| 153 | // return ClientInformation::findClient(clientID)->getPeer(); |
---|
| 154 | // } |
---|
[3214] | 155 | |
---|
| 156 | |
---|
| 157 | } |
---|