Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/ClientConnection.cc @ 3203

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

removed some warnings

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