Adding d1x network code
[btb/d2x.git] / main / ipserver.cpp
1 /*
2  * $Source: /cvs/cvsroot/d2x/main/ipserver.cpp,v $
3  * $Revision: 1.1 $
4  * $Author: bradleyb $
5  * $Date: 2002-02-06 09:22:42 $
6  *
7  * ipserver.cpp - udp/ip dedicated gamelist server
8  * added 2000/02/07 Matt Mueller
9  *
10  * $Log: not supported by cvs2svn $
11  *
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <signal.h>
22
23 #include "ip_base.h"
24
25 extern "C"{
26 #include "error.h"
27 #include "args.h"
28 #include "u_mem.h"
29 }
30
31 unsigned char ipx_MyAddress[10];
32
33 int ipx_general_PacketReady(int fd) {
34         fd_set set;
35         struct timeval tv;
36
37         FD_ZERO(&set);
38         FD_SET(fd, &set);
39         tv.tv_usec = 50000;//50 ms
40         tv.tv_sec = 0;
41         if (select(fd + 1, &set, NULL, NULL, &tv) > 0)
42                 return 1;
43         else
44                 return 0;
45 }
46
47 void ip_server_mainloop(void){
48         struct ipx_recv_data rd;
49         int size;
50         char buf[1500];
51         ip_addr *fromaddr;
52 //      fix curtime;
53         while (1){
54                 while (arch_ip_PacketReady()) {
55                         if ((size =     arch_ip_recvfrom(buf, 1500, &rd)) > 4) {
56                                 memcpy(&fromaddr,rd.src_node,sizeof(ip_addr*));
57                                 if (memcmp(buf,D1Xcfgid,4)==0){
58                                         ip_receive_cfg((ubyte*)buf+4,size-4,*fromaddr);
59                                 }
60                         }
61                 }
62                 peer_list.handshake_frame();
63 //              curtime=timer_get_approx_seconds();
64         }
65 }
66
67 void int_handler(int s){
68         exit(1);
69 }
70
71 int main(int argc,char **argv){
72         error_init(NULL);
73         signal(SIGINT,int_handler);//make ctrl-c do cleanup stuff.
74         InitArgs(argc,argv);
75 #ifndef NDEBUG
76         if ( FindArg( "-showmeminfo" ) )
77                 show_mem_info = 1;              // Make memory statistics show
78 #endif
79         myport=UDP_SERV_BASEPORT;
80         if(arch_ip_open_socket(myport))
81                 return 1;
82         atexit(arch_ip_close_socket);
83         if (ipx_ip_GetMyAddress())
84                 return 2;
85         ip_server_mainloop();
86 }