Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 10, 2010, 3:35:39 PM (14 years ago)
Author:
smerkli
Message:

added comments, lua function (to be tested) and various implementation bits

Location:
code/branches/masterserver/src/libraries/network
Files:
3 edited
2 moved

Legend:

Unmodified
Added
Removed
  • code/branches/masterserver/src/libraries/network/CMakeLists.txt

    r7490 r7631  
    3232  LANDiscoverable.cc
    3333  LANDiscovery.cc
     34  WANDiscovery.cc
     35  MasterServerComm.cc
    3436  NetworkFunction.cc
    3537  Host.cc
     
    5456  LANDiscoverable.h
    5557  LANDiscovery.h
     58  WANDiscovery.h
     59  MasterServerComm.h
    5660  NetworkFunction.h
    5761  NetworkPrecompiledHeaders.h
  • code/branches/masterserver/src/libraries/network/MasterServerComm.cc

    r7630 r7631  
    2727 */
    2828
    29 #include <cstdlib>
    30 #include <cstdio>
    31 #include <cstring>
    32 #include <enet/enet.h>
    3329#include "MasterServerComm.h"
    3430
    35 MasterServerComm::MasterServerComm()
     31namespace orxonox
    3632{
    37   /* initialize Enet */
    38   if (enet_initialize () != 0)
    39   { fprintf (stderr, "An error occurred while initializing ENet.\n");
    40     exit(EXIT_FAILURE);
    41   }
    42   atexit (enet_deinitialize);
    43 
    44 
    45   /* initiate the client */
    46   this->client = enet_host_create( NULL /* create a client host */,
    47       1,
    48       2, /* allow up 2 channels to be used, 0 and 1 */
    49       0,
    50       0 );
    51 
    52   /* see if it worked */
    53   if (this->client == NULL)
    54   { fprintf (stderr,
    55       "An error occurred while trying to create an ENet client host.\n");
    56     exit (EXIT_FAILURE);
    57   }
    58 }
    59 
    60 MasterServerComm::~MasterServerComm()
    61 {
    62   enet_host_destroy(this->client);
    63 }
    64 
    65 int
    66 MasterServerComm::connect( char *address, unsigned int port )
    67 {
    68   /* Connect to address:port. */
    69   enet_address_set_host( &this->address, address );
    70   this->address.port = port;
    71 
    72   /* Initiate the connection, allocating the two channels 0 and 1. */
    73   this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
    74 
    75   if (this->peer == NULL )
    76   { fprintf( stderr,
     33 
     34  MasterServerComm::MasterServerComm()
     35  { /* nothing anymore, everything's been outsourced to
     36     * the initialize method to facilitate debugging
     37     */
     38  }
     39
     40
     41  int MasterServerComm::initialize()
     42  {
     43    /* initialize Enet */
     44    if (enet_initialize () != 0)
     45    { COUT(1) << "An error occurred while initializing ENet.\n";
     46      return 1;
     47    }
     48   
     49    /* install atexit handler for enet */
     50    atexit( enet_deinitialize );
     51
     52
     53    /* initiate the client */
     54    this->client = enet_host_create( NULL /* create a client host */,
     55        1,
     56        2, /* allow up 2 channels to be used, 0 and 1 */
     57        0,
     58        0 );
     59
     60    /* see if it worked */
     61    if (this->client == NULL)
     62    { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
     63      return 1;
     64    }
     65  }
     66
     67  MasterServerComm::~MasterServerComm()
     68  {
     69    /* destroy the enet facilities */
     70    enet_host_destroy(this->client);
     71  }
     72
     73  int MasterServerComm::connect( char *address, unsigned int port )
     74  {
     75    /* Connect to address:port. */
     76    enet_address_set_host( &this->address, address );
     77    this->address.port = port;
     78
     79    /* Initiate the connection, allocating the two channels 0 and 1. */
     80    this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
     81
     82    if (this->peer == NULL )
     83    { fprintf( stderr,
    7784        "No available peers for initiating an ENet connection.\n");
    7885    //exit (EXIT_FAILURE);
    7986    return -1;
    80   }
    81 
    82   /* Wait up to 2 seconds for the connection attempt to succeed. */
    83   if (enet_host_service (this->client, &this->event, 2000) > 0 &&
    84       this->event.type == ENET_EVENT_TYPE_CONNECT )
    85     fprintf( stdout, "Connection to server succeeded." );
    86   else
    87   {
    88     enet_peer_reset (this->peer);
    89     fprintf( stdout, "Connection to %s failed.", address );
     87    }
     88
     89    /* Wait up to 2 seconds for the connection attempt to succeed. */
     90    if (enet_host_service (this->client, &this->event, 2000) > 0 &&
     91        this->event.type == ENET_EVENT_TYPE_CONNECT )
     92      fprintf( stdout, "Connection to server succeeded." );
     93    else
     94    {
     95      enet_peer_reset (this->peer);
     96      fprintf( stdout, "Connection to %s failed.", address );
     97      //exit(EXIT_FAILURE);
     98      return -1;
     99    }
     100
     101    return 0;
     102  }
     103
     104  int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
     105  {
     106    /* see whether anything happened */
     107    if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
     108    {
     109      /* address buffer */
     110      char *addrconv = NULL;
     111
     112      /* check what type of event it is and react accordingly */
     113      switch (this->event.type)
     114      { /* new connection, not supposed to happen. */
     115        case ENET_EVENT_TYPE_CONNECT: break;
     116
     117        /* disconnect */
     118        case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
     119
     120        /* incoming data */
     121        case ENET_EVENT_TYPE_RECEIVE:
     122          addrconv = (char *) calloc( 50, 1 );
     123          enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
     124
     125          /* DEBUG */
     126          printf( "A packet of length %u containing %s was "
     127            "received from %s on channel %u.\n",
     128            this->event.packet->dataLength,
     129            this->event.packet->data,
     130            addrconv,
     131            this->event.channelID );
     132          /* END DEBUG */
     133
     134          /* call the supplied callback, if any. */
     135          if( (*callback) != NULL )
     136            (*callback)( addrconv, &(this->event) );
     137
     138          enet_packet_destroy( event.packet );
     139          if( addrconv ) free( addrconv );
     140          break;
     141        default: break;
     142      }
     143
     144      /* event handled, return 0 */
     145      return 0;
     146    }
     147
     148    /* show that no event occured */
     149    return 1;
     150  }
     151
     152  int MasterServerComm::sendRequest( char *data )
     153  {
     154    /* send the data to the friend */
     155    /* Create a reliable packet of size 7 containing "packet\0" */
     156    ENetPacket * packet = enet_packet_create( data,
     157        strlen( data ) + 1,
     158        ENET_PACKET_FLAG_RELIABLE);
     159
     160    /* Send the packet to the peer over channel id 0. */
     161    enet_peer_send (this->peer, 0, packet);
     162
     163    /* One could just use enet_host_service() instead. */
     164    enet_host_flush( this->client );
     165    if( packet ) free( packet );
     166  }
     167
     168}
     169
     170
     171/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
     172/* not needed anymore, only here for testing purposes */
     173/*
     174//[> sample callback to output debugging info. <]
     175//int callb( char *addr, ENetEvent *ev )
     176//{
     177  //printf( "A packet of length %u containing %s was "
     178      //"received from %s on channel %u.\n",
     179      //ev->packet->dataLength,
     180      //ev->packet->data,
     181      //addr,
     182      //ev->channelID );
     183  //return 0;
     184//}
     185
     186//[> small testing implementation <]
     187//int
     188//main( int argc, char *argv[] )
     189//{
     190  //[> setup object and connect <]
     191  //MasterServerComm msc = MasterServerComm();
     192  //if( msc.connect( argv[1], 1234 ) )
    90193    //exit(EXIT_FAILURE);
    91     return -1;
    92   }
    93 
    94   return 0;
    95 }
    96 
    97 int
    98 MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
    99 {
    100   if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
    101   {
    102     char *addrconv = NULL;
    103     /* check what type of event it is and react accordingly */
    104     switch (this->event.type)
    105     { /* new connection, not supposed to happen. */
    106       case ENET_EVENT_TYPE_CONNECT: break;
    107 
    108       /* disconnect */
    109       case ENET_EVENT_TYPE_DISCONNECT:
    110         /* ?? */ break;
    111 
    112         /* incoming data */
    113       case ENET_EVENT_TYPE_RECEIVE:
    114         addrconv = (char *) calloc( 50, 1 );
    115         enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
    116 
    117         /* DEBUG */
    118         printf( "A packet of length %u containing %s was "
    119           "received from %s on channel %u.\n",
    120           this->event.packet->dataLength,
    121           this->event.packet->data,
    122           addrconv,
    123           this->event.channelID );
    124         /* END DEBUG */
    125 
    126         /* call the supplied callback, if any. */
    127         if( (*callback) != NULL )
    128           (*callback)( addrconv, &(this->event) );
    129 
    130         enet_packet_destroy( event.packet );
    131         if( addrconv ) free( addrconv );
    132         break;
    133       default: break;
    134     }
    135   }
    136194 
    137   return 0;
    138 }
    139 
    140 int
    141 MasterServerComm::sendRequest( char *data )
    142 {
    143   /* send the data to the friend */
    144   /* Create a reliable packet of size 7 containing "packet\0" */
    145   ENetPacket * packet = enet_packet_create( data,
    146       strlen( data ) + 1,
    147       ENET_PACKET_FLAG_RELIABLE);
    148 
    149   /* Send the packet to the peer over channel id 0. */
    150   enet_peer_send (this->peer, 0, packet);
    151 
    152   /* One could just use enet_host_service() instead. */
    153   enet_host_flush( this->client );
    154   if( packet ) free( packet );
    155 }
    156 
    157 /* sample callback to output debugging info. */
    158 int callb( char *addr, ENetEvent *ev )
    159 {
    160   printf( "A packet of length %u containing %s was "
    161       "received from %s on channel %u.\n",
    162       ev->packet->dataLength,
    163       ev->packet->data,
    164       addr,
    165       ev->channelID );
    166   return 0;
    167 }
    168 
    169 /* small testing implementation */
    170 int
    171 main( int argc, char *argv[] )
    172 {
    173   /* setup object and connect */
    174   MasterServerComm msc = MasterServerComm();
    175   if( msc.connect( argv[1], 1234 ) )
    176     exit(EXIT_FAILURE);
    177  
    178   /* send some data and poll for replies */
    179   char *theinput = (char *)calloc( 100,1 );
    180   while( true )
    181   {
    182     fgets( theinput, 90, stdin );
    183     if( !strncmp( theinput, "quit", 4 ) )
    184       break;
    185 
    186     msc.sendRequest( theinput );
    187     msc.pollForReply( &callb );
    188   }
    189 
    190 }
     195  //[> send some data and poll for replies <]
     196  //char *theinput = (char *)calloc( 100,1 );
     197  //while( true )
     198  //{
     199    //fgets( theinput, 90, stdin );
     200    //if( !strncmp( theinput, "quit", 4 ) )
     201      //break;
     202
     203    //msc.sendRequest( theinput );
     204    //msc.pollForReply( &callb );
     205  //}
     206
     207//}
     208*/
  • code/branches/masterserver/src/libraries/network/MasterServerComm.h

    r7629 r7631  
    2727 */
    2828
    29 class MasterServerComm
     29#include <cstdlib>
     30#include <cstdio>
     31#include <cstring>
     32#include <enet/enet.h>
     33
     34namespace orxonox
    3035{
    31   public:
    32     /** constructor */
    33     MasterServerComm();
     36  class MasterServerComm
     37  {
     38    public:
     39      /** constructor */
     40      MasterServerComm();
    3441
    35     /** destructor */
    36     ~MasterServerComm();
     42      /** destructor */
     43      ~MasterServerComm();
    3744
    38    
    39     /** \param address Address to connect to (Host name or IP in text form)
    40      * \param port Port to connect on
    41      * \return 0 for success, other for error
    42      *
    43      * Connect to the master server with the given address on the given port.
    44      */
    45     int connect( char *address, unsigned int port );
    46 
    47     /** \param data The data to be sent.
    48      * \return 0 for success, other for error.
    49      *
    50      * Send a request to the master server containing data specified in data
    51      */
    52     int sendRequest( char *data );
    53 
    54     /** \param callback The callback function to call with data receivced.
    55      * \return 0 for success, other for error
    56      *
    57      * Poll the master server for new data and act accordingly */
    58     int pollForReply( int (*callback)( char*, ENetEvent* ) );
    59 
    60   private:
    61     /* client handle */
    62     ENetHost *client;
    63     ENetEvent event;
    64 
    65     ENetAddress address;
    66     ENetPeer *peer;
     45      /** \return 0 for success, other for error
     46       *
     47       * Initialize everything for the master server communication
     48       */
     49      int initialize();
    6750
    6851
    69 };
     52      /** \param address Address to connect to (Host name or IP in text form)
     53       * \param port Port to connect on
     54       * \return 0 for success, other for error
     55       *
     56       * Connect to the master server with the given address on the given port.
     57       */
     58      int connect( char *address, unsigned int port );
     59
     60      /** \param data The data to be sent.
     61       * \return 0 for success, other for error.
     62       *
     63       * Send a request to the master server containing data specified in data
     64       */
     65      int sendRequest( char *data );
     66
     67      /** \param callback The callback function to call with data receivced.
     68       * \return 0 for success, other for error
     69       *
     70       * Poll the master server for new data and act accordingly */
     71      int pollForReply( int (*callback)( char*, ENetEvent* ) );
     72
     73    private:
     74      /** client handle */
     75      ENetHost *client;
     76
     77      /** event data holder */
     78      ENetEvent event;
     79
     80      /** address holder */
     81      ENetAddress address;
     82
     83      /** peer data holder */
     84      ENetPeer *peer;
     85  };
     86
     87}
  • code/branches/masterserver/src/libraries/network/WANDiscovery.cc

    r7630 r7631  
    2121 *
    2222 *   Author:
    23  *      Fabian 'x3n' Landau
     23 *      Fabian 'x3n' Landau (original)
    2424 *   Co-authors:
     25 *      Sandro 'smerkli' Merkli (adaptions to WAN)
    2526 *      ...
    2627 *
     
    4243  WANDiscovery::WANDiscovery()
    4344  {
     45    /* create master server communications object */
     46    this->msc = MasterServerComm();
     47
     48    /* initialize it and see if it worked */
     49    if( msc.initialize() )
     50      COUT(1) << "Error: could not initialize master server communications!\n";
     51
     52    /* connect and see if it worked */
     53    if( msc.connect( MS_ADDRESS, 1234 ) )
     54      COUT(1) << "Error: could not connect to master server!\n";
    4455  }
    4556
    4657  WANDiscovery::~WANDiscovery()
    47   {
     58  {
     59    /* clear server list */
     60    this->servers_.clear(); 
    4861  }
    4962
     63  /* callback for the network reply poller */
     64  /* NOTE implement protocol-specific part here. */
     65  int replyhandler( char *addr, ENetEvent *ev )
     66  {
     67    /* handle incoming data
     68     * if a list entry arrives add to list
     69     * if a done entry arrives set done to true
     70     */
     71
     72    /* done handling, return all ok code 0 */
     73    return 0;
     74  }
     75 
    5076  void WANDiscovery::discover()
    5177  {
     78    /* clear list */
    5279    this->servers_.clear();
     80
     81    /* send request to server */
     82    msc.sendRequest( MSPROTO_CLIENT MSPROTO_REQ_LIST );
     83
     84    /* deal with replies */
     85    while( msc.pollForReply( replyhandler ) )
     86      /* nothing */;
     87
     88    /* done receiving. */
    5389  }
    5490
    5591  std::string WANDiscovery::getServerListItemName(unsigned int index)
    5692  {
    57 
     93    /* if the index is out of range, return empty */
     94    if( index >= this->servers_.size() )
     95      return BLANKSTRING;
     96    else
     97      /* else return the name of the server */
     98      return this->servers_[index].getServerName();
    5899  }
    59100
    60101  std::string WANDiscovery::getServerListItemIP(unsigned int index)
    61102  {
    62 
     103    /* if the index is out of range, return empty */
     104    if( index >= this->servers_.size() )
     105      return BLANKSTRING;
     106    else
     107      /* else return the IP of the server */
     108      return this->servers_[index].getServerIP();
    63109  }
    64110
  • code/branches/masterserver/src/libraries/network/WANDiscovery.h

    r7630 r7631  
    3333#include "packet/ServerInformation.h"
    3434#include "util/Singleton.h"
     35#include "MasterServerComm.h"
    3536
    3637#include <vector>
     38
     39/* master server address (to be moved elsewhere later) */
     40#define MS_ADDRESS "localhost"
     41
     42/* protocol (to be moved elsewhere later) */
     43#define MSPROTO_CLIENT "CL "
     44#define MSPROTO_REQ_LIST "REQ:LIST"
    3745
    3846// tolua_begin
     
    4654    friend class Singleton<WANDiscovery>;
    4755    public:
     56      /** constructor */
    4857      WANDiscovery();
     58
     59      /** destructor */
    4960      ~WANDiscovery();
     61
     62      /** ask server for server list  */
    5063      void discover(); // tolua_export
     64
     65      /** \param index Index to get the name of
     66       * \return The name of the server
     67       *
     68       * Get the name of the server at index index.
     69       */
    5170      std::string getServerListItemName( unsigned int index ); // tolua_export
     71
     72      /** \param index Index to get the IP of
     73       * \return The IP of the server
     74       *
     75       * Get the IP of the server at index index.
     76       */
    5277      std::string getServerListItemIP( unsigned int index ); // tolua_export
    53       static WANDiscovery& getInstance(){ return Singleton<WANDiscovery>::getInstance(); } // tolua_export
     78
     79      /** \return an instance of WANDiscovery
     80       *
     81       * Create and return an instance of WANDiscovery.
     82       */
     83      static WANDiscovery& getInstance() { return Singleton<WANDiscovery>::getInstance(); } // tolua_export
    5484     
    5585    private:
     86      /** Singleton pointer */
    5687      static WANDiscovery* singletonPtr_s;
    57       ENetHost* host_;
     88
     89      /** Master server communications object */
     90      MasterServerComm msc;
     91
     92      /** game server list */
    5893      std::vector<packet::ServerInformation> servers_;
     94
    5995  }; // tolua_export
    6096
Note: See TracChangeset for help on using the changeset viewer.