Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/PresentationFS18/src/libraries/network/LANDiscoverable.cc @ 12141

Last change on this file since 12141 was 12020, checked in by patricwi, 6 years ago

Merged Masterserver

  • Property svn:eol-style set to native
File size: 3.6 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 "LANDiscoverable.h"
30
31#include <enet/enet.h>
32#include <cassert>
33#include <cstring>
34
35#include "util/Output.h"
36#include "packet/ServerInformation.h"
37#include "core/config/ConfigValueIncludes.h"
38#include "core/CoreIncludes.h"
39
40
41namespace orxonox
42{
43  const char* LAN_DISCOVERY_MESSAGE = "Orxonox Client";
44  const char* LAN_DISCOVERY_ACK     = "Orxonox Server Ack";
45
46  RegisterClassNoArgs(LANDiscoverable);
47
48  LANDiscoverable::LANDiscoverable() : bActive_(false), host_(nullptr)
49  {
50    /* register object in orxonox */
51    RegisterObject(LANDiscoverable);
52
53    this->setConfigValues();
54  }
55
56  void LANDiscoverable::setConfigValues()
57  {
58    /* update ownName string from orxonox.ini config file, if it
59     * has changed.
60     */
61    SetConfigValueExternal(ownName, "Discovery", "ownName", "OrxServer");
62  }
63
64  LANDiscoverable::~LANDiscoverable()
65  {
66    if( this->host_ )
67    {
68      enet_host_destroy( this->host_ );
69      assert( this->bActive_ );
70    }
71  }
72
73  void LANDiscoverable::setActivity(bool bActive)
74  {
75    if( bActive == this->bActive_ )        // no change
76      return;
77
78    if( bActive )
79    {
80      ENetAddress bindAddress;
81      memset(& bindAddress, 0, sizeof(ENetAddress));
82      bindAddress.host = ENET_HOST_ANY;
83      bindAddress.port = LAN_DISCOVERY_PORT;
84      assert( this->host_ == nullptr );
85      this->host_ = enet_host_create( &bindAddress, 10, 0, 0, 0 );
86      if ( this->host_ == nullptr )
87          orxout(internal_error, context::network) << "LANDiscoverable: host_ == nullptr" << endl;
88    }
89    else
90    {
91      enet_host_destroy( this->host_ );
92      this->host_ = nullptr;
93    }
94      this->bActive_ = bActive;
95  }
96
97  void LANDiscoverable::update()
98  {
99    ENetEvent event;
100
101    if( this->bActive_==false )
102      return;
103    assert(this->host_);
104
105    while( enet_host_service( this->host_, &event, 0 ) > 0 )
106    {
107      switch(event.type)
108      {
109        case ENET_EVENT_TYPE_CONNECT:
110            orxout(verbose, context::network) << "Received LAN discovery connect from client " << event.peer->host->receivedAddress << endl;
111            break;
112        case ENET_EVENT_TYPE_DISCONNECT:
113        case ENET_EVENT_TYPE_NONE:
114          break;
115        case ENET_EVENT_TYPE_RECEIVE:
116          if( strcmp( LAN_DISCOVERY_MESSAGE, (char*)event.packet->data ) == 0 )      // check for a suitable orxonox client
117          {
118            orxout(internal_info, context::network) << "Received LAN discovery message from client " << event.peer->host->receivedAddress << endl;
119            packet::ServerInformation info;
120            info.setServerName(this->ownName);
121            info.setClientNumber(this->clientNumber);
122            info.send(event.peer);
123            enet_host_flush(this->host_);
124          }
125          break;
126      }
127    }
128  }
129
130}
Note: See TracBrowser for help on using the repository browser.