[1502] | 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: |
---|
[3045] | 23 | * Oliver Scheuss |
---|
[1502] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | // |
---|
| 30 | // C++ Interface: ClientConnection |
---|
| 31 | // |
---|
| 32 | // Description: The Class ClientConnection manages the servers conenctions to the clients. |
---|
| 33 | // each connection is provided by a new process. communication between master process and |
---|
| 34 | // connection processes is provided by ... |
---|
| 35 | // |
---|
| 36 | // |
---|
| 37 | // Author: Oliver Scheuss |
---|
| 38 | // |
---|
| 39 | |
---|
| 40 | #include "ClientConnection.h" |
---|
| 41 | |
---|
[2773] | 42 | #include <enet/enet.h> |
---|
[1502] | 43 | #include <iostream> |
---|
[2990] | 44 | #include <cassert> |
---|
[1502] | 45 | // boost.thread library for multithreading support |
---|
[1755] | 46 | #include <boost/thread/thread.hpp> |
---|
[1502] | 47 | #include <boost/bind.hpp> |
---|
[2773] | 48 | #include <boost/thread/recursive_mutex.hpp> |
---|
[1502] | 49 | |
---|
| 50 | #include "util/Sleep.h" |
---|
[1747] | 51 | #include "util/Debug.h" |
---|
[1502] | 52 | |
---|
[2171] | 53 | namespace orxonox |
---|
[1502] | 54 | { |
---|
| 55 | //static boost::thread_group network_threads; |
---|
| 56 | |
---|
[2773] | 57 | static boost::recursive_mutex enet_mutex_g; |
---|
[1502] | 58 | |
---|
[2087] | 59 | ClientConnection::ClientConnection(int port, const std::string& address) { |
---|
[2990] | 60 | quit_=false; |
---|
[1502] | 61 | server=NULL; |
---|
[2773] | 62 | serverAddress = new ENetAddress(); |
---|
| 63 | enet_address_set_host(serverAddress, address.c_str()); |
---|
| 64 | serverAddress->port = port; |
---|
[1502] | 65 | established=false; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | ClientConnection::ClientConnection(int port, const char *address) { |
---|
[2990] | 69 | quit_=false; |
---|
[1502] | 70 | server=NULL; |
---|
[2773] | 71 | serverAddress = new ENetAddress(); |
---|
| 72 | enet_address_set_host(serverAddress, address); |
---|
| 73 | serverAddress->port = port; |
---|
[1502] | 74 | established=false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | bool ClientConnection::waitEstablished(int milisec) { |
---|
| 78 | for(int i=0; i<=milisec && !established; i++) |
---|
[2087] | 79 | msleep(1); |
---|
[1502] | 80 | |
---|
| 81 | return established; |
---|
| 82 | } |
---|
| 83 | |
---|
[1907] | 84 | ClientConnection::~ClientConnection(){ |
---|
| 85 | if(established) |
---|
| 86 | closeConnection(); |
---|
[2773] | 87 | delete serverAddress; // surely was created |
---|
[1502] | 88 | } |
---|
| 89 | |
---|
| 90 | ENetEvent *ClientConnection::getEvent(){ |
---|
| 91 | if(!buffer.isEmpty()) |
---|
| 92 | return buffer.pop(); |
---|
| 93 | else |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | bool ClientConnection::queueEmpty() { |
---|
| 98 | return buffer.isEmpty(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | bool ClientConnection::createConnection() { |
---|
| 102 | receiverThread_ = new boost::thread(boost::bind(&ClientConnection::receiverThread, this)); |
---|
| 103 | //network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); |
---|
| 104 | // wait 10 seconds for the connection to be established |
---|
[1534] | 105 | return waitEstablished(NETWORK_CLIENT_CONNECT_TIMEOUT); |
---|
[1502] | 106 | } |
---|
| 107 | |
---|
| 108 | bool ClientConnection::closeConnection() { |
---|
[2990] | 109 | quit_=true; |
---|
[1502] | 110 | //network_threads.join_all(); |
---|
| 111 | receiverThread_->join(); |
---|
| 112 | established=false; |
---|
| 113 | return true; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | bool ClientConnection::addPacket(ENetPacket *packet) { |
---|
| 118 | if(server==NULL) |
---|
| 119 | return false; |
---|
| 120 | if(packet==NULL){ |
---|
| 121 | COUT(3) << "Cl.con: addpacket: invalid packet" << std::endl; |
---|
| 122 | return false; |
---|
| 123 | } |
---|
[2773] | 124 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1502] | 125 | if(enet_peer_send(server, 0, packet)<0) |
---|
| 126 | return false; |
---|
| 127 | else |
---|
| 128 | return true; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | bool ClientConnection::sendPackets() { |
---|
| 132 | if(server==NULL) |
---|
| 133 | return false; |
---|
[2773] | 134 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1502] | 135 | enet_host_flush(client); |
---|
| 136 | lock.unlock(); |
---|
| 137 | return true; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void ClientConnection::receiverThread() { |
---|
| 141 | // what about some error-handling here ? |
---|
| 142 | atexit(enet_deinitialize); |
---|
| 143 | ENetEvent *event; |
---|
| 144 | { |
---|
[2773] | 145 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1502] | 146 | enet_initialize(); |
---|
| 147 | client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); |
---|
| 148 | lock.unlock(); |
---|
| 149 | } |
---|
| 150 | if(client==NULL) { |
---|
| 151 | COUT(2) << "ClientConnection: could not create client host" << std::endl; |
---|
| 152 | // add some error handling here ========================== |
---|
[2990] | 153 | quit_=true; |
---|
[1502] | 154 | } |
---|
| 155 | //connect to the server |
---|
| 156 | if(!establishConnection()){ |
---|
| 157 | COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl; |
---|
[2990] | 158 | quit_=true; |
---|
[1502] | 159 | return; |
---|
| 160 | } |
---|
| 161 | event = new ENetEvent; |
---|
| 162 | //main loop |
---|
[2990] | 163 | while(!quit_){ |
---|
[1502] | 164 | //std::cout << "connection loop" << std::endl; |
---|
| 165 | { |
---|
[2773] | 166 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1534] | 167 | if(enet_host_service(client, event, NETWORK_CLIENT_WAIT_TIME)<0){ |
---|
[1502] | 168 | // we should never reach this point |
---|
[2990] | 169 | // assert(0); |
---|
| 170 | printf("ClientConnection: ENet returned with an error!\n"); |
---|
| 171 | quit_=true; |
---|
| 172 | break; |
---|
[1502] | 173 | // add some error handling here ======================== |
---|
| 174 | } |
---|
| 175 | lock.unlock(); |
---|
| 176 | } |
---|
| 177 | switch(event->type){ |
---|
| 178 | // log handling ================ |
---|
| 179 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 180 | break; |
---|
| 181 | case ENET_EVENT_TYPE_RECEIVE: |
---|
[2662] | 182 | //COUT(5) << "Cl.Con: receiver-Thread while loop: got new packet" << std::endl; |
---|
[1502] | 183 | if ( !processData(event) ) COUT(2) << "Current packet was not pushed to packetBuffer -> ev ongoing SegFault" << std::endl; |
---|
[2662] | 184 | //COUT(5) << "Cl.Con: processed Data in receiver-thread while loop" << std::endl; |
---|
[1502] | 185 | event = new ENetEvent; |
---|
| 186 | break; |
---|
| 187 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
[2990] | 188 | quit_=true; |
---|
| 189 | printf("Received disconnect Packet from Server!\n"); |
---|
[1502] | 190 | // server closed the connection |
---|
| 191 | return; |
---|
| 192 | break; |
---|
| 193 | case ENET_EVENT_TYPE_NONE: |
---|
| 194 | //receiverThread_->yield(); |
---|
[2087] | 195 | msleep(1); |
---|
[1502] | 196 | break; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | // now disconnect |
---|
| 200 | |
---|
| 201 | if(!disconnectConnection()) |
---|
| 202 | // if disconnecting failed destroy conn. |
---|
[2773] | 203 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1502] | 204 | enet_peer_reset(server); |
---|
| 205 | return; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | bool ClientConnection::disconnectConnection() { |
---|
| 209 | ENetEvent event; |
---|
[3003] | 210 | if(this->quit_) |
---|
| 211 | return true; |
---|
[2773] | 212 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
[1502] | 213 | enet_peer_disconnect(server, 0); |
---|
[2990] | 214 | while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME) >= 0){ |
---|
[1502] | 215 | switch (event.type) |
---|
| 216 | { |
---|
| 217 | case ENET_EVENT_TYPE_NONE: |
---|
| 218 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 219 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 220 | enet_packet_destroy(event.packet); |
---|
| 221 | break; |
---|
| 222 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 223 | return true; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | enet_peer_reset(server); |
---|
| 227 | return false; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | bool ClientConnection::establishConnection() { |
---|
| 231 | ENetEvent event; |
---|
| 232 | // connect to peer (server is type ENetPeer*) |
---|
[2773] | 233 | boost::recursive_mutex::scoped_lock lock(enet_mutex_g); |
---|
| 234 | server = enet_host_connect(client, serverAddress, NETWORK_CLIENT_CHANNELS); |
---|
[1502] | 235 | if(server==NULL) { |
---|
| 236 | COUT(2) << "ClientConnection: server == NULL" << std::endl; |
---|
| 237 | // error handling |
---|
| 238 | return false; |
---|
| 239 | } |
---|
| 240 | // handshake |
---|
[2990] | 241 | while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit_){ |
---|
[1534] | 242 | if( event.type == ENET_EVENT_TYPE_CONNECT ){ |
---|
| 243 | established=true; |
---|
| 244 | return true; |
---|
| 245 | } |
---|
[1502] | 246 | } |
---|
[1534] | 247 | COUT(2) << "ClientConnection: enet_host_service < 0 or event.type != ENET_EVENT_TYPE_CONNECT # EVENT:" << event.type << std::endl; |
---|
| 248 | return false; |
---|
[1502] | 249 | } |
---|
| 250 | |
---|
| 251 | bool ClientConnection::processData(ENetEvent *event) { |
---|
| 252 | COUT(5) << "Cl.Con: got packet, pushing to queue" << std::endl; |
---|
| 253 | // just add packet to the buffer |
---|
| 254 | // this can be extended with some preprocessing |
---|
| 255 | return buffer.push(event); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | } |
---|