Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/libraries/network/ServerConnection.cc @ 8991

Last change on this file since 8991 was 8807, checked in by landauf, 13 years ago

Replaced COUT with orxout in network library. Tried to set levels and contexts in a more or less useful way, but not really optimized. Used contexts network, packets, and master_server.
Please use endl instead of \n in the future (@smerkli) ;)

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