Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/libraries/network/ServerConnection.cc @ 7363

Last change on this file since 7363 was 7295, checked in by adrfried, 14 years ago

some compatability changes for ipv6-patched enet

  • Property svn:eol-style set to native
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#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    memset(this->bindAddress_, 0, sizeof(ENetAddress));
47    this->bindAddress_->host = ENET_HOST_ANY;
48    this->bindAddress_->port = NETWORK_PORT;
49  }
50
51  ServerConnection::~ServerConnection(){
52    if ( this->bListening_ )
53      closeListener();
54    delete this->bindAddress_;
55  }
56
57  void ServerConnection::setBindAddress( const std::string& bindAddress ) {
58    enet_address_set_host (this->bindAddress_, bindAddress.c_str());
59  }
60
61  void ServerConnection::setPort( unsigned int port ) {
62      this->bindAddress_->port = port;
63  }
64
65  bool ServerConnection::openListener() {
66    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0, 0);
67    if ( this->host_ == NULL )
68      return false;
69    else
70      return true;
71  }
72
73  bool ServerConnection::closeListener() {
74    this->bListening_=false;
75    disconnectClients();
76    enet_host_destroy(this->host_);
77    return true;
78  }
79
80  bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
81    if ( clientID == CLIENTID_UNKNOWN )
82    {
83      return addPacketAll(packet);
84    }
85    else
86    {
87      ClientInformation *temp = ClientInformation::findClient(clientID);
88      if(!temp){
89        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
90        return false;
91      }
92      return Connection::addPacket(packet, temp->getPeer());
93    }
94  }
95
96  bool ServerConnection::addPacketAll(ENetPacket *packet) {
97//     if ( !Connection::getInstance() )
98//       return false;
99    enet_host_broadcast( Connection::getHost(), 0, packet);
100    return true;
101  }
102
103  void ServerConnection::disconnectClient(ClientInformation *client)
104  {
105    Connection::disconnectPeer( client->getPeer() );
106  }
107
108  void ServerConnection::disconnectClient(int clientID){
109    ClientInformation *client = ClientInformation::findClient(clientID);
110    if(client)
111      ServerConnection::disconnectClient(client);
112  }
113
114  void ServerConnection::disconnectClients() {
115    ENetEvent event;
116    ClientInformation *temp = ClientInformation::getBegin();
117    while(temp!=0){
118      ServerConnection::disconnectClient( temp );
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          removePeer( &event );
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.