Skip to content

Commit 7e52dfc

Browse files
committed
Implements some timeouts.
1 parent 08e68f2 commit 7e52dfc

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/ncp.c

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include "imp.h"
1616
#include "wire.h"
1717

18+
#define RFNM_TIMEOUT 10
19+
#define RRP_TIMEOUT 20
20+
#define ERP_TIMEOUT 20
21+
1822
#define IMP_REGULAR 0
1923
#define IMP_LEADER_ERROR 1
2024
#define IMP_DOWN 2
@@ -71,6 +75,7 @@ static int fd;
7175
static struct sockaddr_un server;
7276
static struct sockaddr_un client;
7377
static socklen_t len;
78+
static unsigned long time_tick;
7479

7580
typedef struct
7681
{
@@ -87,8 +92,10 @@ struct
8792
struct { int link, size; uint32_t lsock, rsock; } rcv, snd;
8893
void (*rrp_callback) (int);
8994
void (*rrp_timeout) (int);
95+
unsigned long rrp_time;
9096
void (*rfnm_callback) (int);
9197
void (*rfnm_timeout) (int);
98+
unsigned long rfnm_time;
9299
} connection[CONNECTIONS];
93100

94101
struct
@@ -104,6 +111,7 @@ static struct
104111
#define HOST_ALIVE 0001
105112

106113
client_t echo;
114+
unsigned long erp_time;
107115
int outstanding_rfnm;
108116
} hosts[256];
109117

@@ -132,6 +140,7 @@ static void when_rrp (int i, void (*cb) (int), void (*to) (int))
132140
{
133141
connection[i].rrp_callback = cb;
134142
connection[i].rrp_timeout = to;
143+
connection[i].rrp_time = time_tick + RRP_TIMEOUT;
135144
}
136145

137146
static void check_rrp (int host)
@@ -154,6 +163,7 @@ static void when_rfnm (int i, void (*cb) (int), void (*to) (int))
154163
{
155164
connection[i].rfnm_callback = cb;
156165
connection[i].rfnm_timeout = to;
166+
connection[i].rfnm_time = time_tick + RFNM_TIMEOUT;
157167
}
158168

159169
static void check_rfnm (int host)
@@ -314,6 +324,8 @@ static int make_open (int host,
314324
connection[i].snd.lsock = snd_lsock;
315325
connection[i].snd.rsock = snd_rsock;
316326
connection[i].flags = 0;
327+
connection[i].rrp_time = time_tick - 1;
328+
connection[i].rfnm_time = time_tick - 1;
317329

318330
return i;
319331
}
@@ -1272,6 +1284,7 @@ static void app_echo (void)
12721284

12731285
memcpy (&hosts[host].echo.addr, &client, len);
12741286
hosts[host].echo.len = len;
1287+
hosts[host].erp_time = time_tick + ERP_TIMEOUT;
12751288
ncp_eco (host, app[2]);
12761289
}
12771290

@@ -1422,6 +1435,37 @@ static void application (void)
14221435
}
14231436
}
14241437

1438+
static void tick (void)
1439+
{
1440+
void (*to) (int);
1441+
int i;
1442+
time_tick++;
1443+
for (i = 0; i < CONNECTIONS; i++) {
1444+
to = connection[i].rrp_timeout;
1445+
if (to != NULL && connection[i].rrp_time == time_tick) {
1446+
connection[i].rrp_callback = NULL;
1447+
connection[i].rrp_timeout = NULL;
1448+
connection[i].rrp_time = time_tick - 1;
1449+
to (i);
1450+
}
1451+
to = connection[i].rfnm_timeout;
1452+
if (to != NULL && connection[i].rfnm_time == time_tick) {
1453+
connection[i].rfnm_callback = NULL;
1454+
connection[i].rfnm_timeout = NULL;
1455+
connection[i].rrp_time = time_tick - 1;
1456+
to (i);
1457+
}
1458+
}
1459+
for (i = 0; i < 256; i++) {
1460+
if (hosts[i].echo.len == 0)
1461+
continue;
1462+
if (hosts[i].erp_time != time_tick)
1463+
continue;
1464+
reply_echo (i, 0, 0x20);
1465+
hosts[i].echo.len = 0;
1466+
}
1467+
}
1468+
14251469
static void cleanup (void)
14261470
{
14271471
unlink (server.sun_path);
@@ -1449,6 +1493,7 @@ void ncp_init (void)
14491493
signal (SIGQUIT, sigcleanup);
14501494
signal (SIGTERM, sigcleanup);
14511495
atexit (cleanup);
1496+
time_tick = 0;
14521497
}
14531498

14541499
int main (int argc, char **argv)
@@ -1461,13 +1506,20 @@ int main (int argc, char **argv)
14611506
for (;;) {
14621507
int n;
14631508
fd_set rfds;
1509+
struct timeval tv;
14641510
FD_ZERO (&rfds);
14651511
FD_SET (fd, &rfds);
14661512
imp_fd_set (&rfds);
1467-
n = select (33, &rfds, NULL, NULL, NULL);
1513+
tv.tv_sec = 1;
1514+
tv.tv_usec = 0;
1515+
n = select (33, &rfds, NULL, NULL, &tv);
14681516
if (n == -1)
14691517
fprintf (stderr, "NCP: select error.\n");
1470-
else if (n > 0) {
1518+
else if (n == 0) {
1519+
tick ();
1520+
tv.tv_sec = 1;
1521+
tv.tv_usec = 0;
1522+
} else {
14711523
if (imp_fd_isset (&rfds)) {
14721524
memset (packet, 0, sizeof packet);
14731525
imp_receive_message (packet, &n);

0 commit comments

Comments
 (0)