Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hoverHS15/src/libraries/network/LANDiscoverable.cc @ 10643

Last change on this file since 10643 was 10622, checked in by landauf, 9 years ago

merged branch presentationFS15merge back to trunk

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