Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 3, 2010, 4:20:02 PM (14 years ago)
Author:
smerkli
Message:

Communication channel now working both ways, doxygen documentation added, renamed files to convention, re-wrote the client part object-oriented and non-blocking

File:
1 moved

Legend:

Unmodified
Added
Removed
  • code/branches/masterserver/src/modules/masterserver/MasterServerComm.cc

    r7610 r7611  
    3131#include <cstring>
    3232#include <enet/enet.h>
     33#include "MasterServerComm.h"
    3334
    34 int main( int argc, char *argv[] )
     35MasterServerComm::MasterServerComm()
    3536{
    3637  /* initialize Enet */
    3738  if (enet_initialize () != 0)
    3839  { fprintf (stderr, "An error occurred while initializing ENet.\n");
    39     return EXIT_FAILURE;
     40    exit(EXIT_FAILURE);
    4041  }
    4142  atexit (enet_deinitialize);
    4243
    43   /* client handle */
    44   ENetHost *client;
    4544
    4645  /* initiate the client */
    47   client = enet_host_create( NULL /* create a client host */,
     46  this->client = enet_host_create( NULL /* create a client host */,
    4847      1,
    4948      2, /* allow up 2 channels to be used, 0 and 1 */
     
    5251
    5352  /* see if it worked */
    54   if (client == NULL)
     53  if (this->client == NULL)
    5554  { fprintf (stderr,
    5655      "An error occurred while trying to create an ENet client host.\n");
    5756    exit (EXIT_FAILURE);
    5857  }
     58}
    5959
    60   ENetAddress address;
    61   ENetEvent event;
    62   ENetPeer *peer;
     60MasterServerComm::~MasterServerComm()
     61{
     62  enet_host_destroy(this->client);
     63}
    6364
    64   /* Connect to some.server.net:1234. */
    65   enet_address_set_host (& address, argv[1] );
    66   address.port = 1234;
     65int
     66MasterServerComm::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;
    6771
    6872  /* Initiate the connection, allocating the two channels 0 and 1. */
    69   peer = enet_host_connect(client, &address, 2, 0);   
     73  this->peer = enet_host_connect(this->client, &this->address, 2, 0);   
    7074
    71   if (peer == NULL )
     75  if (this->peer == NULL )
    7276  { fprintf( stderr,
    7377        "No available peers for initiating an ENet connection.\n");
     
    7579  }
    7680
    77   /* Wait up to 5 seconds for the connection attempt to succeed. */
    78   if (enet_host_service (client, & event, 5000) > 0 &&
    79       event.type == ENET_EVENT_TYPE_CONNECT)
    80   { fprintf( stdout, "Connection to some.server.net:1234 succeeded." );
    81     char *theinput = (char *)calloc( 100,1 );
    82     while( true )
    83     {
    84       fgets( theinput, 90, stdin );
    85       if( !strncmp( theinput, "quit", 4 ) )
    86         break;
    87 
    88       /* send the data to the friend */
    89       /* Create a reliable packet of size 7 containing "packet\0" */
    90       ENetPacket * packet = enet_packet_create( theinput,
    91           strlen( theinput ) + 1,
    92           ENET_PACKET_FLAG_RELIABLE);
    93 
    94       /* Send the packet to the peer over channel id 0. */
    95       enet_peer_send (peer, 0, packet);
    96 
    97       /* One could just use enet_host_service() instead. */
    98       enet_host_flush( client );
    99       if( packet ) free( packet );
    100     }
    101   }
     81  /* Wait up to 2 seconds for the connection attempt to succeed. */
     82  if (enet_host_service (this->client, &this->event, 2000) > 0 &&
     83      this->event.type == ENET_EVENT_TYPE_CONNECT )
     84    fprintf( stdout, "Connection to server succeeded." );
    10285  else
    10386  {
    104     /* Either the 5 seconds are up or a disconnect event was */
    105     /* received. Reset the peer in the event the 5 seconds   */
    106     /* had run out without any significant event.            */
    107     enet_peer_reset (peer);
    108     fprintf( stdout, "Connection to some.server.net:1234 failed." );
     87    enet_peer_reset (this->peer);
     88    fprintf( stdout, "Connection to %s failed.", address );
    10989    exit(EXIT_FAILURE);
    11090  }
    11191
    112   enet_host_destroy(client);
     92  return 0;
    11393}
     94
     95int
     96MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
     97{
     98  if( enet_host_service( this->client, &this->event, 100 ) >= 0 )
     99  {
     100    char *addrconv = NULL;
     101    /* check what type of event it is and react accordingly */
     102    switch (this->event.type)
     103    { /* new connection, not supposed to happen. */
     104      case ENET_EVENT_TYPE_CONNECT: break;
     105
     106      /* disconnect */
     107      case ENET_EVENT_TYPE_DISCONNECT:
     108        /* ?? */ break;
     109
     110        /* incoming data */
     111      case ENET_EVENT_TYPE_RECEIVE:
     112        addrconv = (char *) calloc( 50, 1 );
     113        enet_address_get_host_ip( &(this->event.peer->address), addrconv, 49 );
     114
     115        /* DEBUG */
     116        printf( "A packet of length %u containing %s was "
     117          "received from %s on channel %u.\n",
     118          this->event.packet->dataLength,
     119          this->event.packet->data,
     120          addrconv,
     121          this->event.channelID );
     122        /* END DEBUG */
     123
     124        /* call the supplied callback, if any. */
     125        if( (*callback) != NULL )
     126          (*callback)( addrconv, &(this->event) );
     127
     128        enet_packet_destroy( event.packet );
     129        if( addrconv ) free( addrconv );
     130        break;
     131      default: break;
     132    }
     133  }
     134 
     135  return 0;
     136}
     137
     138int
     139MasterServerComm::sendRequest( char *data )
     140{
     141  /* send the data to the friend */
     142  /* Create a reliable packet of size 7 containing "packet\0" */
     143  ENetPacket * packet = enet_packet_create( data,
     144      strlen( data ) + 1,
     145      ENET_PACKET_FLAG_RELIABLE);
     146
     147  /* Send the packet to the peer over channel id 0. */
     148  enet_peer_send (this->peer, 0, packet);
     149
     150  /* One could just use enet_host_service() instead. */
     151  enet_host_flush( this->client );
     152  if( packet ) free( packet );
     153}
     154
     155/* sample callback to output debugging info. */
     156int callb( char *addr, ENetEvent *ev )
     157{
     158  printf( "A packet of length %u containing %s was "
     159      "received from %s on channel %u.\n",
     160      ev->packet->dataLength,
     161      ev->packet->data,
     162      addr,
     163      ev->channelID );
     164  return 0;
     165}
     166
     167/* small testing implementation */
     168int
     169main( int argc, char *argv[] )
     170{
     171  /* setup object and connect */
     172  MasterServerComm msc = MasterServerComm();
     173  msc.connect( argv[1], 1234 );
     174 
     175  /* send some data and poll for replies */
     176  char *theinput = (char *)calloc( 100,1 );
     177  while( true )
     178  {
     179    fgets( theinput, 90, stdin );
     180    if( !strncmp( theinput, "quit", 4 ) )
     181      break;
     182
     183    msc.sendRequest( theinput );
     184    msc.pollForReply( &callb );
     185  }
     186
     187}
Note: See TracChangeset for help on using the changeset viewer.