Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/libraries/network/ServerConnection.cc @ 7687

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

add some error messages in orxonox network code

  • Property svn:eol-style set to native
File size: 4.7 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 "ClientInformation.h"
38
39namespace orxonox
40{
41
42  ServerConnection::ServerConnection():
43    bListening_(false)
44  {
45    this->bindAddress_ = new ENetAddress();
46    memset(this->bindAddress_, 0, sizeof(ENetAddress));
47    this->bindAddress_->host = ENET_HOST_ANY;
48    this->bindAddress_->port = NETWORK_PORT;
49  }
50
51  ServerConnection::~ServerConnection(){
52    if ( this->bListening_ )
53      closeListener();
54    delete this->bindAddress_;
55  }
56
57  void ServerConnection::setBindAddress( const std::string& bindAddress ) {
58    if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0)
59        COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl;
60  }
61
62  void ServerConnection::setPort( unsigned int port ) {
63      this->bindAddress_->port = port;
64  }
65
66  bool ServerConnection::openListener() {
67    this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0, 0);
68    if ( this->host_ == NULL )
69    {
70        COUT(1) << "ServerConnection: host_ == NULL" << std::endl;
71        return false;
72    }
73    assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL );
74    if (this->host_->socket4 == ENET_SOCKET_NULL)
75        COUT(2) << "Warning: IPv4 Socket failed." << std::endl;
76    else if (this->host_->socket6 == ENET_SOCKET_NULL)
77        COUT(2) << "Warning: IPv6 Socket failed." << std::endl;
78    else
79        COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl;
80
81    return true;
82  }
83
84  bool ServerConnection::closeListener() {
85    this->bListening_=false;
86    disconnectClients();
87    enet_host_destroy(this->host_);
88    return true;
89  }
90
91  bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
92    if ( clientID == CLIENTID_UNKNOWN )
93    {
94      return addPacketAll(packet);
95    }
96    else
97    {
98      ClientInformation *temp = ClientInformation::findClient(clientID);
99      if(!temp){
100        COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
101        return false;
102      }
103      return Connection::addPacket(packet, temp->getPeer());
104    }
105  }
106
107  bool ServerConnection::addPacketAll(ENetPacket *packet) {
108//     if ( !Connection::getInstance() )
109//       return false;
110    enet_host_broadcast( Connection::getHost(), 0, packet);
111    return true;
112  }
113
114  void ServerConnection::disconnectClient(ClientInformation *client)
115  {
116    Connection::disconnectPeer( client->getPeer() );
117  }
118
119  void ServerConnection::disconnectClient(int clientID){
120    ClientInformation *client = ClientInformation::findClient(clientID);
121    if(client)
122      ServerConnection::disconnectClient(client);
123  }
124
125  void ServerConnection::disconnectClients() {
126    ENetEvent event;
127    ClientInformation *temp = ClientInformation::getBegin();
128    while(temp!=0){
129      ServerConnection::disconnectClient( temp );
130      temp = temp->next();
131    }
132    temp = ClientInformation::getBegin();
133    while( temp!=0 ){
134      if( service( &event ) )
135      {
136        switch (event.type)
137        {
138        case ENET_EVENT_TYPE_NONE: break;
139        case ENET_EVENT_TYPE_CONNECT: break;
140        case ENET_EVENT_TYPE_RECEIVE:
141          enet_packet_destroy(event.packet);
142          break;
143        case ENET_EVENT_TYPE_DISCONNECT:
144          removePeer( &event );
145          temp = ClientInformation::getBegin();
146          break;
147        }
148      }
149    }
150    return;
151  }
152
153
154  int ServerConnection::getClientID(ENetPeer* peer) {
155    return getClientID(&(peer->address));
156  }
157
158  int ServerConnection::getClientID(ENetAddress* address) {
159    return ClientInformation::findClient(address)->getID();
160  }
161
162  ENetPeer *ServerConnection::getClientPeer(int clientID) {
163    return ClientInformation::findClient(clientID)->getPeer();
164  }
165
166
167}
Note: See TracBrowser for help on using the repository browser.