Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/masterserver/MasterServer.cc @ 7657

Last change on this file since 7657 was 7657, checked in by smerkli, 13 years ago

it has compiled.

File size: 8.2 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#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  MasterServer::eventConnect( ENetEvent *event )
43  { /* check for bad parameters */
44    if( !event )
45    { COUT(2) << "MasterServer::eventConnect: No event given.\n" ;
46      return -1;
47    }
48
49    /* convert address to string. */
50    char *addrconv = (char *) calloc( 50, 1 );
51    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
52
53    /* output debug info */
54    COUT(4) << "A new client connected from " 
55      << addrconv
56      << " on port " 
57      << event->peer->address.port << "\n";
58
59    /* store string form of address here */
60    event->peer->data = addrconv; 
61
62    /* all fine. */
63    return 0;
64  }
65
66  /* disconnect event */
67  int 
68  MasterServer::eventDisconnect( ENetEvent *event )
69  { /* check for bad parameters */
70    if( !event )
71    { COUT(2) << "No event given.\n";
72      return -1;
73    }
74
75    /* output that the disconnect happened */
76    COUT(4) << (char*)event->peer->data << " disconnected.\n";
77
78    /* create string from peer data */
79    std::string name = std::string( (char*)event->peer->data );
80
81    /* remove the server from the list it belongs to */
82    this->mainlist.delServerByName( name );
83
84    /* Reset the peer's client information. */
85    if( event->peer->data ) free( event->peer->data );
86
87    /* done */
88    return 0;
89  }
90
91  /* data event */
92  int 
93  MasterServer::eventData( ENetEvent *event )
94  { /* validate packet */
95    if( !event || !(event->packet) || !(event->peer) )
96      //|| !(event->packet->data) || !strlen(event->packet->data) )
97    { COUT(2) << "No complete event given.\n";
98      return -1;
99    }
100     
101    /* generate address in readable form */
102    char *addrconv = (char *) calloc( 50, 1 );
103    enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
104
105    /* DEBUG */
106    /* output debug info about the data that has come, to be removed */
107    COUT(4) << "A packet of length" 
108      << event->packet->dataLength
109      << " containing "
110      << (const char*)event->packet->data
111      << " was received from "
112      << addrconv
113      << " on channel "
114      << event->channelID << "\n";
115
116    /*
117    //[> send some packet back for testing <]
118    //[> TESTING <]
119
120    //[> Create a reliable reply of size 7 containing "reply\0" <]
121    //ENetPacket * reply = enet_packet_create ("reply",
122        //strlen ("reply") + 1,
123        //ENET_PACKET_FLAG_RELIABLE);
124
125    //[> Send the reply to the peer over channel id 0. <]
126    //enet_peer_send( event->peer, 0, reply );
127
128    //[> One could just use enet_host_service() instead. <]
129    //enet_host_flush( this->server );
130
131    //[> /TESTING <]
132    */
133
134    /* GAME SERVER OR CLIENT CONNECTION? */
135    if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER, 
136      MSPROTO_GAME_SERVER_LEN ) )
137    { /* Game server */
138
139      if( !strncmp( (char *)event->packet->data
140        + MSPROTO_GAME_SERVER_LEN+1, 
141        MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
142      { /* register new server */
143        mainlist.addServer( packet::ServerInformation( event ) );
144      }
145    }
146    else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT, 
147      MSPROTO_CLIENT_LEN) )
148    { /* client */
149     
150      if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
151        MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
152      { /* send server list */
153       
154        /* get an iterator */
155        std::list<packet::ServerInformation>::iterator i;
156
157        /* loop through list elements */
158        for( i = mainlist.serverlist.begin(); i != mainlist.serverlist.end(); ++i ) 
159        {
160          /* WORK MARK */
161          /* send this particular server */
162          /* build reply string */
163          char *tosend = (char *)calloc( (*i).getServerIP().length() + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
164          sprintf( "%s %s", MSPROTO_SERVERLIST_ITEM, (*i).getServerIP().c_str() );
165
166          /* create packet from it */
167          ENetPacket * reply = enet_packet_create( tosend,
168            strlen( tosend ) + 1, 
169            ENET_PACKET_FLAG_RELIABLE);
170
171          /* Send the reply to the peer over channel id 0. */
172          enet_peer_send( event->peer, 0, reply );
173
174          /* One could just use enet_host_service() instead. */
175          enet_host_flush( this->server );
176        } 
177      }
178    }
179    else
180    { /* bad message, don't do anything. */ } 
181
182    /* delete addrconv */
183    if( addrconv ) free( addrconv );
184
185    /* Clean up the packet now that we're done using it. */
186    enet_packet_destroy( event->packet );
187    return 0;
188  }
189
190
191  /**** MAIN ROUTINE *****/
192  int 
193  MasterServer::run()
194  {
195    /***** ENTER MAIN LOOP *****/
196    ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
197    if( event == NULL )
198    { 
199      COUT(1) << "Could not create ENetEvent structure, exiting.\n";
200      exit( EXIT_FAILURE );
201    }
202
203    /* tell people we're now initialized and blocking. */
204    COUT(0) << "MasterServer initialized, waiting for connections.\n";
205
206    /* create an iterator for the loop */
207    while( enet_host_service( this->server, event, 1000 ) >= 0 )
208    { /* check what type of event it is and react accordingly */
209      switch (event->type)
210      { /* new connection */
211        case ENET_EVENT_TYPE_CONNECT: 
212          eventConnect( event ); break;
213
214          /* disconnect */
215        case ENET_EVENT_TYPE_DISCONNECT: 
216          eventDisconnect( event ); break;
217
218          /* incoming data */
219        case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
220        default: break;
221      }
222    }
223
224    /* free the event */
225    if( event ) free( event );
226
227    /* done */
228    return 0;
229  } 
230
231  /* constructor */
232  MasterServer::MasterServer()
233  {
234    /***** INITIALIZE NETWORKING *****/
235    if( enet_initialize () != 0)
236    { COUT(1) << "An error occurred while initializing ENet.\n";
237      exit( EXIT_FAILURE );
238    }
239
240    /* register deinitialization */
241    atexit( enet_deinitialize );
242
243    /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
244    this->address.host = ENET_HOST_ANY;
245    this->address.port = ORX_MSERVER_PORT;
246
247    /* create a host with the above settings (the last two 0 mean: accept
248     * any input/output bandwidth */
249    this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS, 
250        ORX_MSERVER_MAXCHANS, 0, 0 );     
251
252    /* see if creation worked */
253    if( !this->server )
254    { COUT(1) << 
255        "An error occurred while trying to create an ENet server host.\n";
256      exit( EXIT_FAILURE );
257    }
258
259    /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
260    //this->mainlist = new ServerList();
261    this->peers = new PeerList();
262    //if( this->mainlist == NULL || this->peers == NULL )
263    //{ COUT(1) << "Error creating server or peer list.\n";
264      //exit( EXIT_FAILURE );
265    //}
266
267    /* run the main method */
268    run();
269  }
270
271  /* destructor */
272  MasterServer::~MasterServer()
273  {
274    /***** CLEANUP PROCESS *****/
275    /* terminate all networking connections */
276    enet_host_destroy( this->server );
277
278    /* free all used memory */
279    /* clear the list of connected game servers */
280    /* clear the list of connected game clients */
281
282  }
283
284/* end of namespace */
285}
Note: See TracBrowser for help on using the repository browser.