/* * 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 "ClientInformation.h" #include "core/CoreIncludes.h" namespace orxonox { ServerConnection::ServerConnection(): bListening_(false) { this->bindAddress_ = new ENetAddress(); this->bindAddress_->host = ENET_HOST_ANY; this->bindAddress_->port = NETWORK_PORT; } ServerConnection::~ServerConnection(){ if ( this->bListening_ ) closeListener(); delete this->bindAddress_; } bool ServerConnection::openListener() { enet_initialize(); atexit(enet_deinitialize); this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0); if ( this->host_ == NULL ) return false; } bool ServerConnection::closeListener() { this->bListening_=false; disconnectClients(); enet_host_destroy(this->host_); } bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) { if ( clientID == CLIENTID_UNKNOWN ) { return addPacketAll(packet); } else { ClientInformation *temp = ClientInformation::findClient(clientID); if(!temp){ COUT(3) << "C.Man: addPacket findClient failed" << std::endl; return false; } return Connection::addPacket(packet, temp->getPeer()); } } bool ServerConnection::addPacketAll(ENetPacket *packet) { if ( !Connection::getInstance() ) return false; enet_host_broadcast( Connection::getInstance()->getHost(), 0, packet); return true; } void ServerConnection::disconnectClient(ClientInformation *client) { disconnectPeer( client->getPeer() ); delete client; } void ServerConnection::disconnectClient( ENetEvent* event ) { COUT(4) << "removing client from list" << std::endl; ClientInformation *client = ClientInformation::findClient(&event->peer->address); if(!client) return; else ServerConnection::disconnectClient( client ); } void ServerConnection::disconnectClient(int clientID){ ClientInformation *client = ClientInformation::findClient(clientID); if(client) disconnectClient(client); } void ServerConnection::disconnectClients() { ENetEvent event; ClientInformation *temp = ClientInformation::getBegin(); while(temp!=0){ disconnectPeer( temp->getPeer() ); temp = temp->next(); } temp = ClientInformation::getBegin(); while( temp!=0 ){ if( service( &event ) ) { switch (event.type) { case ENET_EVENT_TYPE_NONE: break; case ENET_EVENT_TYPE_CONNECT: break; case ENET_EVENT_TYPE_RECEIVE: enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: if(ClientInformation::findClient(&(event.peer->address))) delete ClientInformation::findClient(&(event.peer->address)); temp = ClientInformation::getBegin(); break; } } } return; } int ServerConnection::getClientID(ENetPeer* peer) { return getClientID(&(peer->address)); } int ServerConnection::getClientID(ENetAddress* address) { return ClientInformation::findClient(address)->getID(); } ENetPeer *ServerConnection::getClientPeer(int clientID) { return ClientInformation::findClient(clientID)->getPeer(); } }