Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixing a bug that made a segfault/memory corruption/corrupted double linked list in some cases of client disconnect

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