Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/ClientConnection.cc @ 8324

Last change on this file since 8324 was 7801, checked in by dafrick, 14 years ago

Merging presentation2 branch back to trunk.

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[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]36namespace orxonox
[1502]37{
[3214]38  const unsigned int NETWORK_CLIENT_WAIT_TIME = 1;
39  const unsigned int NETWORK_CLIENT_CONNECTION_TIMEOUT = 3000; //millisecs
[5965]40  const unsigned int NETWORK_CLIENT_MAX_CONNECTIONS = 1;
[1502]41
42
[3214]43  ClientConnection::ClientConnection():
44    established_(false),
45    server_(NULL)
46  {
47    this->serverAddress_ = new ENetAddress();
48    //set standard address and port
[7459]49    enet_address_set_host(this->serverAddress_, "127.0.0.1"); // TODO: check for IPv6 and connect to ::1 instead
[3214]50    serverAddress_->port = NETWORK_PORT;
[1502]51  }
52
[1907]53  ClientConnection::~ClientConnection(){
[3214]54    if(this->established_)
[1907]55      closeConnection();
[3214]56    delete this->serverAddress_; // surely was created
[1502]57  }
58
[3214]59  void ClientConnection::setServerAddress( const std::string& serverAddress ) {
[7459]60    if (enet_address_set_host (this->serverAddress_, serverAddress.c_str()) < 0)
61        COUT(1) << "Error: Could not resolve \"" << serverAddress << "\"." << std::endl;
[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;
[6417]71
[7801]72    // create host
73    this->host_ = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0);
74   
[3214]75    if ( this->host_ == NULL )
76    {
[7459]77      COUT(1) << "ClientConnection: host_ == NULL" << std::endl;
[3214]78      // error handling
[1502]79      return false;
[3214]80    }
[7801]81   
82    // enable compression
83    this->enableCompression();
84   
[7459]85    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
86    if (this->host_->socket4 == ENET_SOCKET_NULL)
87        COUT(2) << "Warning: IPv4 Socket failed." << std::endl;
88    else if (this->host_->socket6 == ENET_SOCKET_NULL)
89        COUT(2) << "Warning: IPv6 Socket failed." << std::endl;
90    else
91        COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl;
92
[7801]93    this->server_ = enet_host_connect(this->host_, serverAddress_, NETWORK_CHANNEL_COUNT, 0);
[3214]94    if ( this->server_==NULL )
95    {
[7459]96      COUT(1) << "ClientConnection: server_ == NULL" << std::endl;
[3214]97      // error handling
[1502]98      return false;
99    }
[3214]100    // handshake
101    for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++ )
[1502]102    {
[3214]103      if( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && event.type == ENET_EVENT_TYPE_CONNECT )
[1502]104      {
[3214]105        this->established_=true;
[7801]106        Connection::startCommunicationThread();
[3214]107        return true;
[1502]108      }
109    }
[3214]110    COUT(1) << "Could not connect to server" << endl;
111    return false;
[1502]112  }
113
[3214]114  bool ClientConnection::closeConnection() {
[1502]115    ENetEvent event;
[6417]116
[3214]117    if ( !this->established_ )
[3084]118      return true;
[3214]119    this->established_ = false;
[7801]120    Connection::stopCommunicationThread();
[3214]121    enet_peer_disconnect(this->server_, 0);
122    for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++)
123    {
124      if ( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME) >= 0 )
[1502]125      {
[3214]126        switch (event.type)
127        {
128          case ENET_EVENT_TYPE_NONE:
129          case ENET_EVENT_TYPE_CONNECT:
130            break;
131          case ENET_EVENT_TYPE_RECEIVE:
132            enet_packet_destroy(event.packet);
133            break;
134          case ENET_EVENT_TYPE_DISCONNECT:
135            COUT(4) << "received disconnect confirmation from server" << endl;
[5929]136            this->connectionClosed();
[3214]137            return true;
138        }
[1502]139      }
140    }
[3214]141    enet_peer_reset( this->server_ );
[5929]142    this->connectionClosed();
[1502]143    return false;
144  }
145
[3214]146
[7801]147  void ClientConnection::addPacket(ENetPacket *packet, uint8_t channelID) {
[3214]148    assert( this->server_ );
149    assert( packet );
[7801]150    return Connection::addPacket( packet, this->server_, channelID );
[1502]151  }
152
[5929]153  void ClientConnection::addPeer(ENetEvent* event)
[3214]154  {
155    assert(0);
[1502]156  }
[5929]157  void ClientConnection::removePeer(ENetEvent* event)
[3214]158  {
159    this->established_=false;
160    COUT(1) << "Received disconnect Packet from Server!" << endl;
161        // server closed the connection
[7801]162    this->stopCommunicationThread();
[5929]163    this->connectionClosed();
[3214]164  }
[6417]165
[5961]166  uint32_t ClientConnection::getRTT()
[6417]167  {
[7284]168    if (server_)
169        return server_->roundTripTime;
170    else
171        return 0;
[5961]172  }
[1502]173
174}
Note: See TracBrowser for help on using the repository browser.