Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Further implementation, constructing a testing environment soon.

File size: 5.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "MasterServer.h"
30
31using namespace std;
32
33/***** EVENTS *****/
34/* connect event */
35int eventConnect( ENetEvent *event )
36{ /* check for bad parameters */
37  if( !event )
38  { fprintf( stderr, "No event given.\n" );
39    return -1;
40  }
41
42  /* output debug info */
43  //printf( "A new client connected from %x:%u.\n",
44      //event->peer->address.host,
45      //event->peer->address.port);
46
47  /* game server or client connection? */
48    /* game server */
49      /* add to game server list */
50    /* client */
51      /* add to client list */
52 
53  /* NOTE this seems to be some weird snipped from the tutorial as peer.data
54   * is a uint_32 of some kind and hence shouldn't be assigned a c string? :S
55   */
56  /* Store any relevant client information here. */
57  /* event->peer->data = "Client information"; */
58  /* /NOTE */
59  return 0;
60}
61
62/* disconnect event */
63int eventDisconnect( ENetEvent *event )
64{ /* check for bad parameters */
65  if( !event )
66  { fprintf( stderr, "No event given.\n" );
67    return -1;
68  }
69
70  /* output that the disconnect happened, to be removed at a later time. */
71  //printf ("%s disconnected.\n", event->peer -> data);
72
73  /* remove the server from the list it belongs to */
74
75  /* Reset the peer's client information. */
76  event->peer->data = NULL;
77  return 0;
78}
79
80/* data event */
81int eventData( ENetEvent *event )
82{ /* output what's in the packet (to be removed later) */
83  if( !event || !(event->packet) || !(event->peer) || !(event->channelID) )
84  { fprintf( stderr, "No complete event given.\n" );
85    return -1;
86  }
87
88  /* output debug info about the data that has come, to be removed */
89  //printf( "A packet of length %u containing %s was received from %s on channel %u.\n",
90      //event->packet->dataLength,
91      //event->packet->data,
92      //event->peer->data,
93      //event->channelID );
94 
95  /* game server or client connection? */
96    /* game server */
97      /* parse data */
98      /* start actions */
99      /* and send reply */
100    /* client */
101      /* parse data */
102      /* start actions */
103      /* and send reply */
104
105  /* Clean up the packet now that we're done using it. */
106  enet_packet_destroy( event->packet );
107  return 0;
108}
109
110
111
112/**** MAIN ROUTINE *****/
113int main( int argc, char *argv[] )
114{
115  /***** INITIALIZE NETWORKING *****/
116  if( enet_initialize () != 0)
117  { fprintf( stderr, "An error occurred while initializing ENet.\n");
118    return EXIT_FAILURE;
119  }
120
121  /* register deinitialization */
122  atexit( enet_deinitialize );
123
124  /* define our address and a host structure */
125  ENetAddress address;
126  ENetHost *server;
127
128  /* Bind the server to the default localhost.     */
129  /* A specific host address can be specified by   */
130  /* enet_address_set_host (& address, "x.x.x.x"); */
131  address.host = ENET_HOST_ANY;
132
133  /* Bind the server to port 1234. */
134  address.port = ORX_MSERVER_PORT;
135
136  server = enet_host_create( & address /* the address to bind the server host to */, 
137      ORX_MSERVER_MAXCONNS      /* allow up to 32 clients and/or outgoing connections */,
138      ORX_MSERVER_MAXCHANS      /* allow up to 2 channels to be used, 0 and 1 */,
139      0      /* assume any amount of incoming bandwidth */,
140      0      /* assume any amount of outgoing bandwidth */);
141
142  /* see if creation worked */
143  if( !server )
144  { fprintf( stderr, 
145        "An error occurred while trying to create an ENet server host.\n" );
146    exit( EXIT_FAILURE );
147  }
148
149  /***** INITIALIZE GAME SERVER LIST *****/
150  orxonox::ServerList *mainlist = new orxonox::ServerList();
151  //if( mainlist == NULL )
152  //{ fprintf( stderr, "Error creating server list.\n" );
153    //exit( EXIT_FAILURE );
154  //}
155
156  /***** ENTER MAIN LOOP *****/
157  ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
158  if( event == NULL )
159  { fprintf( stderr, "Could not create ENetEvent structure, exiting.\n" );
160    exit( EXIT_FAILURE );
161  }
162
163  /* NOTE this only waits on one client, we need to find some way to
164   * actually listen on all active connections. This will be implemented
165   * together with the list of active connections.
166   */
167  /* Wait up to 1000 milliseconds for an event. */
168  while (enet_host_service (client, event, 1000) > 0)
169  { /* check what type of event it is and react accordingly */
170    switch (event.type)
171    { /* new connection */
172      case ENET_EVENT_TYPE_CONNECT: eventConnect( event ); break;
173
174      /* disconnect */
175      case ENET_EVENT_TYPE_DISCONNECT: eventDisconnect( event ); break;
176
177      /* incoming data */
178      case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
179    }
180  }
181
182  /***** CLEANUP PROCESS *****/
183  /* terminate all networking connections */
184  enet_host_destroy( server );
185
186  /* clear the list of connected game servers */
187  /* clear the list of connected game clients */
188
189  /* free all used memory */
190  if( event ) free( event );
191
192  /* all done */
193  return EXIT_SUCCESS;
194} 
Note: See TracBrowser for help on using the repository browser.