Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
exserver.c
Go to the documentation of this file.
1
13 #define ALLEGRO_USE_CONSOLE
14 #define _SERVER_ "1.0 b"
15
16
17
18 #include <nilorea.h> /* NILOREA ENGINE HEADER */
19
20
21 NETWORK *server = NULL ,
22 *newclient = NULL ;
23
24 N_USER USERLIST;
25
26 static pthread_t th1;
27
28
29 void *client( void *CClient );
30
31
32
33 int MAX_CLIENTS = 0 , /* nb max client for the server */
34 RESERVED = 0 , /* nb reserved admin connection */
35 PORT = 0 , /* client connection port */
36 ADMPORT = 0 , /* admin connection port */
37 LOGGING = 0 , /* State of LOGGING, 1-ON 2-OFF */
38 sendlimit = 0 , /* Limit of Octsended/sec for each client, 0 -> disable */
39 STATE = RUNNING , /* State of the server */
40 IT = 0; /* Iterator */
41
42 char *worldmap = NULL ;
43
44 int main( void )
45 {
46 allegro_init();
47 install_timer();
48
49 set_log_level( LOG_DEBUG );
50
51 /*ShowWindow(win_get_window(),SW_HIDE);*/
52
53 n_log( LOG_INFO ,"int size: %d , double size: %d\n" , sizeof( int ) , sizeof( double ) );
54
55 n_log( LOG_INFO ,"Loading settings from file serveur.cfg . . .\n\n" );
56
57 load_server_config( "serveur.cfg" ,
58 &MAX_CLIENTS , &RESERVED , &PORT , &ADMPORT ,
59 &LOGGING , &sendlimit , &worldmap );
60
61 n_log( LOG_INFO ,"\nNetwork Config Loaded !\n" );
62
63 if (
64 save_server_config( "serveur.cfg" ,
65 MAX_CLIENTS , RESERVED , PORT , ADMPORT ,
66 LOGGING , sendlimit , worldmap ) == TRUE )
67 n_log( LOG_INFO ,"\nNetwork Config Saved !\n" );
68 else
69 n_log( LOG_INFO ,"\nNetwork Config Creating-saving FAILED ! FATAL ERROR! EXITING NOW !\n" );
70
71
72 n_log( LOG_INFO ,"Creating server:\nPort %d\nAdmin port %d\nMax clients %d\nReserved: %d\nLogging %d\nSend limit %d\n\n",
73 PORT , ADMPORT , MAX_CLIENTS , RESERVED , LOGGING , sendlimit );
74
75
76
77 /*
78 * Initialisation
79 */
80
81
82 if( create_list_of_user( &USERLIST , MAX_CLIENTS ) == FALSE )
83 {
84 n_log( LOG_INFO , "Error initializing USERLIST\n" );
85 exit( FALSE );
86 }
87
88
89 if ( Init_All_Network( 2, 2 ) == FALSE )
90 {
91 n_log( LOG_INFO , "Error initializing network\n" );
92 exit( FALSE );
93 }
94
95 else
96 n_log( LOG_INFO ,"Network Initialised\n" );
97
98 if ( Make_Listening_Network( &server , PORT , 10 ) == FALSE )
99 {
100 n_log( LOG_INFO ,"crashed on making listening socket\n" );
101 exit( FALSE );
102 }
103
104 else
105 n_log( LOG_INFO ,"Listening Network created\n" );
106
107 do
108 {
109
110 n_log( LOG_INFO ,"Waiting for new connection ...\n" );
111
112 if ( !( newclient = Accept_From_Network( server ) ) )
113 {
114 n_log( LOG_INFO , "Error on accept\n" );
115 }
116
117 else
118 {
119 n_log( LOG_INFO ,"Connection from %s accepted\n" , inet_ntoa( newclient -> link . raddr . sin_addr ) );
120 if( pthread_create ( &th1, NULL, client, ( void * ) newclient ) == 0 )
121 n_log( LOG_INFO ,"Thread Created\n" );
122 else
123 n_log( LOG_INFO ,"Error when creating main client thread\n");
124 }
125
126 }
127
128 while ( STATE == RUNNING );
129
130 /*
131 * Exiting
132 */
133
134 allegro_exit();
135
136 return TRUE;
137
138
139 }END_OF_MAIN()
140
141
142
143
144
145
146
147
148 void *client( void *CClient )
149 {
150
151 NETWORK * newnet = NULL ;
152
153 int state = TRUE ,
154 id = -1;
155
156 int d_id = 0 ;
157
158 N_STR *tmpstr = NULL , *in_msg = NULL ;
159
160
161 n_log( LOG_INFO ,"New thread\n" );
162
163 if( Create_Send_Thread( newnet ) == FALSE )
164 TRACE("Error creating sending thread\n");
165 if( Create_Recv_Thread( newnet ) == FALSE )
166 TRACE("Error creating receiving thread\n");
167
168 id = add_user( &USERLIST );
169
170 n_log( LOG_INFO ,"User added with id %d\n" , id );
171
172 pthread_mutex_lock( &USERLIST . user_bolt );
173
174 newnet = ( NETWORK * ) CClient;
175 USERLIST . list [id] . netw = newnet;
176
177 pthread_mutex_unlock( &USERLIST . user_bolt );
178
179 /* wait for ident msg */
180 do
181 {
182 in_msg = Get_Msg( newnet );
183 rest( 10 );
184 }while( !in_msg );
185
186 n_log( LOG_NOTICE , "received %d octets" , in_msg -> length );
187
188 if( in_msg && ( netw_msg_get_type( in_msg ) == NETMSG_IDENT_REQUEST ) )
189 {
190 N_STR *name = NULL , *passwd = NULL ;
191 int d_it = 0 , ident = 0 ;
192 netw_get_ident( in_msg , &d_it , &ident , &name , &passwd );
193 netw_send_ident( newnet , NETMSG_IDENT_REPLY_OK , d_id , name , passwd );
194 n_log( LOG_NOTICE , "Ident %d sended" , id);
195 }
196 else
197 {
198 n_log( LOG_ERR , "ident error. in_msg: %p , %d" , in_msg , in_msg?netw_msg_get_type( in_msg ):-1 );
199 goto exit_client;
200 }
201 free_str( &in_msg );
202
203
204 do{
205
206 tmpstr = Get_Msg( newnet );
207
208 if( tmpstr )
209 {
210 add_msg_to_all( &USERLIST , tmpstr );
211 free_str( &tmpstr );
212 rest(0);
213 }else
214 rest( 10 );
215
216 pthread_mutex_lock( &newnet -> eventbolt );
217 if( newnet -> state == NETW_QUIT )
218 state = FALSE;
219 pthread_mutex_unlock( &newnet -> eventbolt );
220
221 }while( state == TRUE );
222
223
224 exit_client :
225 Stop_Network( &USERLIST . list [id] . netw );
226 Close_Network( &USERLIST . list [id] . netw );
227
228 n_log( LOG_INFO ,"EXIT Status: %d\n", state );
229
230 rem_user( &USERLIST , id );
231
232 n_log( LOG_INFO ,"Connection with id %d closed\n" , id );
233
234 return NULL;
235
236 }