Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7611


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

Location:
code/branches/masterserver/src/modules/masterserver
Files:
1 added
1 edited
4 moved

Legend:

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

    r7610 r7611  
    4040  /* connect event */
    4141  int
    42   eventConnect( ENetEvent *event )
     42  MasterServer::eventConnect( ENetEvent *event )
    4343  { /* check for bad parameters */
    4444    if( !event )
    45     { fprintf( stderr, "No event given.\n" );
     45    { COUT(2) << "MasterServer::eventConnect: No event given.\n" ;
    4646      return -1;
    4747    }
    4848
     49    /* convert address to string. */
     50    char *addrconv = (char *) calloc( 50, 1 );
     51    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
     52
    4953    /* output debug info */
    50     printf( "A new client connected from %x:%u.\n",
    51         (event->peer->address.host),
    52         event->peer->address.port );
     54    COUT(4) << "A new client connected from "
     55      << addrconv
     56      << " on port "
     57      << event->peer->address.port << "\n";
    5358
    5459    /* game server or client connection? */
     
    6166    /* add to client list */
    6267
    63     /* NOTE this seems to be some weird snipped from the tutorial as peer.data
    64      * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S
    65      */
    66     /* Store any relevant client information here. */
    67     /* event->peer->data = "Client information"; */
    68     /* /NOTE */
     68    /* store string form of address here */
     69    event->peer->data = addrconv;
     70
     71    /* all fine. */
    6972    return 0;
    7073  }
     
    7275  /* disconnect event */
    7376  int
    74   eventDisconnect( ENetEvent *event )
     77  MasterServer::eventDisconnect( ENetEvent *event )
    7578  { /* check for bad parameters */
    7679    if( !event )
    77     { fprintf( stderr, "No event given.\n" );
     80    { COUT(2) << "No event given.\n";
    7881      return -1;
    7982    }
    8083
    8184    /* output that the disconnect happened, to be removed at a later time. */
    82     printf( "%s disconnected.\n", (char*)event->peer->data );
     85    COUT(4) << (char*)event->peer->data << " disconnected.\n";
    8386
    8487    /* remove the server from the list it belongs to */
    8588
    8689    /* Reset the peer's client information. */
    87     event->peer->data = NULL;
     90    if( event->peer->data ) free( event->peer->data );
    8891    return 0;
    8992  }
     
    9194  /* data event */
    9295  int
    93   eventData( ENetEvent *event )
     96  MasterServer::eventData( ENetEvent *event )
    9497  { /* output what's in the packet (to be removed later) */
    95     if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
    96     { fprintf( stderr, "No complete event given.\n" );
     98    if( !event || !(event->packet) || !(event->peer) )
     99    { COUT(2) << "No complete event given.\n";
    97100      return -1;
    98101    }
    99 
     102     
     103    /* generate address in readable form */
     104    char *addrconv = (char *) calloc( 50, 1 );
     105    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
     106
     107    /* DEBUG */
    100108    /* output debug info about the data that has come, to be removed */
    101     //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
    102     //event->packet->dataLength,
    103     //event->packet->data,
    104     //event->peer->data,
    105     //event->channelID );
     109    COUT(4) << "A packet of length"
     110      << event->packet->dataLength
     111      << " containing "
     112      << event->packet->data
     113      << " was received from "
     114      << addrconv
     115      << " on channel "
     116      << event->channelID << "\n";
     117
     118    /* send some packet back for testing */
     119    /* TESTING */
     120
     121    /* Create a reliable reply of size 7 containing "reply\0" */
     122    ENetPacket * reply = enet_packet_create ("reply",
     123        strlen ("reply") + 1,
     124        ENET_PACKET_FLAG_RELIABLE);
     125
     126    /* Send the reply to the peer over channel id 0. */
     127    enet_peer_send( event->peer, 0, reply );
     128
     129    /* One could just use enet_host_service() instead. */
     130    enet_host_flush( this->server );
     131
     132    /* /TESTING */
    106133
    107134    /* game server or client connection? */
     
    115142    /* and send reply */
    116143
     144    /* delete addrconv */
     145    if( addrconv ) free( addrconv );
     146
    117147    /* Clean up the packet now that we're done using it. */
    118148    enet_packet_destroy( event->packet );
     
    120150  }
    121151
     152
    122153  /**** MAIN ROUTINE *****/
    123154  int
    124155  MasterServer::run()
    125156  {
    126     COUT(0) << "omg, i got baschtl'd!\n";
    127 
    128157    /***** ENTER MAIN LOOP *****/
    129158    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
    130159    if( event == NULL )
    131     { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
    132       exit( EXIT_FAILURE );
    133     }
     160    {
     161      COUT(1) << "Could not create ENetEvent structure, exiting.\n";
     162      exit( EXIT_FAILURE );
     163    }
     164
     165    /* tell people we're now initialized and blocking. */
     166    COUT(0) << "MasterServer initialized, waiting for connections.\n";
    134167
    135168    /* create an iterator for the loop */
    136     while( enet_host_service( this->server, event, 1000 ) > 0 )
     169    while( enet_host_service( this->server, event, 1000 ) >= 0 )
    137170    { /* check what type of event it is and react accordingly */
    138171      switch (event->type)
     
    163196    /***** INITIALIZE NETWORKING *****/
    164197    if( enet_initialize () != 0)
    165     { fprintf( stderr, "An error occurred while initializing ENet.\n");
     198    { COUT(1) << "An error occurred while initializing ENet.\n";
    166199      exit( EXIT_FAILURE );
    167200    }
     
    181214    /* see if creation worked */
    182215    if( !this->server )
    183     { fprintf( stderr,
    184         "An error occurred while trying to create an ENet server host.\n" );
    185     exit( EXIT_FAILURE );
    186     }
    187 
    188     /***** INITIALIZE GAME SERVER LIST *****/
     216    { COUT(1) <<
     217        "An error occurred while trying to create an ENet server host.\n";
     218      exit( EXIT_FAILURE );
     219    }
     220
     221    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
    189222    this->mainlist = new ServerList();
    190     if( this->mainlist == NULL )
    191     { fprintf( stderr, "Error creating server list.\n" );
    192       exit( EXIT_FAILURE );
    193     }
    194 
    195     /***** INITIALIZE PEER LIST *****/
    196223    this->peers = new PeerList();
     224    if( this->mainlist == NULL || this->peers == NULL )
     225    { COUT(1) << "Error creating server or peer list.\n";
     226      exit( EXIT_FAILURE );
     227    }
    197228
    198229    /* run the main method */
     
    207238    enet_host_destroy( this->server );
    208239
     240    /* free all used memory */
    209241    /* clear the list of connected game servers */
    210242    /* clear the list of connected game clients */
    211243
    212     /* free all used memory */
    213 
    214244  }
    215245
  • code/branches/masterserver/src/modules/masterserver/MasterServer.h

    r7590 r7611  
    6868   
    6969    private:
     70      /* methods */
     71      int eventConnect( ENetEvent *event );
     72      int eventDisconnect( ENetEvent *event );
     73      int eventData( ENetEvent *event );
     74
     75      /* members */
    7076      ENetAddress address;
    7177      ENetHost *server;
    7278      ServerList *mainlist;
    7379      PeerList *peers;
     80
     81      /* main routine */
    7482      int run();
    7583
  • 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.