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 | |
---|
39 | namespace 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 | { |
---|
53 | if ( this->bListening_ ) |
---|
54 | closeListener(); |
---|
55 | delete this->bindAddress_; |
---|
56 | } |
---|
57 | |
---|
58 | void ServerConnection::setBindAddress( const std::string& bindAddress ) |
---|
59 | { |
---|
60 | if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) |
---|
61 | COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl; |
---|
62 | } |
---|
63 | |
---|
64 | void ServerConnection::setPort( unsigned int port ) { |
---|
65 | this->bindAddress_->port = port; |
---|
66 | } |
---|
67 | |
---|
68 | bool ServerConnection::openListener() |
---|
69 | { |
---|
70 | // create host |
---|
71 | this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); |
---|
72 | |
---|
73 | if ( this->host_ == NULL ) |
---|
74 | { |
---|
75 | COUT(1) << "ServerConnection: host_ == NULL" << std::endl; |
---|
76 | return false; |
---|
77 | } |
---|
78 | |
---|
79 | // enable compression |
---|
80 | this->enableCompression(); |
---|
81 | assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); |
---|
82 | if (this->host_->socket4 == ENET_SOCKET_NULL) |
---|
83 | COUT(2) << "Warning: IPv4 Socket failed." << std::endl; |
---|
84 | else if (this->host_->socket6 == ENET_SOCKET_NULL) |
---|
85 | COUT(2) << "Warning: IPv6 Socket failed." << std::endl; |
---|
86 | else |
---|
87 | COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl; |
---|
88 | |
---|
89 | // start communication thread |
---|
90 | Connection::startCommunicationThread(); |
---|
91 | |
---|
92 | return true; |
---|
93 | } |
---|
94 | |
---|
95 | bool ServerConnection::closeListener() |
---|
96 | { |
---|
97 | this->bListening_=false; |
---|
98 | disconnectClients(); |
---|
99 | Connection::stopCommunicationThread(); |
---|
100 | enet_host_destroy(this->host_); |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) |
---|
105 | { |
---|
106 | if ( clientID == NETWORK_PEER_ID_BROADCAST ) |
---|
107 | { |
---|
108 | broadcastPacket(packet, channelID); |
---|
109 | } |
---|
110 | else |
---|
111 | { |
---|
112 | // ClientInformation *temp = ClientInformation::findClient(clientID); |
---|
113 | // if(!temp){ |
---|
114 | // COUT(3) << "C.Man: addPacket findClient failed" << std::endl; |
---|
115 | // } |
---|
116 | Connection::addPacket(packet, clientID, channelID); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | // void ServerConnection::disconnectClient(ClientInformation *client) |
---|
121 | // { |
---|
122 | // Connection::disconnectPeer( client->getPeer() ); |
---|
123 | // } |
---|
124 | |
---|
125 | void ServerConnection::disconnectClient(int clientID) |
---|
126 | { |
---|
127 | // ClientInformation *client = ClientInformation::findClient(clientID); |
---|
128 | // if(client) |
---|
129 | ServerConnection::disconnectClient(clientID); |
---|
130 | } |
---|
131 | |
---|
132 | void ServerConnection::disconnectClients() |
---|
133 | { |
---|
134 | Connection::disconnectPeers(); |
---|
135 | // ClientInformation *temp = ClientInformation::getBegin(); |
---|
136 | // while(temp!=0) |
---|
137 | // { |
---|
138 | // ServerConnection::disconnectClient( temp ); |
---|
139 | // temp = temp->next(); |
---|
140 | // } |
---|
141 | return; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | // int ServerConnection::getClientID(ENetPeer* peer) |
---|
146 | // { |
---|
147 | // return getClientID(&(peer->address)); |
---|
148 | // } |
---|
149 | |
---|
150 | // int ServerConnection::getClientID(ENetAddress* address) |
---|
151 | // { |
---|
152 | // return ClientInformation::findClient(address)->getID(); |
---|
153 | // } |
---|
154 | // |
---|
155 | // ENetPeer *ServerConnection::getClientPeer(int clientID) |
---|
156 | // { |
---|
157 | // return ClientInformation::findClient(clientID)->getPeer(); |
---|
158 | // } |
---|
159 | |
---|
160 | |
---|
161 | } |
---|