Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12161 was 12027, checked in by merholzl, 6 years ago

Merged Masterserver, refresh button had to be removed

  • Property svn:eol-style set to native
File size: 4.2 KB
RevLine 
[3214]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>
[5929]33#define WIN32_LEAN_AND_MEAN
[3214]34#include <enet/enet.h>
35
[8858]36#include "util/Output.h"
[8327]37#include <util/Sleep.h>
[3214]38
39namespace orxonox
40{
[12027]41  /**
42   * Constructor
43   */
[3214]44  ServerConnection::ServerConnection():
45    bListening_(false)
46  {
47    this->bindAddress_ = new ENetAddress();
48    this->bindAddress_->host = ENET_HOST_ANY;
49    this->bindAddress_->port = NETWORK_PORT;
[8327]50    this->bindAddress_->scopeID = 0;
[3214]51  }
52
[12027]53  /**
54   * Destructor
55   */
[7801]56  ServerConnection::~ServerConnection()
57  {
[12027]58    if (this->bListening_)
59    {
60      this->closeListener();
61    }
[3214]62    delete this->bindAddress_;
63  }
64
[12027]65  /**
66   * Set address on which to listen.
67   * @param bindAddress The address on which to listen
68   */
69  void ServerConnection::setBindAddress(const std::string& bindAddress)
[7801]70  {
[12027]71    if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0)
72    {
73      orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl;
74    }
[3214]75  }
76
[12027]77  /**
78   * Set port on which to listen on.
79   * @param port The port on which to listen on.
80   */
81  void ServerConnection::setPort(unsigned int port) {
[3214]82      this->bindAddress_->port = port;
83  }
84
[7801]85  bool ServerConnection::openListener()
86  {
87    // create host
88    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0);
89   
[12027]90    if (this->host_ == nullptr)
[7459]91    {
[11071]92        orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl;
[7459]93        return false;
94    }
[7801]95   
96    // enable compression
97    this->enableCompression();
[12027]98
99    // ensure that either IPv4 or IPv6 succeeded
[7459]100    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
[12027]101
[7459]102    if (this->host_->socket4 == ENET_SOCKET_NULL)
[12027]103    {
[8858]104        orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl;
[12027]105    }
[7459]106    else if (this->host_->socket6 == ENET_SOCKET_NULL)
[12027]107    {
[8858]108        orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl;
[12027]109    }
[3214]110    else
[12027]111    {
[8858]112        orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl;
[12027]113    }
[7801]114   
115    // start communication thread
116    Connection::startCommunicationThread();
[7459]117
118    return true;
[3214]119  }
120
[12027]121  /**
122   * Stop listening.
123   */
[7801]124  bool ServerConnection::closeListener()
125  {
[12027]126    this->bListening_ = false;
127    this->disconnectClients();
[7801]128    Connection::stopCommunicationThread();
[3214]129    enet_host_destroy(this->host_);
130    return true;
131  }
132
[12027]133  /**
134   * Add outgoing packet to queue.
135   * @param packet The packet to send
136   * @param clientID The ID of the recipient
137   * @param channelID The channel ID
138   */
[7801]139  void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID)
140  {
[12027]141    if (clientID == NETWORK_PEER_ID_BROADCAST)
[3214]142    {
[12027]143      this->broadcastPacket(packet, channelID);
[3214]144    }
145    else
146    {
[8327]147      Connection::addPacket(packet, clientID, channelID);
[3214]148    }
149  }
150
[12027]151  /**
152   * Terminate connection with a peer.
153   * @param clientID The peer with which to terminate the connection.
154   */
[7801]155  void ServerConnection::disconnectClient(int clientID)
156  {
[8358]157    Connection::disconnectPeer(clientID);
[3214]158  }
159
[12027]160  /**
161   * Disconnect all peers.
162   */
[7801]163  void ServerConnection::disconnectClients()
164  {
[8327]165    Connection::disconnectPeers();
166    Connection::waitOutgoingQueue();
[3214]167    return;
168  }
169}
Note: See TracBrowser for help on using the repository browser.