Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 27, 2010, 4:16:08 PM (14 years ago)
Author:
smerkli
Message:

done for today, everything compiles, test client as well. beginning testing of basic networking functionality over the course of the next week.

Location:
code/branches/masterserver/src/modules/masterserver
Files:
3 edited

Legend:

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

    r7589 r7590  
    2828
    2929#include "MasterServer.h"
    30 
    31 using namespace std;
    32 
    33 /***** EVENTS *****/
    34 /* connect event */
    35 int eventConnect( ENetEvent *event,
    36   orxonox::ServerList *serverlist,
    37   orxonox::PeerList *peers )
    38 { /* check for bad parameters */
    39   if( !event )
    40   { fprintf( stderr, "No event given.\n" );
    41     return -1;
    42   }
    43 
    44   /* output debug info */
    45   printf( "A new client connected from %x:%u.\n",
    46       event->peer->address.host,
    47       event->peer->address.port);
    48 
    49   /* game server or client connection? */
    50   /* -> decide in protocol */
     30#include "util/ScopedSingletonManager.h"
     31#include "core/CoreIncludes.h"
     32#include "core/CorePrereqs.h"
     33
     34namespace orxonox
     35{
     36  /* singleton stuff */
     37  ManageScopedSingleton( MasterServer, ScopeID::Root, false );
     38
     39  /***** EVENTS *****/
     40  /* connect event */
     41  int
     42  eventConnect( ENetEvent *event )
     43  { /* check for bad parameters */
     44    if( !event )
     45    { fprintf( stderr, "No event given.\n" );
     46      return -1;
     47    }
     48
     49    /* output debug info */
     50    printf( "A new client connected from %x:%u.\n",
     51        (event->peer->address.host),
     52        event->peer->address.port );
     53
     54    /* game server or client connection? */
     55    /* -> decide in protocol */
    5156    /* game server */
    52       /* add to game server list */
    53       /*  */
     57    /* add to game server list */
     58    /*  */
    5459
    5560    /* client */
    56       /* add to client list */
    57  
    58   /* NOTE this seems to be some weird snipped from the tutorial as peer.data
    59    * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S
    60    */
    61   /* Store any relevant client information here. */
    62   /* event->peer->data = "Client information"; */
    63   /* /NOTE */
    64   return 0;
    65 }
    66 
    67 /* disconnect event */
    68 int eventDisconnect( ENetEvent *event,
    69   orxonox::ServerList *serverlist,
    70   orxonox::PeerList *peers )
    71 { /* check for bad parameters */
    72   if( !event )
    73   { fprintf( stderr, "No event given.\n" );
    74     return -1;
    75   }
    76 
    77   /* output that the disconnect happened, to be removed at a later time. */
    78   printf( "%s disconnected.\n", event->peer->data );
    79 
    80   /* remove the server from the list it belongs to */
    81 
    82   /* Reset the peer's client information. */
    83   event->peer->data = NULL;
    84   return 0;
    85 }
    86 
    87 /* data event */
    88 int eventData( ENetEvent *event )
    89 { /* output what's in the packet (to be removed later) */
    90   if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
    91   { fprintf( stderr, "No complete event given.\n" );
    92     return -1;
    93   }
    94 
    95   /* output debug info about the data that has come, to be removed */
    96   //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
    97       //event->packet->dataLength,
    98       //event->packet->data,
    99       //event->peer->data,
    100       //event->channelID );
    101  
    102   /* game server or client connection? */
     61    /* add to client list */
     62
     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 */
     69    return 0;
     70  }
     71
     72  /* disconnect event */
     73  int
     74  eventDisconnect( ENetEvent *event )
     75  { /* check for bad parameters */
     76    if( !event )
     77    { fprintf( stderr, "No event given.\n" );
     78      return -1;
     79    }
     80
     81    /* output that the disconnect happened, to be removed at a later time. */
     82    printf( "%s disconnected.\n", (char*)event->peer->data );
     83
     84    /* remove the server from the list it belongs to */
     85
     86    /* Reset the peer's client information. */
     87    event->peer->data = NULL;
     88    return 0;
     89  }
     90
     91  /* data event */
     92  int
     93  eventData( ENetEvent *event )
     94  { /* 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" );
     97      return -1;
     98    }
     99
     100    /* 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 );
     106
     107    /* game server or client connection? */
    103108    /* game server */
    104       /* parse data */
    105       /* start actions */
    106       /* and send reply */
     109    /* parse data */
     110    /* start actions */
     111    /* and send reply */
    107112    /* client */
    108       /* parse data */
    109       /* start actions */
    110       /* and send reply */
    111 
    112   /* Clean up the packet now that we're done using it. */
    113   enet_packet_destroy( event->packet );
    114   return 0;
    115 }
    116 
    117 
    118 
    119 /**** MAIN ROUTINE *****/
    120 int main( int argc, char *argv[] )
    121 {
    122   /***** INITIALIZE NETWORKING *****/
    123   if( enet_initialize () != 0)
    124   { fprintf( stderr, "An error occurred while initializing ENet.\n");
    125     return EXIT_FAILURE;
    126   }
    127 
    128   /* register deinitialization */
    129   atexit( enet_deinitialize );
    130 
    131   /* define our address and a host structure */
    132   ENetAddress address;
    133   ENetHost *server;
    134 
    135   /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
    136   address.host = ENET_HOST_ANY;
    137   address.port = ORX_MSERVER_PORT;
    138 
    139   /* create a host with the above settings (the last two 0 mean: accept
    140    * any input/output bandwidth */
    141   server = enet_host_create( &address, ORX_MSERVER_MAXCONNS,
    142     ORX_MSERVER_MAXCHANS, 0, 0 );     
    143 
    144   /* see if creation worked */
    145   if( !server )
    146   { fprintf( stderr,
     113    /* parse data */
     114    /* start actions */
     115    /* and send reply */
     116
     117    /* Clean up the packet now that we're done using it. */
     118    enet_packet_destroy( event->packet );
     119    return 0;
     120  }
     121
     122  /**** MAIN ROUTINE *****/
     123  int
     124  MasterServer::run()
     125  {
     126    COUT(0) << "omg, i got baschtl'd!\n";
     127
     128    /***** ENTER MAIN LOOP *****/
     129    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
     130    if( event == NULL )
     131    { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
     132      exit( EXIT_FAILURE );
     133    }
     134
     135    /* create an iterator for the loop */
     136    while( enet_host_service( this->server, event, 1000 ) > 0 )
     137    { /* check what type of event it is and react accordingly */
     138      switch (event->type)
     139      { /* new connection */
     140        case ENET_EVENT_TYPE_CONNECT:
     141          eventConnect( event ); break;
     142
     143          /* disconnect */
     144        case ENET_EVENT_TYPE_DISCONNECT:
     145          eventDisconnect( event ); break;
     146
     147          /* incoming data */
     148        case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
     149        default: break;
     150      }
     151    }
     152
     153    /* free the event */
     154    if( event ) free( event );
     155
     156    /* done */
     157    return 0;
     158  }
     159
     160  /* constructor */
     161  MasterServer::MasterServer()
     162  {
     163    /***** INITIALIZE NETWORKING *****/
     164    if( enet_initialize () != 0)
     165    { fprintf( stderr, "An error occurred while initializing ENet.\n");
     166      exit( EXIT_FAILURE );
     167    }
     168
     169    /* register deinitialization */
     170    atexit( enet_deinitialize );
     171
     172    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
     173    this->address.host = ENET_HOST_ANY;
     174    this->address.port = ORX_MSERVER_PORT;
     175
     176    /* create a host with the above settings (the last two 0 mean: accept
     177     * any input/output bandwidth */
     178    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS,
     179        ORX_MSERVER_MAXCHANS, 0, 0 );     
     180
     181    /* see if creation worked */
     182    if( !this->server )
     183    { fprintf( stderr,
    147184        "An error occurred while trying to create an ENet server host.\n" );
    148185    exit( EXIT_FAILURE );
    149   }
    150 
    151   /***** INITIALIZE GAME SERVER LIST *****/
    152   orxonox::ServerList *mainlist = new orxonox::ServerList();
    153   if( mainlist == NULL )
    154   { fprintf( stderr, "Error creating server list.\n" );
    155     exit( EXIT_FAILURE );
    156   }
    157 
    158   /***** INITIALIZE PEER LIST *****/
    159   orxonox::PeerList *peers = new orxonox::PeerList();
    160 
    161   /***** ENTER MAIN LOOP *****/
    162   ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
    163   if( event == NULL )
    164   { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
    165     exit( EXIT_FAILURE );
    166   }
    167 
    168   /* create an iterator for the loop */
    169   while( enet_host_service( server, event, 1000 ) > 0 )
    170   { /* check what type of event it is and react accordingly */
    171     switch (event->type)
    172     { /* new connection */
    173       case ENET_EVENT_TYPE_CONNECT:
    174         eventConnect( event, mainlist, peers ); break;
    175 
    176       /* disconnect */
    177       case ENET_EVENT_TYPE_DISCONNECT:
    178         eventDisconnect( event, mainlist, peers ); break;
    179 
    180       /* incoming data */
    181       case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
    182       default: break;
    183     }
    184   }
    185 
    186   /***** CLEANUP PROCESS *****/
    187   /* terminate all networking connections */
    188   enet_host_destroy( server );
    189 
    190   /* clear the list of connected game servers */
    191   /* clear the list of connected game clients */
    192 
    193   /* free all used memory */
    194   if( event ) free( event );
    195 
    196   /* all done */
    197   return EXIT_SUCCESS;
    198 }
     186    }
     187
     188    /***** INITIALIZE GAME SERVER LIST *****/
     189    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 *****/
     196    this->peers = new PeerList();
     197
     198    /* run the main method */
     199    run();
     200  }
     201
     202  /* destructor */
     203  MasterServer::~MasterServer()
     204  {
     205    /***** CLEANUP PROCESS *****/
     206    /* terminate all networking connections */
     207    enet_host_destroy( this->server );
     208
     209    /* clear the list of connected game servers */
     210    /* clear the list of connected game clients */
     211
     212    /* free all used memory */
     213
     214  }
     215
     216/* end of namespace */
     217}
  • code/branches/masterserver/src/modules/masterserver/MasterServer.h

    r7589 r7590  
    3838#include <network/packet/Gamestate.h>
    3939#include <network/packet/Welcome.h>
     40#include <util/Singleton.h>
    4041
    4142/* my includes */
     
    5051#define ORX_MSERVER_MAXCHANS 2
    5152
     53namespace orxonox
     54{
     55  /* singleton */
     56  class MasterServer: public Singleton<MasterServer>
     57  {
     58    public:
     59      MasterServer();
     60      ~MasterServer();
     61
     62      friend class Singleton<MasterServer>;
     63      static MasterServer& getInstance(void)
     64      { return Singleton<MasterServer>::getInstance(); }
     65
     66      /* data fields */
     67      static MasterServer* singletonPtr_s;
     68   
     69    private:
     70      ENetAddress address;
     71      ENetHost *server;
     72      ServerList *mainlist;
     73      PeerList *peers;
     74      int run();
     75
     76  };
     77}
     78
    5279#endif /* _MasterServer_ */
  • code/branches/masterserver/src/modules/masterserver/PeerList.cpp

    r7580 r7590  
    2828
    2929#include "PeerList.h"
     30#include <network/packet/ServerInformation.h>
    3031#include <cstdio>
    3132
     
    4950
    5051  bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
    51   { return ( (addr1.host == addr2.host) && (addr1.port == addr2.port) ); }
     52  {
     53    for( int i = 0; i < 16; ++i )
     54      if( addr1.host.addr[i] < addr2.host.addr[i] )
     55        return -i;
     56      else if( addr1.host.addr[i] > addr2.host.addr[i] )
     57        return i;
     58
     59    return 0;
     60  }
     61   
    5262
    5363  bool
    5464  PeerList::remPeerByAddr( ENetAddress addr )
    5565  { /* get an iterator */
    56     list<packet::ENetPeer *>::iterator i;
     66    std::list<ENetPeer *>::iterator i;
    5767
    5868    /* loop through list elements */
    5969    for( i = peerlist.begin(); i != peerlist.end(); ++i )
    60       if( sub_compAddr((*i)->address, addr ) )
     70      if( !sub_compAddr((*i)->address, addr ) )
    6171      { /* found this name, remove and quit */
    62         this->peerlist.remove( i );
     72        this->peerlist.remove( *i );
    6373        return true;
    6474      }
     
    7181  PeerList::findPeerByAddr( ENetAddress addr )
    7282  { /* get an iterator */
    73     list<packet::ENetPeer *>::iterator i;
     83    std::list<ENetPeer *>::iterator i;
    7484
    7585    /* loop through list elements */
    7686    for( i = peerlist.begin(); i != peerlist.end(); ++i )
    77       if( sub_compAddr((*i)->address, addr ) )
     87      if( !sub_compAddr((*i)->address, addr ) )
    7888        /* found this name, remove and quit */
    79         return i;
     89        return *i;
    8090
    8191    /* not found */
Note: See TracChangeset for help on using the changeset viewer.