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

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

core: Get rid of mostly useless headers, move endianness handling to
common.h (and rewrite functions so that they do not need the SDL headers)
and move a few functions out of sdlport's video.cpp. These functions
were in the original video.cpp (which reappears) and shouldn't be part
of the SDL port.

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