Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/masterserver/MasterServer.cpp @ 7568

Last change on this file since 7568 was 7568, checked in by smerkli, 14 years ago

some progress, nothing working yet though.

File size: 4.7 KB
Line 
1#include "MasterServer.h"
2
3using namespace std;
4
5/***** EVENTS *****/
6/* connect event */
7int eventConnect( ENetEvent *event )
8{ /* check for bad parameters */
9  if( !event )
10  { fprintf( stderr, "No event given.\n" );
11    return -1;
12  }
13
14  /* output debug info */
15  printf( "A new client connected from %x:%u.\n", 
16      event->peer->address.host,
17      event->peer->address.port);
18
19  /* game server or client connection? */
20    /* game server */
21      /* add to game server list */
22    /* client */
23      /* add to client list */
24 
25  /* NOTE this seems to be some weird snipped from the tutorial as peer.data
26   * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S
27   */
28  /* Store any relevant client information here. */
29  /* event->peer->data = "Client information"; */
30  /* /NOTE */
31  return 0;
32}
33
34/* disconnect event */
35int eventDisconnect( ENetEvent *event )
36{ /* check for bad parameters */
37  if( !event )
38  { fprintf( stderr, "No event given.\n" );
39    return -1;
40  }
41
42  /* output that the disconnect happened, to be removed at a later time. */
43  printf ("%s disconnected.\n", event.peer -> data);
44
45  /* remove the server from the list it belongs to */
46
47  /* Reset the peer's client information. */
48  event->peer->data = NULL;
49  return 0;
50}
51
52/* data event */
53int eventData( ENetEvent *event )
54{ /* output what's in the packet (to be removed later) */
55  if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
56  { fprintf( stderr, "No complete event given.\n" );
57    return -1;
58  }
59
60  /* output debug info about the data that has come, to be removed */
61  printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
62      event->packet->dataLength,
63      event->packet->data,
64      event->peer->data,
65      event->channelID );
66 
67  /* game server or client connection? */
68    /* game server */
69      /* parse data */
70      /* start actions */
71      /* and send reply */
72    /* client */
73      /* parse data */
74      /* start actions */
75      /* and send reply */
76
77  /* Clean up the packet now that we're done using it. */
78  enet_packet_destroy( event->packet );
79  return 0;
80}
81
82
83
84/**** MAIN ROUTINE *****/
85int main( int argc, char *argv[] )
86{
87  /***** INITIALIZE NETWORKING *****/
88  if( enet_initialize () != 0)
89  { fprintf( stderr, "An error occurred while initializing ENet.\n");
90    return EXIT_FAILURE;
91  }
92
93  /* register deinitialization */
94  atexit( enet_deinitialize );
95
96  /* define our address and a host structure */
97  ENetAddress address;
98  ENetHost *server;
99
100  /* Bind the server to the default localhost.     */
101  /* A specific host address can be specified by   */
102  /* enet_address_set_host (& address, "x.x.x.x"); */
103  address.host = ENET_HOST_ANY;
104
105  /* Bind the server to port 1234. */
106  address.port = ORX_MSERVER_PORT;
107
108  server = enet_host_create( & address /* the address to bind the server host to */, 
109      ORX_MSERVER_MAXCONNS      /* allow up to 32 clients and/or outgoing connections */,
110      ORX_MSERVER_MAXCHANS      /* allow up to 2 channels to be used, 0 and 1 */,
111      0      /* assume any amount of incoming bandwidth */,
112      0      /* assume any amount of outgoing bandwidth */);
113
114  /* see if creation worked */
115  if( !server )
116  { fprintf( stderr, 
117        "An error occurred while trying to create an ENet server host.\n" );
118    exit( EXIT_FAILURE );
119  }
120
121  /***** INITIALIZE GAME SERVER LIST *****/
122  orxonox::ServerList *mainlist = new orxonox::ServerList();
123  //if( mainlist == NULL )
124  //{ fprintf( stderr, "Error creating server list.\n" );
125    //exit( EXIT_FAILURE );
126  //}
127
128  /***** ENTER MAIN LOOP *****/
129  ENetEvent *event = 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  /* NOTE this only waits on one client, we need to find some way to
136   * actually listen on all active connections. This will be implemented
137   * together with the list of active connections.
138   */
139  /* Wait up to 1000 milliseconds for an event. */
140  while (enet_host_service (client, event, 1000) > 0)
141  { /* check what type of event it is and react accordingly */
142    switch (event.type)
143    { /* new connection */
144      case ENET_EVENT_TYPE_CONNECT: eventConnect( event ); break;
145
146      /* disconnect */
147      case ENET_EVENT_TYPE_DISCONNECT: eventDisconnect( event ); break;
148
149      /* incoming data */
150      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
151    }
152  }
153
154  /***** CLEANUP PROCESS *****/
155  /* terminate all networking connections */
156  enet_host_destroy( server );
157
158  /* clear the list of connected game servers */
159  /* clear the list of connected game clients */
160
161  /* free all used memory */
162  if( event ) free( event );
163
164  /* all done */
165  return EXIT_SUCCESS;
166} 
Note: See TracBrowser for help on using the repository browser.