Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

rest of the cleanup ( mostly client connection handling)
network is now single-threaded ( only in order to become multithreaded again, but thats another story ;) )

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