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/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
Note: See TracChangeset for help on using the changeset viewer.