Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/network/ServerConnection.cc @ 5849

Last change on this file since 5849 was 5849, checked in by scheusso, 15 years ago

some svn:eol-style native
fix which improves disconnect handling

  • Property svn:eol-style set to native
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  }
106 
107  void ServerConnection::disconnectPeer( ENetEvent* event )
108  {
109    COUT(4) << "removing client from list" << std::endl;
110    ClientInformation *client = ClientInformation::findClient(&event->peer->address);
111    if(!client)
112      return;
113    else
114      ServerConnection::disconnectClient( client );
115  }
116 
117  void ServerConnection::disconnectClient(int clientID){
118    ClientInformation *client = ClientInformation::findClient(clientID);
119    if(client)
120      ServerConnection::disconnectClient(client);
121  }
122
123  void ServerConnection::disconnectClients() {
124    ENetEvent event;
125    ClientInformation *temp = ClientInformation::getBegin();
126    while(temp!=0){
127      ServerConnection::disconnectClient( temp );
128      temp = temp->next();
129    }
130    temp = ClientInformation::getBegin();
131    while( temp!=0 ){
132      if( service( &event ) )
133      {
134        switch (event.type)
135        {
136        case ENET_EVENT_TYPE_NONE: break;
137        case ENET_EVENT_TYPE_CONNECT: break;
138        case ENET_EVENT_TYPE_RECEIVE:
139          enet_packet_destroy(event.packet);
140          break;
141        case ENET_EVENT_TYPE_DISCONNECT:
142          if(ClientInformation::findClient(&(event.peer->address)))
143            delete ClientInformation::findClient(&(event.peer->address));
144          temp = ClientInformation::getBegin();
145          break;
146        }
147      }
148    }
149    return;
150  }
151
152
153  int ServerConnection::getClientID(ENetPeer* peer) {
154    return getClientID(&(peer->address));
155  }
156
157  int ServerConnection::getClientID(ENetAddress* address) {
158    return ClientInformation::findClient(address)->getID();
159  }
160
161  ENetPeer *ServerConnection::getClientPeer(int clientID) {
162    return ClientInformation::findClient(clientID)->getPeer();
163  }
164
165
166}
Note: See TracBrowser for help on using the repository browser.