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

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

imlib: refactor Event and EventHandler?.

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