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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

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