Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12394 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
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/Output.h"
37#include <util/Sleep.h>
38
39namespace orxonox
40{
41  /**
42   * Constructor
43   */
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;
50    this->bindAddress_->scopeID = 0;
51  }
52
53  /**
54   * Destructor
55   */
56  ServerConnection::~ServerConnection()
57  {
58    if (this->bListening_)
59    {
60      this->closeListener();
61    }
62    delete this->bindAddress_;
63  }
64
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)
70  {
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    }
75  }
76
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) {
82      this->bindAddress_->port = port;
83  }
84
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   
90    if (this->host_ == nullptr)
91    {
92        orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl;
93        return false;
94    }
95   
96    // enable compression
97    this->enableCompression();
98
99    // ensure that either IPv4 or IPv6 succeeded
100    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
101
102    if (this->host_->socket4 == ENET_SOCKET_NULL)
103    {
104        orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl;
105    }
106    else if (this->host_->socket6 == ENET_SOCKET_NULL)
107    {
108        orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl;
109    }
110    else
111    {
112        orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl;
113    }
114   
115    // start communication thread
116    Connection::startCommunicationThread();
117
118    return true;
119  }
120
121  /**
122   * Stop listening.
123   */
124  bool ServerConnection::closeListener()
125  {
126    this->bListening_ = false;
127    this->disconnectClients();
128    Connection::stopCommunicationThread();
129    enet_host_destroy(this->host_);
130    return true;
131  }
132
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   */
139  void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID)
140  {
141    if (clientID == NETWORK_PEER_ID_BROADCAST)
142    {
143      this->broadcastPacket(packet, channelID);
144    }
145    else
146    {
147      Connection::addPacket(packet, clientID, channelID);
148    }
149  }
150
151  /**
152   * Terminate connection with a peer.
153   * @param clientID The peer with which to terminate the connection.
154   */
155  void ServerConnection::disconnectClient(int clientID)
156  {
157    Connection::disconnectPeer(clientID);
158  }
159
160  /**
161   * Disconnect all peers.
162   */
163  void ServerConnection::disconnectClients()
164  {
165    Connection::disconnectPeers();
166    Connection::waitOutgoingQueue();
167    return;
168  }
169}
Note: See TracBrowser for help on using the repository browser.