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 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | |
---|
42 | // TODO: Calls to the Connection superclass are done as follows: |
---|
43 | // Connection::startCommunicationThread() |
---|
44 | // However, these methods are not overridden. |
---|
45 | // Why can't we just do this->startCommunicationThread()? |
---|
46 | |
---|
47 | /** |
---|
48 | * Constructor |
---|
49 | */ |
---|
50 | ServerConnection::ServerConnection(): |
---|
51 | bListening_(false) |
---|
52 | { |
---|
53 | this->bindAddress_ = new ENetAddress(); |
---|
54 | this->bindAddress_->host = ENET_HOST_ANY; |
---|
55 | this->bindAddress_->port = NETWORK_PORT; |
---|
56 | this->bindAddress_->scopeID = 0; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Destructor |
---|
61 | */ |
---|
62 | ServerConnection::~ServerConnection() |
---|
63 | { |
---|
64 | if (this->bListening_) |
---|
65 | { |
---|
66 | this->closeListener(); |
---|
67 | } |
---|
68 | delete this->bindAddress_; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Set address on which to listen. |
---|
73 | * @param bindAddress The address on which to listen |
---|
74 | */ |
---|
75 | void ServerConnection::setBindAddress(const std::string& bindAddress) |
---|
76 | { |
---|
77 | if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0) |
---|
78 | { |
---|
79 | orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Set port on which to listen on. |
---|
85 | * @param port The port on which to listen on. |
---|
86 | */ |
---|
87 | void ServerConnection::setPort(unsigned int port) { |
---|
88 | this->bindAddress_->port = port; |
---|
89 | } |
---|
90 | |
---|
91 | bool ServerConnection::openListener() |
---|
92 | { |
---|
93 | // create host |
---|
94 | this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); |
---|
95 | |
---|
96 | if (this->host_ == nullptr) |
---|
97 | { |
---|
98 | orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl; |
---|
99 | return false; |
---|
100 | } |
---|
101 | |
---|
102 | // enable compression |
---|
103 | this->enableCompression(); |
---|
104 | |
---|
105 | // ensure that either IPv4 or IPv6 succeeded |
---|
106 | assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); |
---|
107 | |
---|
108 | if (this->host_->socket4 == ENET_SOCKET_NULL) |
---|
109 | { |
---|
110 | orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl; |
---|
111 | } |
---|
112 | else if (this->host_->socket6 == ENET_SOCKET_NULL) |
---|
113 | { |
---|
114 | orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl; |
---|
119 | } |
---|
120 | |
---|
121 | // start communication thread |
---|
122 | Connection::startCommunicationThread(); |
---|
123 | |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Stop listening. |
---|
129 | */ |
---|
130 | bool ServerConnection::closeListener() |
---|
131 | { |
---|
132 | this->bListening_ = false; |
---|
133 | disconnectClients(); |
---|
134 | Connection::stopCommunicationThread(); |
---|
135 | enet_host_destroy(this->host_); |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Add outgoing packet to queue. |
---|
141 | * @param packet The packet to send |
---|
142 | * @param clientID The ID of the recipient |
---|
143 | * @param channelID The channel ID |
---|
144 | * TODO: Not sure yet how this actually works |
---|
145 | */ |
---|
146 | void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) |
---|
147 | { |
---|
148 | if (clientID == NETWORK_PEER_ID_BROADCAST) |
---|
149 | { |
---|
150 | broadcastPacket(packet, channelID); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | Connection::addPacket(packet, clientID, channelID); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Terminate connection with a peer. |
---|
160 | * @param clientID The peer with which to terminate the connection. |
---|
161 | */ |
---|
162 | void ServerConnection::disconnectClient(int clientID) |
---|
163 | { |
---|
164 | Connection::disconnectPeer(clientID); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Disconnect all peers. |
---|
169 | */ |
---|
170 | void ServerConnection::disconnectClients() |
---|
171 | { |
---|
172 | Connection::disconnectPeers(); |
---|
173 | Connection::waitOutgoingQueue(); |
---|
174 | return; |
---|
175 | } |
---|
176 | } |
---|