Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/ClientConnection.cc @ 5190

Last change on this file since 5190 was 3214, checked in by scheusso, 15 years ago

merged netp5 back to trunk

  • Property svn:eol-style set to native
File size: 4.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>
[2773]32#include <enet/enet.h>
[1747]33#include "util/Debug.h"
[1502]34
[2171]35namespace orxonox
[1502]36{
[3214]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;
[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
49    enet_address_set_host(this->serverAddress_, "127.0.0.1");
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 ) {
60    enet_address_set_host (this->serverAddress_, serverAddress.c_str());
[1502]61  }
62
[3214]63  void ClientConnection::setPort( unsigned int port ) {
64    this->serverAddress_->port = port;
[1502]65  }
66
[3214]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
[1502]76      return false;
[3214]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
[1502]83      return false;
84    }
[3214]85    // handshake
86    for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++ )
[1502]87    {
[3214]88      if( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && event.type == ENET_EVENT_TYPE_CONNECT )
[1502]89      {
[3214]90        this->established_=true;
91        return true;
[1502]92      }
93    }
[3214]94    COUT(1) << "Could not connect to server" << endl;
95    return false;
[1502]96  }
97
[3214]98  bool ClientConnection::closeConnection() {
[1502]99    ENetEvent event;
[3214]100   
101    if ( !this->established_ )
[3084]102      return true;
[3214]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 )
[1502]108      {
[3214]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        }
[1502]121      }
122    }
[3214]123    enet_peer_reset( this->server_ );
[1502]124    return false;
125  }
126
[3214]127
128  bool ClientConnection::addPacket(ENetPacket *packet) {
129    assert( this->server_ );
130    assert( packet );
131    return Connection::addPacket( packet, this->server_ );
[1502]132  }
133
[3214]134  void ClientConnection::addClient(ENetEvent* event)
135  {
136    assert(0);
[1502]137  }
[3214]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  }
[1502]144
145}
Note: See TracBrowser for help on using the repository browser.