1 | /* |
---|
2 | |
---|
3 | This file is a combination of : |
---|
4 | src/net/unix/unixnfc.c |
---|
5 | src/net/unix/netdrv.c |
---|
6 | src/net/unix/undrv.c |
---|
7 | |
---|
8 | netdrv & undrv compile to a stand-alone program with talk with unixnfc |
---|
9 | via a FIFO in /tmp, using a RPC-like scheme. This versions runs inside |
---|
10 | of a abuse and therefore is a bit simpler. |
---|
11 | |
---|
12 | |
---|
13 | */ |
---|
14 | #include "demo.hpp" |
---|
15 | #include "macs.hpp" |
---|
16 | #include "specs.hpp" |
---|
17 | #include "level.hpp" |
---|
18 | #include "game.hpp" |
---|
19 | #include <stdio.h> |
---|
20 | #include "timing.hpp" |
---|
21 | #include "fileman.hpp" |
---|
22 | #include "netface.hpp" |
---|
23 | |
---|
24 | #include "gserver.hpp" |
---|
25 | #include "gclient.hpp" |
---|
26 | #include "dprint.hpp" |
---|
27 | #include "netcfg.hpp" |
---|
28 | |
---|
29 | extern char *symbol_str(char *name); |
---|
30 | |
---|
31 | #ifdef __WATCOMC__ |
---|
32 | #define getlogin() "DOS user" |
---|
33 | #endif |
---|
34 | |
---|
35 | base_memory_struct *base; // points to shm_addr |
---|
36 | base_memory_struct local_base; |
---|
37 | net_address *net_server=NULL; |
---|
38 | extern int registered; |
---|
39 | net_protocol *prot=NULL; |
---|
40 | net_socket *comm_sock=NULL,*game_sock=NULL; |
---|
41 | extern char lsf[256]; |
---|
42 | game_handler *game_face=NULL; |
---|
43 | int local_client_number=0; // 0 is the server |
---|
44 | join_struct *join_array=NULL; // points to an array of possible joining clients |
---|
45 | extern char *get_login(); |
---|
46 | extern void set_login(char *name); |
---|
47 | |
---|
48 | |
---|
49 | int net_init(int argc, char **argv) |
---|
50 | { |
---|
51 | int i,x,db_level=0; |
---|
52 | base=&local_base; |
---|
53 | |
---|
54 | local_client_number=0; |
---|
55 | |
---|
56 | if (!main_net_cfg) |
---|
57 | main_net_cfg=new net_configuration; |
---|
58 | |
---|
59 | if (!registered) |
---|
60 | return 0; |
---|
61 | |
---|
62 | for (i=1;i<argc;i++) |
---|
63 | { |
---|
64 | if (!strcmp(argv[i],"-nonet")) |
---|
65 | { |
---|
66 | printf( "Net: Disabled (-nonet)\n" ); |
---|
67 | return 0; |
---|
68 | } |
---|
69 | else if (!strcmp(argv[i],"-port")) |
---|
70 | { |
---|
71 | if( i == argc - 1 || |
---|
72 | !sscanf( argv[i + 1], "%d", &x ) || |
---|
73 | x < 1 || x > 0x7fff ) |
---|
74 | { |
---|
75 | fprintf( stderr, "Net: Bad value following -port, use 1..32000\n" ); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | main_net_cfg->port = x; |
---|
81 | } |
---|
82 | } |
---|
83 | else if( !strcmp( argv[i], "-net" ) && i < argc-1 ) |
---|
84 | { |
---|
85 | i++; |
---|
86 | strcpy( main_net_cfg->server_name, argv[i] ); |
---|
87 | main_net_cfg->state = net_configuration::CLIENT; |
---|
88 | } |
---|
89 | else if (!strcmp(argv[i],"-ndb")) |
---|
90 | { |
---|
91 | if (i==argc-1 || !sscanf(argv[i+1],"%d",&x) || x<1 || x>3) |
---|
92 | { |
---|
93 | fprintf(stderr,"Net: Bad value following -ndb, use 1..3\n" ); |
---|
94 | return 0; |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | db_level = x; |
---|
99 | } |
---|
100 | } |
---|
101 | else if( !strcmp( argv[i], "-server" ) ) |
---|
102 | { |
---|
103 | main_net_cfg->state = net_configuration::SERVER; |
---|
104 | } |
---|
105 | else if( !strcmp( argv[i], "-min_players" ) ) |
---|
106 | { |
---|
107 | i++; |
---|
108 | int x = atoi( argv[i] ); |
---|
109 | if (x >= 1 && x <= 8) |
---|
110 | { |
---|
111 | main_net_cfg->min_players=x; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | fprintf(stderr,"bad value for min_players use 1..8\n"); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | net_protocol *n = net_protocol::first, *usable = NULL; // find a usable protocol from installed list |
---|
121 | int total_usable = 0; |
---|
122 | for( ; n; n = n->next ) // show a list of usables, just to be cute |
---|
123 | { |
---|
124 | fprintf( stderr, "Protocol %s : ",n->installed() ? "Installed" : "Not_installed" ); |
---|
125 | fprintf( stderr, "%s\n", n->name() ); |
---|
126 | if( n->installed() ) |
---|
127 | { |
---|
128 | total_usable++; |
---|
129 | usable=n; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | if (!usable) |
---|
134 | { |
---|
135 | fprintf(stderr,"Net: No network protocols installed\n"); |
---|
136 | return 0; |
---|
137 | } |
---|
138 | prot=usable; |
---|
139 | prot->set_debug_printing((net_protocol::debug_type)db_level); |
---|
140 | if (main_net_cfg->state==net_configuration::SERVER) |
---|
141 | set_login(main_net_cfg->name); |
---|
142 | |
---|
143 | comm_sock=game_sock=NULL; |
---|
144 | if (main_net_cfg->state==net_configuration::CLIENT) |
---|
145 | { |
---|
146 | dprintf("Attempting to locate server %s, please wait\n",main_net_cfg->server_name); |
---|
147 | char *sn=main_net_cfg->server_name; |
---|
148 | net_server=prot->get_node_address(sn,DEFAULT_COMM_PORT,0); |
---|
149 | if (!net_server) |
---|
150 | { |
---|
151 | dprintf(symbol_str("unable_locate")); |
---|
152 | exit(0); |
---|
153 | } |
---|
154 | dprintf("Server located! Please wait while data loads....\n"); |
---|
155 | } |
---|
156 | |
---|
157 | fman=new file_manager(argc,argv,prot); // manages remote file access |
---|
158 | game_face=new game_handler; |
---|
159 | join_array=(join_struct *)jmalloc(sizeof(join_struct)*MAX_JOINERS,"join array"); |
---|
160 | base->join_list=NULL; |
---|
161 | base->mem_lock=0; |
---|
162 | base->calc_crcs=0; |
---|
163 | base->get_lsf=0; |
---|
164 | base->wait_reload=0; |
---|
165 | base->need_reload=0; |
---|
166 | base->input_state=INPUT_COLLECTING; |
---|
167 | base->current_tick=0; |
---|
168 | base->packet.packet_reset(); |
---|
169 | |
---|
170 | return 1; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | int net_start() // is the game starting up off the net? (i.e. -net hostname) |
---|
177 | { return (main_net_cfg && main_net_cfg->state==net_configuration::CLIENT); } |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | int kill_net() |
---|
182 | { |
---|
183 | if (game_face) delete game_face; game_face=NULL; |
---|
184 | if (join_array) jfree(join_array); join_array=NULL; |
---|
185 | if (game_sock) { delete game_sock; game_sock=NULL; } |
---|
186 | if (comm_sock) { delete comm_sock; comm_sock=NULL; } |
---|
187 | delete fman; fman=NULL; |
---|
188 | if (net_server) { delete net_server; net_server=NULL; } |
---|
189 | if (prot) |
---|
190 | { |
---|
191 | |
---|
192 | prot->cleanup(); |
---|
193 | prot=NULL; |
---|
194 | return 1; |
---|
195 | } else return 0; |
---|
196 | } |
---|
197 | |
---|
198 | void net_uninit() |
---|
199 | { |
---|
200 | kill_net(); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | int NF_set_file_server(net_address *addr) |
---|
205 | { |
---|
206 | if (prot) |
---|
207 | { |
---|
208 | fman->set_default_fs(addr); |
---|
209 | net_socket *sock=prot->connect_to_server(addr,net_socket::SOCKET_SECURE); |
---|
210 | |
---|
211 | if (!sock) { printf("set_file_server::connect failed\n"); return 0; } |
---|
212 | uchar cmd=CLIENT_CRC_WAITER; |
---|
213 | if ( (sock->write(&cmd,1)!=1 && printf("set_file_server::writefailed\n")) || |
---|
214 | (sock->read(&cmd,1)!=1 && printf("set_file_server::read failed\n"))) // wait for confirmation that crc's are written |
---|
215 | { delete sock; return 0; } |
---|
216 | delete sock; |
---|
217 | return cmd; |
---|
218 | } else return 0; |
---|
219 | } |
---|
220 | |
---|
221 | int NF_set_file_server(char *name) |
---|
222 | { |
---|
223 | if (prot) |
---|
224 | { |
---|
225 | net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0); |
---|
226 | if (addr) |
---|
227 | { |
---|
228 | int ret=NF_set_file_server(addr); |
---|
229 | delete addr; |
---|
230 | return ret; |
---|
231 | } else return 0; |
---|
232 | } else return 0; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | int NF_open_file(char *filename, char *mode) |
---|
237 | { |
---|
238 | if (prot) |
---|
239 | return fman->rf_open_file(filename,mode); |
---|
240 | else return -2; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | long NF_close(int fd) |
---|
245 | { |
---|
246 | if (prot) |
---|
247 | return fman->rf_close(fd); |
---|
248 | else return 0; |
---|
249 | } |
---|
250 | |
---|
251 | long NF_read(int fd, void *buf, long size) |
---|
252 | { |
---|
253 | if (prot) |
---|
254 | return fman->rf_read(fd,buf,size); |
---|
255 | else return 0; |
---|
256 | } |
---|
257 | |
---|
258 | long NF_filelength(int fd) |
---|
259 | { |
---|
260 | if (prot) |
---|
261 | return fman->rf_file_size(fd); |
---|
262 | else return 0; |
---|
263 | } |
---|
264 | |
---|
265 | long NF_seek(int fd, long offset) |
---|
266 | { |
---|
267 | if (prot) |
---|
268 | return fman->rf_seek(fd,offset); |
---|
269 | else return 0; |
---|
270 | } |
---|
271 | |
---|
272 | long NF_tell(int fd) |
---|
273 | { |
---|
274 | if (prot) |
---|
275 | return fman->rf_tell(fd); |
---|
276 | else return 0; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | void service_net_request() |
---|
281 | { |
---|
282 | if (prot) |
---|
283 | { |
---|
284 | if (prot->select(0)) // anything happening net-wise? |
---|
285 | { |
---|
286 | if (comm_sock && comm_sock->ready_to_read()) // new connection? |
---|
287 | { |
---|
288 | net_address *addr; |
---|
289 | |
---|
290 | net_socket *new_sock=comm_sock->accept(addr); |
---|
291 | if (new_sock) |
---|
292 | { |
---|
293 | uchar client_type; |
---|
294 | if (new_sock->read(&client_type,1)!=1) |
---|
295 | { |
---|
296 | delete addr; |
---|
297 | delete new_sock; |
---|
298 | } |
---|
299 | else |
---|
300 | { |
---|
301 | switch (client_type) |
---|
302 | { |
---|
303 | case CLIENT_NFS : |
---|
304 | { |
---|
305 | delete addr; |
---|
306 | fman->add_nfs_client(new_sock); |
---|
307 | } break; |
---|
308 | case CLIENT_CRC_WAITER : |
---|
309 | { |
---|
310 | crc_man.write_crc_file(NET_CRC_FILENAME); // return 0 on failure |
---|
311 | client_type=1; // confirmation byte |
---|
312 | new_sock->write(&client_type,1); |
---|
313 | delete new_sock; // done with this socket now |
---|
314 | delete addr; |
---|
315 | } break; |
---|
316 | case CLIENT_LSF_WAITER : // wants to know which .lsp file to start with |
---|
317 | { |
---|
318 | uchar len=strlen(lsf); |
---|
319 | new_sock->write(&len,1); |
---|
320 | new_sock->write(lsf,len); |
---|
321 | delete new_sock; |
---|
322 | delete addr; |
---|
323 | } break; |
---|
324 | default : |
---|
325 | { |
---|
326 | if (game_face->add_client(client_type,new_sock,addr)==0) // ask server or client to add new client |
---|
327 | { |
---|
328 | delete addr; |
---|
329 | delete new_sock; |
---|
330 | } |
---|
331 | } break; |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | if (!game_face->process_net()) |
---|
337 | { |
---|
338 | delete game_face; |
---|
339 | game_face=new game_handler; |
---|
340 | } |
---|
341 | fman->process_net(); |
---|
342 | } |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | int get_remote_lsf(net_address *addr, char *filename) // filename should be 256 bytes |
---|
348 | { |
---|
349 | if (prot) |
---|
350 | { |
---|
351 | net_socket *sock=prot->connect_to_server(addr,net_socket::SOCKET_SECURE); |
---|
352 | if (!sock) return 0; |
---|
353 | |
---|
354 | uchar ctype=CLIENT_LSF_WAITER; |
---|
355 | uchar len; |
---|
356 | |
---|
357 | if (sock->write(&ctype,1)!=1 || |
---|
358 | sock->read(&len,1)!=1 || len==0 || |
---|
359 | sock->read(filename,len)!=len) |
---|
360 | { |
---|
361 | delete sock; |
---|
362 | return 0; |
---|
363 | } |
---|
364 | |
---|
365 | delete sock; |
---|
366 | return 1; |
---|
367 | |
---|
368 | } else return 0; |
---|
369 | } |
---|
370 | |
---|
371 | void server_check() { ; } |
---|
372 | |
---|
373 | int request_server_entry() |
---|
374 | { |
---|
375 | if (prot && main_net_cfg) |
---|
376 | { |
---|
377 | if (!net_server) return 0; |
---|
378 | |
---|
379 | if (game_sock) delete game_sock; |
---|
380 | dprintf("Joining game in progress, hang on....\n"); |
---|
381 | |
---|
382 | game_sock=prot->create_listen_socket(main_net_cfg->port+1,net_socket::SOCKET_FAST); // this is used for fast game packet transmission |
---|
383 | if (!game_sock) { if (comm_sock) delete comm_sock; comm_sock=NULL; prot=NULL; return 0; } |
---|
384 | game_sock->read_selectable(); |
---|
385 | |
---|
386 | net_socket *sock=prot->connect_to_server(net_server,net_socket::SOCKET_SECURE); |
---|
387 | if (!sock) |
---|
388 | { |
---|
389 | fprintf(stderr,"unable to connect to server\n"); |
---|
390 | return 0; |
---|
391 | } |
---|
392 | |
---|
393 | uchar ctype=CLIENT_ABUSE; |
---|
394 | ushort port=lstl(main_net_cfg->port+1),cnum; |
---|
395 | |
---|
396 | uchar reg; |
---|
397 | if (sock->write(&ctype,1)!=1 || // send server out game port |
---|
398 | sock->read(®,1)!=1) // is remote engine registered? |
---|
399 | { delete sock; return 0; } |
---|
400 | |
---|
401 | if (reg==2) // too many players |
---|
402 | { |
---|
403 | fprintf(stderr,symbol_str("max_players")); |
---|
404 | delete sock; |
---|
405 | return 0; |
---|
406 | } |
---|
407 | |
---|
408 | // maker sure the two games are both registered or unregistered or sync problems |
---|
409 | // will occur. |
---|
410 | |
---|
411 | if (reg && !registered) |
---|
412 | { |
---|
413 | fprintf(stderr,symbol_str("net_not_reg")); |
---|
414 | delete sock; |
---|
415 | return 0; |
---|
416 | } |
---|
417 | |
---|
418 | if (!reg && registered) |
---|
419 | { |
---|
420 | fprintf(stderr,symbol_str("server_not_reg")); |
---|
421 | delete sock; |
---|
422 | return 0; |
---|
423 | } |
---|
424 | |
---|
425 | char uname[256]; |
---|
426 | if (get_login()) |
---|
427 | strcpy(uname,get_login()); |
---|
428 | else strcpy(uname,"unknown"); |
---|
429 | uchar len=strlen(uname)+1; |
---|
430 | short nkills; |
---|
431 | |
---|
432 | if (sock->write(&len,1)!=1 || |
---|
433 | sock->write(uname,len)!=len || |
---|
434 | sock->write(&port,2)!=2 || // send server out game port |
---|
435 | sock->read(&port,2)!=2 || // read server's game port |
---|
436 | sock->read(&nkills,2)!=2 || |
---|
437 | sock->read(&cnum,2)!=2 || cnum==0 // read player number (cannot be 0 because 0 is server) |
---|
438 | ) |
---|
439 | { delete sock; return 0; } |
---|
440 | |
---|
441 | nkills=lstl(nkills); |
---|
442 | port=lstl(port); |
---|
443 | cnum=lstl(cnum); |
---|
444 | |
---|
445 | main_net_cfg->kills=nkills; |
---|
446 | net_address *addr=net_server->copy(); |
---|
447 | addr->set_port(port); |
---|
448 | |
---|
449 | delete game_face; |
---|
450 | game_face=new game_client(sock,addr); |
---|
451 | delete addr; |
---|
452 | |
---|
453 | local_client_number=cnum; |
---|
454 | return cnum; |
---|
455 | } else return 0; |
---|
456 | } |
---|
457 | |
---|
458 | int reload_start() |
---|
459 | { |
---|
460 | if (prot) |
---|
461 | return game_face->start_reload(); |
---|
462 | else return 0; |
---|
463 | } |
---|
464 | |
---|
465 | int reload_end() |
---|
466 | { |
---|
467 | if (prot) |
---|
468 | return game_face->end_reload(); |
---|
469 | else return 0; |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | void net_reload() |
---|
474 | { |
---|
475 | if (prot) |
---|
476 | { |
---|
477 | if (net_server) |
---|
478 | { |
---|
479 | if (current_level) |
---|
480 | delete current_level; |
---|
481 | bFILE *fp; |
---|
482 | |
---|
483 | if (!reload_start()) return ; |
---|
484 | |
---|
485 | do { // make sure server saves the file |
---|
486 | fp=open_file(NET_STARTFILE,"rb"); |
---|
487 | if (fp->open_failure()) { delete fp; fp=NULL; } |
---|
488 | } while (!fp); |
---|
489 | |
---|
490 | spec_directory sd(fp); |
---|
491 | |
---|
492 | spec_entry *e=sd.find("Copyright 1995 Crack dot Com, All Rights reserved"); |
---|
493 | if (!e) |
---|
494 | { |
---|
495 | the_game->show_help("This level is missing copyright information, cannot load\n"); |
---|
496 | current_level=new level(100,100,"untitled"); |
---|
497 | the_game->need_refresh(); |
---|
498 | } |
---|
499 | else |
---|
500 | current_level=new level(&sd,fp,NET_STARTFILE); |
---|
501 | |
---|
502 | delete fp; |
---|
503 | base->current_tick=(current_level->tick_counter()&0xff); |
---|
504 | |
---|
505 | reload_end(); |
---|
506 | } else if (current_level) |
---|
507 | { |
---|
508 | |
---|
509 | join_struct *join_list=base->join_list; |
---|
510 | |
---|
511 | |
---|
512 | while (join_list) |
---|
513 | { |
---|
514 | |
---|
515 | view *f=player_list; |
---|
516 | for (;f && f->next;f=f->next); // find last player, add one for pn |
---|
517 | int i,st=0; |
---|
518 | for (i=0;i<total_objects;i++) |
---|
519 | if (!strcmp(object_names[i],"START")) |
---|
520 | st=i; |
---|
521 | |
---|
522 | game_object *o=create(current_start_type,0,0); |
---|
523 | game_object *start=current_level->get_random_start(320,NULL); |
---|
524 | if (start) { o->x=start->x; o->y=start->y; } |
---|
525 | else { o->x=100; o->y=100; } |
---|
526 | |
---|
527 | f->next=new view(o,NULL,join_list->client_id); |
---|
528 | strcpy(f->next->name,join_list->name); |
---|
529 | o->set_controller(f->next); |
---|
530 | |
---|
531 | if (start) |
---|
532 | current_level->add_object_after(o,start); |
---|
533 | else |
---|
534 | current_level->add_object(o); |
---|
535 | |
---|
536 | view *v=f->next; |
---|
537 | |
---|
538 | v->cx1=5; |
---|
539 | v->cy1=5; |
---|
540 | v->cx2=319-5; |
---|
541 | v->cy2=199-5; |
---|
542 | join_list=join_list->next; |
---|
543 | } |
---|
544 | base->join_list=NULL; |
---|
545 | current_level->save(NET_STARTFILE,1); |
---|
546 | base->mem_lock=0; |
---|
547 | |
---|
548 | |
---|
549 | jwindow *j=eh->new_window(0,yres/2,-1,-1,new info_field(WINDOW_FRAME_LEFT, |
---|
550 | WINDOW_FRAME_TOP, |
---|
551 | 0,symbol_str("resync"), |
---|
552 | new button(WINDOW_FRAME_LEFT, |
---|
553 | WINDOW_FRAME_TOP+eh->font()->height()+5,ID_NET_DISCONNECT, |
---|
554 | symbol_str("slack"),NULL)),symbol_str("hold!")) |
---|
555 | ; |
---|
556 | |
---|
557 | |
---|
558 | |
---|
559 | eh->flush_screen(); |
---|
560 | if (!reload_start()) return ; |
---|
561 | |
---|
562 | base->input_state=INPUT_RELOAD; // if someone missed the game tick with the RELOAD data in it, make sure the get it |
---|
563 | |
---|
564 | // wait for all client to reload the level with the new players |
---|
565 | do |
---|
566 | { |
---|
567 | service_net_request(); |
---|
568 | if (eh->event_waiting()) |
---|
569 | { |
---|
570 | event ev; |
---|
571 | do |
---|
572 | { |
---|
573 | eh->get_event(ev); |
---|
574 | if (ev.type==EV_MESSAGE && ev.message.id==ID_NET_DISCONNECT) |
---|
575 | { |
---|
576 | game_face->end_reload(1); |
---|
577 | base->input_state=INPUT_PROCESSING; |
---|
578 | } |
---|
579 | |
---|
580 | } while (eh->event_waiting()); |
---|
581 | |
---|
582 | eh->flush_screen(); |
---|
583 | } |
---|
584 | |
---|
585 | } while (!reload_end()); |
---|
586 | eh->close_window(j); |
---|
587 | unlink(NET_STARTFILE); |
---|
588 | |
---|
589 | the_game->reset_keymap(); |
---|
590 | |
---|
591 | base->input_state=INPUT_COLLECTING; |
---|
592 | |
---|
593 | } |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | |
---|
598 | int client_number() { return local_client_number; } |
---|
599 | |
---|
600 | |
---|
601 | void send_local_request() |
---|
602 | { |
---|
603 | |
---|
604 | if (prot) |
---|
605 | { |
---|
606 | if (current_level) |
---|
607 | base->current_tick=(current_level->tick_counter()&0xff); |
---|
608 | game_face->add_engine_input(); |
---|
609 | } else base->input_state=INPUT_PROCESSING; |
---|
610 | |
---|
611 | } |
---|
612 | |
---|
613 | |
---|
614 | void kill_slackers() |
---|
615 | { |
---|
616 | if (prot) |
---|
617 | { |
---|
618 | if (!game_face->kill_slackers()) |
---|
619 | { |
---|
620 | delete game_face; |
---|
621 | game_face=new game_handler(); |
---|
622 | } |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | int get_inputs_from_server(unsigned char *buf) |
---|
627 | { |
---|
628 | if (prot && base->input_state!=INPUT_PROCESSING) // if input is not here, wait on it |
---|
629 | { |
---|
630 | time_marker start; |
---|
631 | |
---|
632 | int total_retry=0; |
---|
633 | jwindow *abort=NULL; |
---|
634 | |
---|
635 | while (base->input_state!=INPUT_PROCESSING) |
---|
636 | { |
---|
637 | if (!prot) |
---|
638 | { |
---|
639 | base->input_state=INPUT_PROCESSING; |
---|
640 | return 1; |
---|
641 | } |
---|
642 | service_net_request(); |
---|
643 | |
---|
644 | time_marker now; // if this is taking to long, the packet was probably lost, ask for it to be resent |
---|
645 | |
---|
646 | if (now.diff_time(&start)>0.05) |
---|
647 | { |
---|
648 | if (prot->debug_level(net_protocol::DB_IMPORTANT_EVENT)) |
---|
649 | fprintf(stderr,"(missed packet)"); |
---|
650 | |
---|
651 | |
---|
652 | game_face->input_missing(); |
---|
653 | start.get_time(); |
---|
654 | |
---|
655 | total_retry++; |
---|
656 | if (total_retry==12000) // 2 minutes and nothing |
---|
657 | { |
---|
658 | abort=eh->new_window(0,yres/2,-1,eh->font()->height()*4, |
---|
659 | new info_field(WINDOW_FRAME_LEFT, |
---|
660 | WINDOW_FRAME_TOP, |
---|
661 | 0,symbol_str("waiting"), |
---|
662 | new button(WINDOW_FRAME_LEFT, |
---|
663 | WINDOW_FRAME_TOP+eh->font()->height()+5,ID_NET_DISCONNECT, |
---|
664 | symbol_str("slack"),NULL)),symbol_str("Error")); |
---|
665 | eh->flush_screen(); |
---|
666 | } |
---|
667 | } |
---|
668 | if (abort) |
---|
669 | { |
---|
670 | if (eh->event_waiting()) |
---|
671 | { |
---|
672 | event ev; |
---|
673 | do |
---|
674 | { |
---|
675 | eh->get_event(ev); |
---|
676 | if (ev.type==EV_MESSAGE && ev.message.id==ID_NET_DISCONNECT) |
---|
677 | { |
---|
678 | kill_slackers(); |
---|
679 | base->input_state=INPUT_PROCESSING; |
---|
680 | } |
---|
681 | } while (eh->event_waiting()); |
---|
682 | |
---|
683 | eh->flush_screen(); |
---|
684 | } |
---|
685 | } |
---|
686 | } |
---|
687 | |
---|
688 | if (abort) |
---|
689 | { |
---|
690 | eh->close_window(abort); |
---|
691 | the_game->reset_keymap(); |
---|
692 | |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | |
---|
697 | memcpy(base->last_packet.data,base->packet.data,base->packet.packet_size()+base->packet.packet_prefix_size()); |
---|
698 | |
---|
699 | int size=base->packet.packet_size(); |
---|
700 | memcpy(buf,base->packet.packet_data(),size); |
---|
701 | |
---|
702 | base->packet.packet_reset(); |
---|
703 | base->mem_lock=0; |
---|
704 | |
---|
705 | return size; |
---|
706 | } |
---|
707 | |
---|
708 | int become_server(char *name) |
---|
709 | { |
---|
710 | if (prot && main_net_cfg) |
---|
711 | { |
---|
712 | delete game_face; |
---|
713 | |
---|
714 | if (comm_sock) delete comm_sock; |
---|
715 | comm_sock=prot->create_listen_socket(main_net_cfg->port,net_socket::SOCKET_SECURE); // this is used for incomming connections |
---|
716 | if (!comm_sock) { prot=NULL; return 0; } |
---|
717 | comm_sock->read_selectable(); |
---|
718 | prot->start_notify(0x9090, name, strlen(name)); // should we define a new socket for notifiers? |
---|
719 | |
---|
720 | if (game_sock) delete game_sock; |
---|
721 | game_sock=prot->create_listen_socket(main_net_cfg->port+1,net_socket::SOCKET_FAST); // this is used for fast game packet transmission |
---|
722 | if (!game_sock) { if (comm_sock) delete comm_sock; comm_sock=NULL; prot=NULL; return 0; } |
---|
723 | game_sock->read_selectable(); |
---|
724 | |
---|
725 | game_face=new game_server; |
---|
726 | local_client_number=0; |
---|
727 | return 1; |
---|
728 | } |
---|
729 | return 0; |
---|
730 | } |
---|
731 | |
---|
732 | void read_new_views() { ; } |
---|
733 | |
---|
734 | |
---|
735 | void wait_min_players() |
---|
736 | { |
---|
737 | if (game_face) game_face->game_start_wait(); |
---|
738 | } |
---|