Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/ServerConnection.cc @ 3201

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

cleaned up server connection handling
client is yet to come

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