Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/network/MasterServerComm.cc @ 10219

Last change on this file since 10219 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: 7.6 KB
RevLine 
[7589]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 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[7611]29#include "MasterServerComm.h"
[8858]30#include "util/Output.h"
31#include "WANDiscovery.h"
[7589]32
[7631]33namespace orxonox
[7589]34{
[7768]35
[7631]36  MasterServerComm::MasterServerComm()
37  { /* nothing anymore, everything's been outsourced to
38     * the initialize method to facilitate debugging
39     */
[7768]40    /* register object in orxonox */
[7631]41  } 
[7589]42
[7631]43  int MasterServerComm::initialize()
44  {
45    /* initialize Enet */
[7763]46    if( enet_initialize () != 0 )
[8858]47    { orxout(internal_error, context::master_server) << "An error occurred while initializing ENet." << endl;
[7631]48      return 1;
49    }
[7684]50
51    /* initialize the event holder */
[7801]52//     this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
[7631]53   
[7589]54
[7631]55    /* initiate the client */
56    this->client = enet_host_create( NULL /* create a client host */,
57        1,
58        2, /* allow up 2 channels to be used, 0 and 1 */
59        0, 
60        0 ); 
61
62    /* see if it worked */
63    if (this->client == NULL)
[8858]64    { orxout(internal_error, context::master_server) << "An error occurred while trying to create an " 
65        << "ENet client host." << endl;
[7631]66      return 1;
67    }
[7632]68
69    return 0;
[7589]70  }
71
[7631]72  MasterServerComm::~MasterServerComm()
73  {
74    /* destroy the enet facilities */
75    enet_host_destroy(this->client);
76  }
[7589]77
[7725]78  int MasterServerComm::connect( const char *address, unsigned int port )
[7631]79  {
80    /* Connect to address:port. */
81    enet_address_set_host( &this->address, address );
82    this->address.port = port;
[7589]83
[7631]84    /* Initiate the connection, allocating the two channels 0 and 1. */
85    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
[7589]86
[7763]87    if( this->peer == NULL )
[8858]88    { orxout(internal_error, context::master_server) << "No available peers for initiating an ENet"
89        << " connection." << endl;
[7756]90      return -1;
[7631]91    }
[7589]92
[7631]93    /* Wait up to 2 seconds for the connection attempt to succeed. */
[7801]94    if (enet_host_service (this->client, &this->event, 500) > 0 &&
95        this->event.type == ENET_EVENT_TYPE_CONNECT )
[8858]96      orxout(internal_info, context::master_server) << "Connection to master server succeeded." << endl;
[7631]97    else
98    {
99      enet_peer_reset (this->peer);
[8858]100      orxout(internal_warning, context::master_server) << "Connection to " << address << " failed." << endl;
[7631]101      return -1;
102    }
103
[7756]104    /* all fine */
[7631]105    return 0;
[7611]106  }
[7801]107 
108void MasterServerComm::update()
109{
110  while( enet_host_service( this->client, &this->event, 1 ) );
111}
[7589]112
[7801]113
[7761]114  int MasterServerComm::disconnect( void )
115  {
[7801]116    while( enet_host_service( this->client, &this->event, 1 ) );
[7761]117    enet_peer_disconnect( this->peer, 0 );
118
119    /* Allow up to 1 second for the disconnect to succeed
120     * and drop any packets received packets.
121     */
[7801]122    while (enet_host_service (this->client, &this->event, 1000) > 0)
[7761]123    {
[7801]124      switch (this->event.type)
[7761]125      {
126        case ENET_EVENT_TYPE_RECEIVE:
[7801]127          enet_packet_destroy (event.packet);
[7761]128          break;
129
130        case ENET_EVENT_TYPE_DISCONNECT:
[8858]131          orxout(verbose, context::master_server) << "Disconnect from master server successful." << endl; 
[7761]132          return 0;
133        default: break;
134      }
135    }
136
137    /* We've arrived here, so the disconnect attempt didn't
138     * succeed yet, hence: force the connection down.           
139     */
140    enet_peer_reset( this->peer );
141
142    /* done */
143    return 0;
144  }
145
[7756]146  /* NOTE this is to be reimplemented soon to return
147   * a structure containing
148   * - addrconv
149   * - the event
150   * so we can also make callbacks from objects
151   */
[8858]152  int MasterServerComm::pollForReply( WANDiscovery* listener, int delayms )
[7611]153  { 
[7631]154    /* see whether anything happened */
[7668]155    /* WORK MARK REMOVE THIS OUTPUT */
[8858]156    orxout(verbose, context::master_server) << "polling masterserver..." << endl;
[7668]157
[7692]158    /* address buffer */
159    char *addrconv = NULL;
160    int retval = 0;
161
[7672]162    /* enet_host_service returns 0 if no event occured */
163    /* just newly set below test to >0 from >= 0, to be tested */
[7801]164    if( enet_host_service( this->client, &this->event, delayms ) > 0 )
[7631]165    { 
166      /* check what type of event it is and react accordingly */
[7801]167      switch (this->event.type)
[7631]168      { /* new connection, not supposed to happen. */
169        case ENET_EVENT_TYPE_CONNECT: break;
[7611]170
[7631]171        /* disconnect */
172        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
173
[7611]174        /* incoming data */
[7631]175        case ENET_EVENT_TYPE_RECEIVE: 
176          addrconv = (char *) calloc( 50, 1 );
[7692]177          if( !addrconv ) 
[8858]178          { orxout(internal_warning, context::master_server) << "MasterServerComm.cc: Could not allocate memory!" << endl;
[7692]179            break;
180          }
181
182          /* resolve IP */
[7801]183          enet_address_get_host_ip( &(this->event.peer->address), 
[7684]184            addrconv, 49 );
[7611]185
[7631]186          /* DEBUG */
[8858]187          orxout(verbose, context::master_server) << "MasterServer Debug: A packet of length " 
[7801]188            << this->event.packet->dataLength
189            << " containing " << this->event.packet->data
[7725]190            << " was received from " << addrconv
[7801]191            << " on channel " << this->event.channelID;
[7631]192          /* END DEBUG */
[7611]193
[7631]194          /* call the supplied callback, if any. */
[8858]195          if( listener != NULL )
196            retval = listener->rhandler( addrconv, &(this->event) );
[7611]197
[7756]198          /* clean up */
[7801]199          enet_packet_destroy( event.packet );
[7756]200          if( addrconv ) 
201            free( addrconv );
202
[7631]203          break;
204        default: break;
205      }
206
207      /* event handled, return 0 */
[7650]208      return retval;
[7589]209    }
[7631]210
211    /* show that no event occured */
[7668]212    return 0;
[7589]213  }
[7611]214
[7725]215  int MasterServerComm::sendRequest( const char *data )
[7631]216  {
217    /* send the data to the friend */
218    /* Create a reliable packet of size 7 containing "packet\0" */
219    ENetPacket * packet = enet_packet_create( data, 
220        strlen( data ) + 1, 
221        ENET_PACKET_FLAG_RELIABLE);
[7611]222
[7631]223    /* Send the packet to the peer over channel id 0. */
224    enet_peer_send (this->peer, 0, packet);
[7611]225
[7631]226    /* One could just use enet_host_service() instead. */
227    enet_host_flush( this->client );
[7668]228   
[7692]229    /* free the packet */
[7801]230    // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
231//     enet_packet_destroy( packet );
[7632]232
233    /* all done. */
234    return 0;
[7631]235  }
[7611]236
[7650]237  int MasterServerComm::sendRequest( std::string data )
238  {
239    /* send the data to the friend */
240    /* Create a reliable packet of size 7 containing "packet\0" */
241    ENetPacket * packet = enet_packet_create( data.c_str(), 
242        data.length() + 1, 
243        ENET_PACKET_FLAG_RELIABLE);
244
245    /* Send the packet to the peer over channel id 0. */
246    enet_peer_send (this->peer, 0, packet);
247
248    /* One could just use enet_host_service() instead. */
249    enet_host_flush( this->client );
[7801]250    // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
251//     enet_packet_destroy( packet );
[7650]252
253    /* all done. */
254    return 0;
255  }
256
[7611]257}
Note: See TracBrowser for help on using the repository browser.