Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5738 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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