Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network6/src/libraries/network/ServerConnection.cc @ 7878

Last change on this file since 7878 was 7878, checked in by scheusso, 13 years ago

-some cleaning up
-fixing disconnect behaviour
-trying to find a bug

  • Property svn:eol-style set to native
File size: 4.3 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 "ServerConnection.h"
30
31#include <cassert>
32#include <string>
33#define WIN32_LEAN_AND_MEAN
34#include <enet/enet.h>
35
36#include "util/Debug.h"
37#include <util/Sleep.h>
38// #include "ClientInformation.h"
39
40namespace orxonox
41{
42
43  ServerConnection::ServerConnection():
44    bListening_(false)
45  {
46    this->bindAddress_ = new ENetAddress();
47    memset(this->bindAddress_, 0, sizeof(ENetAddress));
48    this->bindAddress_->host = ENET_HOST_ANY;
49    this->bindAddress_->port = NETWORK_PORT;
50  }
51
52  ServerConnection::~ServerConnection()
53  {
54    if ( this->bListening_ )
55      closeListener();
56    delete this->bindAddress_;
57  }
58
59  void ServerConnection::setBindAddress( const std::string& bindAddress )
60  {
61    if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0)
62        COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl;
63  }
64
65  void ServerConnection::setPort( unsigned int port ) {
66      this->bindAddress_->port = port;
67  }
68
69  bool ServerConnection::openListener()
70  {
71    // create host
72    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0);
73   
74    if ( this->host_ == NULL )
75    {
76        COUT(1) << "ServerConnection: host_ == NULL" << std::endl;
77        return false;
78    }
79   
80    // enable compression
81    this->enableCompression();
82    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
83    if (this->host_->socket4 == ENET_SOCKET_NULL)
84        COUT(2) << "Warning: IPv4 Socket failed." << std::endl;
85    else if (this->host_->socket6 == ENET_SOCKET_NULL)
86        COUT(2) << "Warning: IPv6 Socket failed." << std::endl;
87    else
88        COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl;
89   
90    // start communication thread
91    Connection::startCommunicationThread();
92
93    return true;
94  }
95
96  bool ServerConnection::closeListener()
97  {
98    this->bListening_=false;
99    disconnectClients();
100    Connection::stopCommunicationThread();
101    enet_host_destroy(this->host_);
102    return true;
103  }
104
105  void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID)
106  {
107    if ( clientID == NETWORK_PEER_ID_BROADCAST )
108    {
109      broadcastPacket(packet, channelID);
110    }
111    else
112    {
113//       ClientInformation *temp = ClientInformation::findClient(clientID);
114//       if(!temp){
115//         COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
116//       }
117      Connection::addPacket(packet, clientID, channelID);
118    }
119  }
120
121//   void ServerConnection::disconnectClient(ClientInformation *client)
122//   {
123//     Connection::disconnectPeer( client->getPeer() );
124//   }
125
126  void ServerConnection::disconnectClient(int clientID)
127  {
128//     ClientInformation *client = ClientInformation::findClient(clientID);
129//     if(client)
130    ServerConnection::disconnectClient(clientID);
131  }
132
133  void ServerConnection::disconnectClients()
134  {
135    Connection::disconnectPeers();
136    Connection::waitOutgoingQueue();
137    return;
138  }
139
140
141//   int ServerConnection::getClientID(ENetPeer* peer)
142//   {
143//     return getClientID(&(peer->address));
144//   }
145
146//   int ServerConnection::getClientID(ENetAddress* address)
147//   {
148//     return ClientInformation::findClient(address)->getID();
149//   }
150//
151//   ENetPeer *ServerConnection::getClientPeer(int clientID)
152//   {
153//     return ClientInformation::findClient(clientID)->getPeer();
154//   }
155
156
157}
Note: See TracBrowser for help on using the repository browser.