4
|
1 /*--------------------------------------------------------------------------
|
|
2 NETREK II -- Paradise
|
|
3
|
|
4 Permission to use, copy, modify, and distribute this software and its
|
|
5 documentation for any NON-COMMERCIAL purpose (following the terms of
|
|
6 the GNU General Public License (read the file 'COPYING')) and without
|
|
7 fee is hereby granted, provided that this copyright notice appear in all
|
|
8 copies. No representations are made about the suitability of this
|
|
9 software for any purpose. This software is provided "as is" without
|
|
10 express or implied warranty.
|
|
11
|
|
12 Xtrek Copyright 1986 Chris Guthrie
|
|
13 Netrek (Xtrek II) Copyright 1989 Kevin P. Smith
|
|
14 Scott Silvey
|
|
15 Paradise II (Netrek II) Copyright 1993 Larry Denys
|
|
16 Kurt Olsen
|
|
17 Brandon Gillespie
|
|
18
|
|
19 Comprehensive credits are available in the file "CREDITS"
|
|
20 --------------------------------------------------------------------------*/
|
|
21
|
|
22 #include "config.h"
|
|
23 #include <stdio.h>
|
|
24 #include <string.h>
|
|
25 #include <math.h>
|
|
26 #include <sys/types.h>
|
|
27 #include <sys/time.h>
|
|
28 #include <signal.h>
|
|
29 #include <errno.h>
|
|
30 #include "defs.h"
|
|
31 #include "struct.h"
|
|
32 #include "data.h"
|
|
33 #include "shmem.h"
|
|
34
|
|
35 static int sendflag = 0;
|
|
36
|
|
37 extern int intrupt();
|
|
38 extern void start_interruptor();
|
|
39 extern int isClientDead();
|
|
40 int reconnect();
|
|
41 extern int readFromClient();
|
|
42 extern void stop_interruptor();
|
|
43 extern int exitGame();
|
|
44 extern int closeUdpConn();
|
|
45 extern unsigned int sleep();
|
|
46 extern int connectToClient();
|
|
47 extern void (*r_signal()) ();
|
|
48
|
|
49 void
|
|
50 setflag()
|
|
51 {
|
|
52 sendflag = 1;
|
|
53 }
|
|
54
|
|
55
|
|
56 void
|
|
57 input()
|
|
58 {
|
|
59 fd_set readfds;
|
|
60
|
|
61 start_interruptor();
|
|
62
|
|
63 /* Idea: read from client often, send to client not so often */
|
|
64 while (me->p_status == PALIVE ||
|
|
65 me->p_status == PEXPLODE ||
|
|
66 me->p_status == PDEAD ||
|
|
67 me->p_status == POBSERVE)
|
|
68 {
|
|
69 if (isClientDead())
|
|
70 {
|
|
71 if (!reconnect())
|
|
72 exit(0);
|
|
73 }
|
|
74 FD_ZERO(&readfds);
|
|
75 FD_SET(sock, &readfds);
|
|
76 if (udpSock >= 0)
|
|
77 FD_SET(udpSock, &readfds);
|
|
78
|
|
79 /*
|
|
80 * blocks indefinitely unless client input or timerDelay SIGALRM causes
|
|
81 * EINTR
|
|
82 */
|
|
83
|
|
84 if (socketWait() > 0)
|
|
85 {
|
|
86 readFromClient();
|
|
87 }
|
|
88 if (sendflag)
|
|
89 {
|
|
90 intrupt();
|
|
91 sendflag = 0;
|
|
92 }
|
|
93
|
|
94 /* statements below are executed once per timerDelay */
|
|
95 if (me->p_updates > delay)
|
|
96 {
|
|
97 me->p_flags &= ~(PFWAR);
|
|
98 }
|
|
99 if (me->p_updates > rdelay)
|
|
100 {
|
|
101 me->p_flags &= ~(PFREFITTING);
|
|
102 }
|
|
103
|
|
104 /*
|
|
105 * this will ghostbust a player if no ping has been received in
|
|
106 * ping_ghostbust_interval seconds
|
|
107 */
|
|
108
|
|
109 if (configvals->ping_allow_ghostbust
|
|
110 && (me->p_status == PALIVE || me->p_status == POBSERVE)
|
|
111 && ping_ghostbust > configvals->ping_ghostbust_interval)
|
|
112 me->p_ghostbuster = 100000; /* ghostbusted */
|
|
113 else
|
|
114 /* Keep self from being nuked... */
|
|
115 me->p_ghostbuster = 0;
|
|
116 }
|
|
117 stop_interruptor();
|
|
118 }
|
|
119
|
|
120 int
|
|
121 reconnect()
|
|
122 {
|
|
123 int i;
|
|
124
|
|
125 if (noressurect)
|
|
126 exitGame();
|
|
127 stop_interruptor();
|
|
128
|
|
129 r_signal(SIGIO, SIG_IGN);
|
|
130 #ifdef DEBUG
|
|
131 printf("Ack! The client went away!\n");
|
|
132 printf("I will attempt to resurrect him!\n");
|
|
133 fflush(stdout);
|
|
134 #endif
|
|
135 commMode = COMM_TCP;
|
|
136 if (udpSock >= 0)
|
|
137 closeUdpConn();
|
|
138
|
|
139 /* For next two minutes, we try to restore connection */
|
|
140 shutdown(sock, 2);
|
|
141 sock = -1;
|
|
142 for (i = 0;; i++)
|
|
143 {
|
|
144 me->p_ghostbuster = 0;
|
|
145 sleep(5);
|
|
146 if (connectToClient(host, nextSocket))
|
|
147 break;
|
|
148 if (i == 23)
|
|
149 {
|
|
150 #ifdef DEBUG
|
|
151 printf("Oh well, maybe I'm getting rusty!\n");
|
|
152 fflush(stdout);
|
|
153 #endif
|
|
154 return 0;
|
|
155 }
|
|
156 }
|
|
157 start_interruptor();
|
|
158 #ifdef DEBUG
|
|
159 printf("A miracle! He's alive!\n");
|
|
160 fflush(stdout);
|
|
161 #endif
|
|
162 return 1;
|
|
163 }
|