[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: |
---|
[3084] | 23 | * Oliver Scheuss |
---|
[1502] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ClientConnection.h" |
---|
| 30 | |
---|
[3214] | 31 | #include <cassert> |
---|
[5749] | 32 | #define WIN32_LEAN_AND_MEAN |
---|
[2773] | 33 | #include <enet/enet.h> |
---|
[1747] | 34 | #include "util/Debug.h" |
---|
[1502] | 35 | |
---|
[2171] | 36 | namespace orxonox |
---|
[1502] | 37 | { |
---|
[3214] | 38 | const unsigned int NETWORK_CLIENT_WAIT_TIME = 1; |
---|
| 39 | const unsigned int NETWORK_CLIENT_CONNECTION_TIMEOUT = 3000; //millisecs |
---|
| 40 | const unsigned int NETWORK_CLIENT_MAX_CONNECTIONS = 5; |
---|
| 41 | const unsigned int NETWORK_CLIENT_CHANNELS = 2; |
---|
[1502] | 42 | |
---|
| 43 | |
---|
[3214] | 44 | ClientConnection::ClientConnection(): |
---|
| 45 | established_(false), |
---|
| 46 | server_(NULL) |
---|
| 47 | { |
---|
| 48 | this->serverAddress_ = new ENetAddress(); |
---|
| 49 | //set standard address and port |
---|
| 50 | enet_address_set_host(this->serverAddress_, "127.0.0.1"); |
---|
| 51 | serverAddress_->port = NETWORK_PORT; |
---|
[1502] | 52 | } |
---|
| 53 | |
---|
[1907] | 54 | ClientConnection::~ClientConnection(){ |
---|
[3214] | 55 | if(this->established_) |
---|
[1907] | 56 | closeConnection(); |
---|
[3214] | 57 | delete this->serverAddress_; // surely was created |
---|
[1502] | 58 | } |
---|
| 59 | |
---|
[3214] | 60 | void ClientConnection::setServerAddress( const std::string& serverAddress ) { |
---|
| 61 | enet_address_set_host (this->serverAddress_, serverAddress.c_str()); |
---|
[1502] | 62 | } |
---|
| 63 | |
---|
[3214] | 64 | void ClientConnection::setPort( unsigned int port ) { |
---|
| 65 | this->serverAddress_->port = port; |
---|
[1502] | 66 | } |
---|
| 67 | |
---|
[3214] | 68 | bool ClientConnection::establishConnection() |
---|
| 69 | { |
---|
| 70 | ENetEvent event; |
---|
| 71 | |
---|
| 72 | this->host_ = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); |
---|
| 73 | if ( this->host_ == NULL ) |
---|
| 74 | { |
---|
| 75 | COUT(2) << "ClientConnection: host_ == NULL" << std::endl; |
---|
| 76 | // error handling |
---|
[1502] | 77 | return false; |
---|
[3214] | 78 | } |
---|
| 79 | this->server_ = enet_host_connect(this->host_, serverAddress_, NETWORK_CLIENT_CHANNELS); |
---|
| 80 | if ( this->server_==NULL ) |
---|
| 81 | { |
---|
| 82 | COUT(2) << "ClientConnection: server == NULL" << std::endl; |
---|
| 83 | // error handling |
---|
[1502] | 84 | return false; |
---|
| 85 | } |
---|
[3214] | 86 | // handshake |
---|
| 87 | for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++ ) |
---|
[1502] | 88 | { |
---|
[3214] | 89 | if( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && event.type == ENET_EVENT_TYPE_CONNECT ) |
---|
[1502] | 90 | { |
---|
[3214] | 91 | this->established_=true; |
---|
| 92 | return true; |
---|
[1502] | 93 | } |
---|
| 94 | } |
---|
[3214] | 95 | COUT(1) << "Could not connect to server" << endl; |
---|
| 96 | return false; |
---|
[1502] | 97 | } |
---|
| 98 | |
---|
[3214] | 99 | bool ClientConnection::closeConnection() { |
---|
[1502] | 100 | ENetEvent event; |
---|
[3214] | 101 | |
---|
| 102 | if ( !this->established_ ) |
---|
[3084] | 103 | return true; |
---|
[3214] | 104 | this->established_ = false; |
---|
| 105 | enet_peer_disconnect(this->server_, 0); |
---|
| 106 | for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++) |
---|
| 107 | { |
---|
| 108 | if ( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME) >= 0 ) |
---|
[1502] | 109 | { |
---|
[3214] | 110 | switch (event.type) |
---|
| 111 | { |
---|
| 112 | case ENET_EVENT_TYPE_NONE: |
---|
| 113 | case ENET_EVENT_TYPE_CONNECT: |
---|
| 114 | break; |
---|
| 115 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 116 | enet_packet_destroy(event.packet); |
---|
| 117 | break; |
---|
| 118 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 119 | COUT(4) << "received disconnect confirmation from server" << endl; |
---|
| 120 | return true; |
---|
| 121 | } |
---|
[1502] | 122 | } |
---|
| 123 | } |
---|
[3214] | 124 | enet_peer_reset( this->server_ ); |
---|
[1502] | 125 | return false; |
---|
| 126 | } |
---|
| 127 | |
---|
[3214] | 128 | |
---|
| 129 | bool ClientConnection::addPacket(ENetPacket *packet) { |
---|
| 130 | assert( this->server_ ); |
---|
| 131 | assert( packet ); |
---|
| 132 | return Connection::addPacket( packet, this->server_ ); |
---|
[1502] | 133 | } |
---|
| 134 | |
---|
[3214] | 135 | void ClientConnection::addClient(ENetEvent* event) |
---|
| 136 | { |
---|
| 137 | assert(0); |
---|
[1502] | 138 | } |
---|
[3214] | 139 | void ClientConnection::disconnectPeer(ENetEvent* event) |
---|
| 140 | { |
---|
| 141 | this->established_=false; |
---|
| 142 | COUT(1) << "Received disconnect Packet from Server!" << endl; |
---|
| 143 | // server closed the connection |
---|
| 144 | } |
---|
[1502] | 145 | |
---|
| 146 | } |
---|