Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/ServerConnection.cc @ 6858

Last change on this file since 6858 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • 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    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  }
106
107  void ServerConnection::disconnectClient(int clientID){
108    ClientInformation *client = ClientInformation::findClient(clientID);
109    if(client)
110      ServerConnection::disconnectClient(client);
111  }
112
113  void ServerConnection::disconnectClients() {
114    ENetEvent event;
115    ClientInformation *temp = ClientInformation::getBegin();
116    while(temp!=0){
117      ServerConnection::disconnectClient( temp );
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          removePeer( &event );
133          temp = ClientInformation::getBegin();
134          break;
135        }
136      }
137    }
138    return;
139  }
140
141
142  int ServerConnection::getClientID(ENetPeer* peer) {
143    return getClientID(&(peer->address));
144  }
145
146  int ServerConnection::getClientID(ENetAddress* address) {
147    return ClientInformation::findClient(address)->getID();
148  }
149
150  ENetPeer *ServerConnection::getClientPeer(int clientID) {
151    return ClientInformation::findClient(clientID)->getPeer();
152  }
153
154
155}
Note: See TracBrowser for help on using the repository browser.