Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed some warnings

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