Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7459 was 7459, checked in by adrfried, 14 years ago

Merged ipv6 branch

Orxonox now includes a modified version of ENet 1.3.0

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
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 "ClientConnection.h"
30
31#include <cassert>
32#define WIN32_LEAN_AND_MEAN
33#include <enet/enet.h>
34#include "util/Debug.h"
35
36namespace orxonox
37{
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 = 1;
41  const unsigned int NETWORK_CLIENT_CHANNELS = 1;
42
43
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"); // TODO: check for IPv6 and connect to ::1 instead
51    serverAddress_->port = NETWORK_PORT;
52  }
53
54  ClientConnection::~ClientConnection(){
55    if(this->established_)
56      closeConnection();
57    delete this->serverAddress_; // surely was created
58  }
59
60  void ClientConnection::setServerAddress( const std::string& serverAddress ) {
61    if (enet_address_set_host (this->serverAddress_, serverAddress.c_str()) < 0)
62        COUT(1) << "Error: Could not resolve \"" << serverAddress << "\"." << std::endl;
63  }
64
65  void ClientConnection::setPort( unsigned int port ) {
66    this->serverAddress_->port = port;
67  }
68
69  bool ClientConnection::establishConnection()
70  {
71    ENetEvent event;
72
73    this->host_ = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0, 0);
74    if ( this->host_ == NULL )
75    {
76      COUT(1) << "ClientConnection: host_ == NULL" << std::endl;
77      // error handling
78      return false;
79    }
80    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
81    if (this->host_->socket4 == ENET_SOCKET_NULL)
82        COUT(2) << "Warning: IPv4 Socket failed." << std::endl;
83    else if (this->host_->socket6 == ENET_SOCKET_NULL)
84        COUT(2) << "Warning: IPv6 Socket failed." << std::endl;
85    else
86        COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl;
87
88    this->server_ = enet_host_connect(this->host_, serverAddress_, NETWORK_CLIENT_CHANNELS, 0);
89    if ( this->server_==NULL )
90    {
91      COUT(1) << "ClientConnection: server_ == NULL" << std::endl;
92      // error handling
93      return false;
94    }
95    // handshake
96    for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++ )
97    {
98      if( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && event.type == ENET_EVENT_TYPE_CONNECT )
99      {
100        this->established_=true;
101        return true;
102      }
103    }
104    COUT(1) << "Could not connect to server" << endl;
105    return false;
106  }
107
108  bool ClientConnection::closeConnection() {
109    ENetEvent event;
110
111    if ( !this->established_ )
112      return true;
113    this->established_ = false;
114    enet_peer_disconnect(this->server_, 0);
115    for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++)
116    {
117      if ( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME) >= 0 )
118      {
119        switch (event.type)
120        {
121          case ENET_EVENT_TYPE_NONE:
122          case ENET_EVENT_TYPE_CONNECT:
123            break;
124          case ENET_EVENT_TYPE_RECEIVE:
125            enet_packet_destroy(event.packet);
126            break;
127          case ENET_EVENT_TYPE_DISCONNECT:
128            COUT(4) << "received disconnect confirmation from server" << endl;
129            this->connectionClosed();
130            return true;
131        }
132      }
133    }
134    enet_peer_reset( this->server_ );
135    this->connectionClosed();
136    return false;
137  }
138
139
140  bool ClientConnection::addPacket(ENetPacket *packet) {
141    assert( this->server_ );
142    assert( packet );
143    return Connection::addPacket( packet, this->server_ );
144  }
145
146  void ClientConnection::addPeer(ENetEvent* event)
147  {
148    assert(0);
149  }
150  void ClientConnection::removePeer(ENetEvent* event)
151  {
152    this->established_=false;
153    COUT(1) << "Received disconnect Packet from Server!" << endl;
154        // server closed the connection
155    this->connectionClosed();
156  }
157
158  uint32_t ClientConnection::getRTT()
159  {
160    if (server_)
161        return server_->roundTripTime;
162    else
163        return 0;
164  }
165
166}
Note: See TracBrowser for help on using the repository browser.