source: abuse/trunk/src/net/undrv.cpp @ 651

Last change on this file since 651 was 651, checked in by Sam Hocevar, 9 years ago

build: add a --disable-network compilation flag and get rid of most of
the CELLOS_LV2 ifdefs.

File size: 4.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if HAVE_NETWORK
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <sys/ioctl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/time.h>
25#include <string.h>
26#include <signal.h>
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <sys/types.h>
30#include <sys/ipc.h>
31#include <sys/shm.h>
32#include <netdb.h>
33
34#include "common.h"
35
36#include "undrv.h"
37#include "../include/netface.h" // net interface structures the engine will use
38
39//#include "netdrv.h"
40#include "gserver.h"
41#include "gclient.h"
42#include "fileman.h"
43#include "sock.h"
44#include "tcpip.h"
45
46
47
48static int comm_fd=-1,game_fd=-1;
49net_socket *game_sock=NULL,*comm_sock=NULL;
50
51void undrv_cleanup()
52{
53  if (game_sock) { delete game_sock; game_sock=NULL; }
54  if (comm_sock) { delete comm_sock; comm_sock=NULL; }
55}
56
57
58void setup_ports(int comm_port, int game_port)
59{
60  comm_sock=tcpip.create_listen_socket(comm_port,net_socket::SOCKET_SECURE);
61  if (!comm_sock) mdie("net driver : could not setup comminication socket");
62  comm_sock->read_selectable();
63
64  game_sock=tcpip.create_listen_socket(game_port,net_socket::SOCKET_FAST);
65  if (!game_sock) mdie("net driver : could not setup data socket");
66  game_sock->read_selectable();
67}
68
69int kill_old_driver(int argc, char **argv)
70{
71  FILE *fp=fopen(DLOCK_NAME,"rb");
72  if (fp)
73  {
74    int pid;
75    if (fscanf(fp,"%d",&pid)==1)
76    {
77      struct stat st;
78      char proc_path[50];
79      sprintf(proc_path,"/proc/%d",pid);
80      if (!stat(proc_path,&st))
81      {
82    fprintf(stderr,"net driver : warning, %s already running, attempting to kill...\n",argv[0]);
83    if (kill(pid,SIGKILL))
84    {
85      fprintf(stderr,"net driver : unable to kill process %d, cannot run net-abuse\n",pid);
86      fclose(fp);
87      return 0;
88    }
89    fprintf(stderr,"killed process %d\n",pid);
90      }
91    }
92    fclose(fp);
93    unlink(DLOCK_NAME);
94  }
95  unlink(DIN_NAME);    // remove any previous files if they existss
96  unlink(DOUT_NAME);
97  return 1;
98}
99
100main(int argc, char **argv)
101{
102  if (!kill_old_driver(argc,argv))
103    return 0;
104
105  int no_fork=0,i;
106  for (i=1; i<argc; i++)
107    if (!strcmp(argv[i],"-no_fork"))    // use this to debug easier
108      no_fork=1;
109
110  if (!no_fork)      // use this for debugging
111  {
112    int child_pid=fork();
113    if (child_pid)
114    {
115      FILE *fp=fopen(DLOCK_NAME,"wb");
116      if (!fp)
117      {
118    fprintf(stderr,"Unable to open %s for writing, killing child\n",DLOCK_NAME);
119    kill(child_pid,SIGUSR2);
120    return 0;
121      }
122      fprintf(fp,"%d\n",child_pid);
123      fclose(fp);
124      printf("%d\n",child_pid);         // tell parent the sound driver's process number
125      return 0;                         // exit, child will continue
126    }
127  }
128
129  fman=new file_manager(argc,argv,&tcpip);
130
131  int comm_port=DEFAULT_COMM_PORT;
132  int game_port=-1;
133  int debug=0;
134
135  for (i=1; i<argc-1; i++)
136    if (!strcmp(argv[i],"-port"))
137    {
138      comm_port=atoi(argv[i+1]);
139      if (game_port==-1)
140        game_port=comm_port+1;
141    }
142    else if (!strcmp(argv[i],"-game_port"))
143      game_port=atoi(argv[i+1]);
144    else if (!strcmp(argv[i],"-debug"))
145      debug=1;
146
147
148  if (game_port==-1) game_port=DEFAULT_GAME_PORT;
149
150  // make sure this program was run by the abuse engine
151  if (argc<2 || strcmp(argv[1],"runme"))
152  {
153    fprintf(stderr,"%s is normally run by abuse, running stand-alone file server\n"
154               "Server will be killed by running abuse\n",argv[0]);
155  } else driver=new net_driver(argc,argv,comm_port,game_port,&tcpip);
156
157  setup_ports(comm_port,game_port);
158
159  while (1)
160  {
161    tcpip.select_sockets();
162    if (driver)
163      driver->check_commands();
164
165    if (comm_sock->ready_to_read())
166    {
167      net_address *addr;
168
169      net_socket *new_sock=comm_sock->accept(addr);
170      if (debug)
171      {
172    if (new_sock)
173    {
174      fprintf(stderr,"accepting new connection from \n");
175      addr->print();
176    } else
177    fprintf(stderr,"accept failed\n");
178      }
179
180
181      if (new_sock)
182      {
183    uchar client_type;
184    if (new_sock->read(&client_type,1)!=1)
185    {
186      delete addr;
187      delete new_sock;
188    }
189    else if (client_type==CLIENT_NFS)
190    {
191      delete addr;
192      fman->add_nfs_client(new_sock);
193    }
194    else if (!driver || !driver->add_client(client_type,new_sock,addr))
195    {
196      delete addr;
197      delete new_sock;
198    }
199      }
200    }
201
202    fman->process_net();
203  }
204}
205
206#endif // HAVE_NETWORK
207
Note: See TracBrowser for help on using the repository browser.