Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/ServerConnection.cc @ 5781

Last change on this file since 5781 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

File size: 4.5 KB
Line 
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>
33#define WIN32_LEAN_AND_MEAN
34#include <enet/enet.h>
35
36#include "util/Debug.h"
37#include "ClientInformation.h"
38
39namespace orxonox
40{
41
42  ServerConnection::ServerConnection():
43    bListening_(false)
44  {
45    this->bindAddress_ = new ENetAddress();
46    this->bindAddress_->host = ENET_HOST_ANY;
47    this->bindAddress_->port = NETWORK_PORT;
48  }
49
50  ServerConnection::~ServerConnection(){
51    if ( this->bListening_ )
52      closeListener();
53    delete this->bindAddress_;
54  }
55
56  void ServerConnection::setBindAddress( const std::string& bindAddress ) {
57    enet_address_set_host (this->bindAddress_, bindAddress.c_str());
58  }
59
60  void ServerConnection::setPort( unsigned int port ) {
61      this->bindAddress_->port = port;
62  }
63
64  bool ServerConnection::openListener() {
65    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0);
66    if ( this->host_ == NULL )
67      return false;
68    else
69      return true;
70  }
71
72  bool ServerConnection::closeListener() {
73    this->bListening_=false;
74    disconnectClients();
75    enet_host_destroy(this->host_);
76    return true;
77  }
78
79  bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
80    if ( clientID == CLIENTID_UNKNOWN )
81    {
82      return addPacketAll(packet);
83    }
84    else
85    {
86      ClientInformation *temp = ClientInformation::findClient(clientID);
87      if(!temp){
88        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
89        return false;
90      }
91      return Connection::addPacket(packet, temp->getPeer());
92    }
93  }
94
95  bool ServerConnection::addPacketAll(ENetPacket *packet) {
96    if ( !Connection::getInstance() )
97      return false;
98    enet_host_broadcast( Connection::getInstance()->getHost(), 0, packet);
99    return true;
100  }
101 
102  void ServerConnection::disconnectClient(ClientInformation *client)
103  {
104    Connection::disconnectPeer( client->getPeer() );
105    delete client;
106  }
107 
108  void ServerConnection::disconnectPeer( ENetEvent* event )
109  {
110    COUT(4) << "removing client from list" << std::endl;
111    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
112    if(!client)
113      return;
114    else
115      ServerConnection::disconnectClient( client );
116  }
117 
118  void ServerConnection::disconnectClient(int clientID){
119    ClientInformation *client = ClientInformation::findClient(clientID);
120    if(client)
121      disconnectClient(client);
122  }
123
124  void ServerConnection::disconnectClients() {
125    ENetEvent event;
126    ClientInformation *temp = ClientInformation::getBegin();
127    while(temp!=0){
128      disconnectClient( temp );
129      temp = temp->next();
130    }
131    temp = ClientInformation::getBegin();
132    while( temp!=0 ){
133      if( service( &event ) )
134      {
135        switch (event.type)
136        {
137        case ENET_EVENT_TYPE_NONE: break;
138        case ENET_EVENT_TYPE_CONNECT: break;
139        case ENET_EVENT_TYPE_RECEIVE:
140          enet_packet_destroy(event.packet);
141          break;
142        case ENET_EVENT_TYPE_DISCONNECT:
143          if(ClientInformation::findClient(&(event.peer->address)))
144            delete ClientInformation::findClient(&(event.peer->address));
145          temp = ClientInformation::getBegin();
146          break;
147        }
148      }
149    }
150    return;
151  }
152
153
154  int ServerConnection::getClientID(ENetPeer* peer) {
155    return getClientID(&(peer->address));
156  }
157
158  int ServerConnection::getClientID(ENetAddress* address) {
159    return ClientInformation::findClient(address)->getID();
160  }
161
162  ENetPeer *ServerConnection::getClientPeer(int clientID) {
163    return ClientInformation::findClient(clientID)->getPeer();
164  }
165
166
167}
Note: See TracBrowser for help on using the repository browser.