Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network5/src/libraries/network/ServerConnection.cc @ 7772

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

network is now multithreaded again
further testing needed

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