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