/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oliver Scheuss * Co-authors: * ... * */ #include "ServerConnection.h" #include #include #define WIN32_LEAN_AND_MEAN #include #include "util/Output.h" #include namespace orxonox { // TODO: Calls to the Connection superclass are done as follows: // Connection::startCommunicationThread() // However, these methods are not overridden. // Why can't we just do this->startCommunicationThread()? /** * Constructor */ ServerConnection::ServerConnection(): bListening_(false) { this->bindAddress_ = new ENetAddress(); this->bindAddress_->host = ENET_HOST_ANY; this->bindAddress_->port = NETWORK_PORT; this->bindAddress_->scopeID = 0; } /** * Destructor */ ServerConnection::~ServerConnection() { if (this->bListening_) { this->closeListener(); } delete this->bindAddress_; } /** * Set address on which to listen. * @param bindAddress The address on which to listen */ void ServerConnection::setBindAddress(const std::string& bindAddress) { if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0) { orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; } } /** * Set port on which to listen on. * @param port The port on which to listen on. */ void ServerConnection::setPort(unsigned int port) { this->bindAddress_->port = port; } bool ServerConnection::openListener() { // create host this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); if (this->host_ == nullptr) { orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl; return false; } // enable compression this->enableCompression(); // ensure that either IPv4 or IPv6 succeeded assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); if (this->host_->socket4 == ENET_SOCKET_NULL) { orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl; } else if (this->host_->socket6 == ENET_SOCKET_NULL) { orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl; } else { orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl; } // start communication thread Connection::startCommunicationThread(); return true; } /** * Stop listening. */ bool ServerConnection::closeListener() { this->bListening_ = false; disconnectClients(); Connection::stopCommunicationThread(); enet_host_destroy(this->host_); return true; } /** * Add outgoing packet to queue. * @param packet The packet to send * @param clientID The ID of the recipient * @param channelID The channel ID * TODO: Not sure yet how this actually works */ void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) { if (clientID == NETWORK_PEER_ID_BROADCAST) { broadcastPacket(packet, channelID); } else { Connection::addPacket(packet, clientID, channelID); } } /** * Terminate connection with a peer. * @param clientID The peer with which to terminate the connection. */ void ServerConnection::disconnectClient(int clientID) { Connection::disconnectPeer(clientID); } /** * Disconnect all peers. */ void ServerConnection::disconnectClients() { Connection::disconnectPeers(); Connection::waitOutgoingQueue(); return; } }