Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
exclient.c
Go to the documentation of this file.
1
13 #define ALLEGRO_USE_CONSOLE
14
15
16 #define _CLIENT_ "1.0 b"
17
18 /* NILOREA ENGINE HEADER */
19 #include <nilorea.h>
20
21 int heartbeat = 0 ,
22 BLITTIME = 16666 , /* Time between screen refresh */
23 UPDATETIME = 250000 , /* Max Time between two update ( defaut 250000 microseconds ) */
24 LOGICTIME = 33333 , /* Time between two logic refresh ( defaut 10000 microseconds ) */
25 PINGTIME = 1000000 , /* Ping interval, 500 000 usec */
26 ping_time = 0 , /* ping timer */
27 elapsed_time = 0 , /* main loop timer for adjusting rest/draw/logic */
28 logic_time = 0 , /* logic timer value */
29 blit_time = 0 , /* blit timer value */
30 update_time = 0 , /* update timer value */
31 auto_nice_check = 0 , /* if in a loop there was no update, this value will be 0. If not, then no rest. */
32 PORT = 0 , /* client connection port */
33 LOGGING = 0 ,/* State of LOGGING, 1-ON 2-OFF */
34 state = 0 ,
35 send_ping = 0 ,
36 cur = 0,
37 max = 1024,
38 it=0,it1=0,it2=0,
39 idle =0,
40 nboct=0,
41 scroll=0,
42 sendlimit=0,
43 med_t=0,
44 med_f=0,
45 color=0;
46
47 int d_it = 0, ident = -1 ;
48
49 char *ip = NULL, /* ip to connect */
50 *tmpstr = NULL ,
51 name[ 512 ] , /*client name */
52 buf[ 512 ] ; /* typing buffer */
53
54 N_STR *in_buf = NULL ,
55 *tmpbuf = NULL ,
56 *namestr = NULL ,
57 *passwd = NULL ,
58 *netw_exchange = NULL ;
59
60 BITMAP *scr_buf = NULL;
61
62 FILE *chatparamf = NULL;
63
64 NETWORK *n_client = NULL;
65
66 LIST *chatbuf = NULL;
67 N_STR *chat_line = NULL;
68 N_TIME timer;
69
70 LIST_NODE *node = NULL ;
71
72 int main( void )
73 {
74 set_log_level( LOG_DEBUG );
75
76 n_log( LOG_INFO , "\nNILOREA Client\n\nVersion: CLIENT %s ", _CLIENT_ );
77
78 n_log( LOG_INFO , "Loading settings from file client.cfg . . .\n\n" );
79
80 load_client_config( "client.cfg" , &ip , &PORT , &sendlimit , &LOGGING );
81
82 n_log( LOG_INFO , "\nNetwork Config Loaded !\n" );
83
84 if ( save_client_config( "client.cfg" , ip , PORT , sendlimit , LOGGING ) == TRUE )
85 n_log( LOG_INFO , "\nNetwork Config Saved !\n" );
86 else
87 n_log( LOG_INFO , "\nNetwork Config Creating-saving FAILED ! FATAL ERROR! EXITING NOW !\n" );
88
89 n_log( LOG_INFO , "Loading chat parameters...\n");
90
91 chatparamf = fopen( "chatparam.txt" , "rt+" );
92
93 if( !chatparamf )
94 {
95 n_log( LOG_INFO , "Error opening file chatparam.txt !\n" );
96 return FALSE;
97 }
98
99 fill_str( buf , 0 , 1024 );
100 fill_str( name , 0 , 512 );
101
102 fgets( buf , 512 , chatparamf );
103
104 ustrcpy( name , buf );
105 uremove( name , -1 );
106
107 fscanf( chatparamf , "%d %d %d" , &it , &it1 , &it2 );
108
109 fclose( chatparamf );
110
111 color = makecol( it , it1 , it2 );
112
113 n_log( LOG_INFO , "Chat params loaded, User: %s , color=%d,%d,%d\n" , name , it , it1 , it2 );
114
115 n_log( LOG_INFO , "Creating client: Ip %s\nPort %d\nLogging %d\nSendlimit: %d o/s\n", ip , PORT , LOGGING , sendlimit );
116
117 /*
118 * Initialisation
119 */
120
121
122 allegro_init();
123 install_timer();
124 install_keyboard();
125 set_uformat(U_ASCII);
126
127 /* gfx_mode( GFX_DIRECTX_WIN , 640,480,0,0,32,0,0,0,255);*/
128 set_color_depth( 16 );
129 gfx_mode( GFX_DIRECTX_WIN , 640,480,0,0,32,0,0,0,255);
130
131
132 if (set_display_switch_mode(SWITCH_BACKGROUND) != 0)
133 {
134 n_log( LOG_INFO , "Error changing switch to SWITCH_BACKGROUND ");
135 if( set_display_switch_mode(SWITCH_BACKAMNESIA) !=0)
136 {
137 n_log( LOG_INFO , "Error changing switch mode to SWITCH_BACKGROUND");
138 return FALSE;
139 }
140 }
141
142 scr_buf = create_bitmap( SCREEN_W , SCREEN_H );
143 if( !scr_buf )
144 {
145 n_log( LOG_ERR , "Error creating bitmap of size %dx%d" , SCREEN_W,SCREEN_H );
146 allegro_exit();
147 return FALSE;
148 }
149
150 Init_All_Network( 2,2);
151
152 tmpstr = Connect_Network ( &n_client , ip , PORT );
153 if( tmpstr )
154 {
155 n_log( LOG_INFO , "error while connecting %s port %d\n" , ip , PORT );
156 allegro_exit();
157 return FALSE;
158 }
159
160 n_log( LOG_INFO , "Connected !!!\n");
161
162 n_log( LOG_NOTICE ,"Sending ident request with name:\"%s\"" , name );
163
164 char_to_str( name , strlen( name ) , &namestr );
165 char_to_str( "blankpasswd" , strlen( "blankpasswd" ) , &passwd );
166 if( netw_send_ident( n_client , NETMSG_IDENT_REQUEST , 0 , namestr , passwd ) == FALSE )
167 {
168 n_log( LOG_ERR , "Sending ident request failed" );
169 goto exit_client;
170 }
171 n_log( LOG_INFO ,"Ident request sended");
172
173 netw_exchange = NULL ;
174
175 while( !netw_exchange && !key[KEY_ESC])
176 {
177 netw_exchange = Get_Msg( n_client );
178
179 if( netw_exchange )
180 {
181 break;
182 }
183 rest( 10 );
184 }
185
186 /* quitting if no right answer */
187 if( !netw_exchange || ( netw_exchange && netw_exchange -> data == NULL ) )
188 goto exit_client;
189
190 netw_get_ident( netw_exchange , &d_it , &ident , &namestr , &passwd );
191 n_log( LOG_INFO ,"Id Message received, pointer:%p size %d id %g",netw_exchange -> data, netw_exchange -> length , ident );
192
193 if( ident == -1 )
194 {
195 n_log( LOG_ERR ,"No ident received");
196 goto exit_client;
197 }
198
199 /* We got our id ! */
200 n_log( LOG_NOTICE , "Id is:%g" , ident );
201
202 free_str( &netw_exchange );
203
204 chatbuf = new_generic_list( 1000 );
205
206 n_log( LOG_INFO , "Chat Buffer Initialised\n");
207
208 fill_str( buf , 0 , 1024 );
209
210 n_log( LOG_INFO ,"Entering loop...\n");
211
212
213 start_HiTimer( &timer );
214
215 n_log( LOG_INFO ,"netw -> state = %d\n" , n_client -> state );
216 n_client -> state = NETW_RUN ;
217 n_log( LOG_INFO ,"n_client -> state = NETW_RUN ;\nnetw -> state = %d\n" , n_client -> state );
218
219 if( Create_Send_Thread( n_client ) == TRUE )
220 n_log( LOG_INFO ,"Create_Send_Thread OK , nnetw -> state = %d\n" , n_client -> state );
221 else
222 n_log( LOG_INFO ,"Create_Send_Thread NON OK , nnetw -> state = %d\n" , n_client -> state );
223
224
225 if( Create_Recv_Thread( n_client ) == TRUE )
226 n_log( LOG_INFO ,"Create_recv_Thread OK , nnetw -> state = %d\n" , n_client -> state );
227 else
228 n_log( LOG_INFO ,"Create_recv_Thread NON OK , nnetw -> state = %d\n" , n_client -> state );
229
230
231 cur = ustrlen( name );
232 ustrcpy( buf , name );
233
234 do
235 {
236 elapsed_time = get_usec( &timer );
237
238 logic_time += elapsed_time ;
239 update_time += elapsed_time ;
240 blit_time += elapsed_time ;
241
242 auto_nice_check = 0 ;
243
244 if( send_ping )
245 {
246 ping_time += elapsed_time ;
247
248 if( ping_time > PINGTIME )
249 {
250
251 while( ping_time >= PINGTIME )
252 ping_time -= PINGTIME;
253
254 it=timeGetTime();
255 netw_send_ping( n_client , NETMSG_PING_REQUEST , ident , -1 , it );
256 n_log( LOG_DEBUG ,"Sended Pinq Request, type:%d from %d to %d at %d",NETMSG_PING_REQUEST,(int)ident ,-1, it );
257
258 }
259 }
260
261 if( logic_time >= LOGICTIME )
262 {
263 auto_nice_check ++ ;
264
265 while( logic_time >= LOGICTIME )
266 logic_time -= LOGICTIME;
267
268 if( key[KEY_PGUP] )
269 {
270 rest( 10 );
271 it = chatbuf -> nb_items - 45;
272 if( it < 0 ) it = 0;
273 if( scroll < it )scroll++;
274 }
275 if( key[KEY_PGDN] )
276 {
277 rest( 10 );
278 if( scroll > 0 )
279 scroll--;
280 }
281
282 get_keyboard( buf , &cur , ustrsize( name ),max );
283
284 if( key[KEY_ENTER] && cur > ustrsize( name ) )
285 {
286 N_STR msg ;
287 msg.data = buf ;
288 msg.length = strlen( buf ) ;
289 Add_Msg( n_client, &msg );
290 fill_str( buf , 0 , max );
291 ustrcpy( buf , name );
292 cur = ustrsize( name );
293 }
294
295 in_buf = Get_Msg( n_client );
296
297 if( in_buf )
298 {
299 if( list_push( chatbuf , in_buf ) == FALSE )
300 {
301 tmpbuf = list_shift( chatbuf , N_STR );
302 free_str( &tmpbuf );
303 if( list_push( chatbuf , in_buf ) == FALSE )
304 {
305 n_log( LOG_INFO , "Error adding %s to chat\n" , in_buf -> data );
306 return FALSE;
307 }
308 }
309 free_str( &in_buf );
310 }
311 }
312
313 if( idle > BLITTIME )
314 {
315 if( key[ KEY_F11 ] && BLITTIME > 0 )
316 BLITTIME -= 10;
317 if( key[ KEY_F12 ] && BLITTIME < 100000 )
318 BLITTIME += 10;
319
320 if( gfx_status( GET , 0 ) == MODE_OGL )
321 allegro_gl_set_allegro_mode();
322
323 clear(scr_buf);
324 textprintf_ex( scr_buf , font , 0 , SCREEN_H-10 , makecol(255,255,255), 1 , "%s",buf);
325 rectfill( scr_buf , text_length(font, buf),SCREEN_H-10 , text_length(font, buf)+6,SCREEN_H,makecol(200,100,255));
326
327 node = chatbuf -> end ;
328 for( it = 0 ; it < scroll ; it ++)
329 {
330 if( node -> prev )
331 node = node -> prev ;
332 }
333
334 for( it = 0; it < 45 ; it ++ )
335 {
336 if( node )
337 {
338 chat_line = (N_STR *)node -> ptr ;
339 textprintf_ex( scr_buf , font , 5 , 460 - it*10 , makecol( 128+it2,128+it2,128+it2) ,1, "%s", chat_line -> data);
340 if( node -> prev )
341 node = node -> prev ;
342 }
343
344 textprintf_ex( scr_buf , font , 5 , 5 , makecol( 255,255,255) ,1, "fps: %d, usec/loop: %d, %s: %d, %d item, boucle: %d",((med_f>0)?1000000/med_f:9999),med_t,name,color,n_client -> send_buf -> nb_items,BLITTIME);
345 blit(scr_buf,screen,0,0,0,0,SCREEN_W,SCREEN_H);
346
347 if( gfx_status( GET , 0 ) == MODE_OGL )
348 {
349 allegro_gl_unset_allegro_mode();
350 allegro_gl_flip();
351 }
352 idle -= BLITTIME ;
353 if( idle > BLITTIME )
354 idle = 0 ;
355 else
356 rest(1);
357 }
358 rest( 1 );
359 }
360
361 if( update_time >= UPDATETIME )
362 {
363
364 auto_nice_check ++ ;
365
366 while( update_time >= UPDATETIME )
367 update_time -= UPDATETIME ;
368
369 /*
370 * Network output processing
371 */
372
373 }
374
375 if( auto_nice_check == 0 )
376 rest( 1 );
377 else
378 rest( 0 );
379
380 }while( !key[KEY_ESC] );
381
382 exit_client:
383 Stop_Network( &n_client );
384 Close_Network( &n_client );
385
386 /*
387 * Exiting
388 */
389
390 allegro_exit();
391
392 return TRUE;
393
394 }END_OF_MAIN()