Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/ServerConnection.cc @ 3374

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

fix in disconnect behaviour

File size: 4.5 KB
RevLine 
[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>
33#include <enet/enet.h>
34
35#include "util/Debug.h"
36#include "ClientInformation.h"
37
38namespace orxonox
39{
40
41  ServerConnection::ServerConnection():
42    bListening_(false)
43  {
44    this->bindAddress_ = new ENetAddress();
45    this->bindAddress_->host = ENET_HOST_ANY;
46    this->bindAddress_->port = NETWORK_PORT;
47  }
48
49  ServerConnection::~ServerConnection(){
50    if ( this->bListening_ )
51      closeListener();
52    delete this->bindAddress_;
53  }
54
55  void ServerConnection::setBindAddress( const std::string& bindAddress ) {
56    enet_address_set_host (this->bindAddress_, bindAddress.c_str());
57  }
58
59  void ServerConnection::setPort( unsigned int port ) {
60      this->bindAddress_->port = port;
61  }
62
63  bool ServerConnection::openListener() {
64    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0);
65    if ( this->host_ == NULL )
66      return false;
67    else
68      return true;
69  }
70
71  bool ServerConnection::closeListener() {
72    this->bListening_=false;
73    disconnectClients();
74    enet_host_destroy(this->host_);
75    return true;
76  }
77
78  bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
79    if ( clientID == CLIENTID_UNKNOWN )
80    {
81      return addPacketAll(packet);
82    }
83    else
84    {
85      ClientInformation *temp = ClientInformation::findClient(clientID);
86      if(!temp){
87        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
88        return false;
89      }
90      return Connection::addPacket(packet, temp->getPeer());
91    }
92  }
93
94  bool ServerConnection::addPacketAll(ENetPacket *packet) {
95    if ( !Connection::getInstance() )
96      return false;
97    enet_host_broadcast( Connection::getInstance()->getHost(), 0, packet);
98    return true;
99  }
100 
101  void ServerConnection::disconnectClient(ClientInformation *client)
102  {
103    Connection::disconnectPeer( client->getPeer() );
104    delete client;
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      disconnectClient(client);
121  }
122
123  void ServerConnection::disconnectClients() {
124    ENetEvent event;
125    ClientInformation *temp = ClientInformation::getBegin();
126    while(temp!=0){
[3312]127      disconnectClient( temp );
[3214]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.