Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/minigame4DHS14/src/libraries/network/LANDiscovery.cc @ 10255

Last change on this file since 10255 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 4.0 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 "LANDiscovery.h"
30
31#include <enet/enet.h>
32#include <cstring>
33
34#include "util/ScopedSingletonManager.h"
35#include "core/CoreIncludes.h"
36
37
38namespace orxonox
39{
40  ManageScopedSingleton(LANDiscovery, ScopeID::Graphics, true);
41
42  LANDiscovery::LANDiscovery()
43  {
44    this->host_ = enet_host_create(NULL, 10, 0, 0, 0 );
45    if ( this->host_ == NULL )
46        orxout(internal_error, context::network) << "LANDiscovery: host_ == NULL" << endl;
47  }
48
49  LANDiscovery::~LANDiscovery()
50  {
51    if (this->host_ != NULL)
52      enet_host_destroy(this->host_);
53  }
54
55  void LANDiscovery::discover()
56  {
57    this->servers_.clear();
58    ENetPeer* peer;
59    ENetAddress address;
60    memset(&address, 0, sizeof(ENetAddress));
61    address.port = LAN_DISCOVERY_PORT;
62
63    /* TODO: check for availability of each protocol */
64    /* IPv4 */
65    address.host = ENET_HOST_BROADCAST;
66    peer = enet_host_connect(this->host_, &address, 0, 0);
67    if (peer == NULL)
68        orxout(internal_error, context::network) << "Could not send LAN discovery to IPv4 Broadcast." << endl;
69
70    /* IPv6 */
71    enet_address_set_host(&address, "ff02::1"); // TODO: use a multicast group
72    peer = enet_host_connect(this->host_, &address, 0, 0);
73    if (peer == NULL)
74        orxout(internal_error, context::network) << "Could not send LAN discovery to IPv6 Multicast." << endl;
75
76    ENetEvent event;
77    while( enet_host_service(this->host_, &event, 1000 ) )
78    {
79      switch( event.type )
80      {
81        case ENET_EVENT_TYPE_CONNECT:
82        {
83          orxout(verbose, context::network) << "Received LAN discovery connect from server " << event.peer->host->receivedAddress << endl;
84          ENetPacket* packet = enet_packet_create(LAN_DISCOVERY_MESSAGE, strlen(LAN_DISCOVERY_MESSAGE)+1, ENET_PACKET_FLAG_RELIABLE);
85          enet_peer_send(event.peer, 0, packet);
86          break;
87        }
88        case ENET_EVENT_TYPE_RECEIVE:
89          {
90            packet::ServerInformation info(&event);
91            orxout(internal_info, context::network) << "Received LAN discovery server information; Name: " << info.getServerName() << ", Address: " << info.getServerIP() << ", RTT: " << info.getServerRTT() << endl;
92            std::vector<packet::ServerInformation>::iterator it;
93            for( it=this->servers_.begin(); it!=this->servers_.end(); ++it )
94            {
95              if( (*it).getServerIP() == info.getServerIP() )
96                break;
97            }
98            if( it==this->servers_.end() )
99              this->servers_.push_back(info);
100          }
101//           enet_address_get_host_ip(&event.peer->address, buffer, buflen );
102//           serverIPs.push_back(std::string(buffer));
103          break;
104        default:
105          break;
106      }
107    }
108  }
109
110  std::string LANDiscovery::getServerListItemName(unsigned int index)
111  {
112    if( index >= this->servers_.size() )
113      return BLANKSTRING;
114    else
115      return this->servers_[index].getServerName();
116  }
117
118  std::string LANDiscovery::getServerListItemIP(unsigned int index)
119  {
120    if( index >= this->servers_.size() )
121      return BLANKSTRING;
122    else
123      return this->servers_[index].getServerIP();
124  }
125
126
127} // namespace orxonox
Note: See TracBrowser for help on using the repository browser.