39934deb |
1 | /* |
2 | * pinger.c: centralised module that deals with sending TS_PING |
3 | * keepalives, to avoid replicating this code in multiple backends. |
4 | */ |
5 | |
6 | #include "putty.h" |
7 | |
8 | struct pinger_tag { |
9 | int interval; |
10 | int pending; |
d719927e |
11 | unsigned long next; |
39934deb |
12 | Backend *back; |
13 | void *backhandle; |
14 | }; |
15 | |
16 | static void pinger_schedule(Pinger pinger); |
17 | |
d719927e |
18 | static void pinger_timer(void *ctx, unsigned long now) |
39934deb |
19 | { |
20 | Pinger pinger = (Pinger)ctx; |
21 | |
d719927e |
22 | if (pinger->pending && now == pinger->next) { |
39934deb |
23 | pinger->back->special(pinger->backhandle, TS_PING); |
24 | pinger->pending = FALSE; |
25 | pinger_schedule(pinger); |
26 | } |
27 | } |
28 | |
29 | static void pinger_schedule(Pinger pinger) |
30 | { |
31 | int next; |
32 | |
33 | if (!pinger->interval) { |
34 | pinger->pending = FALSE; /* cancel any pending ping */ |
35 | return; |
36 | } |
37 | |
38 | next = schedule_timer(pinger->interval * TICKSPERSEC, |
39 | pinger_timer, pinger); |
40 | if (!pinger->pending || next < pinger->next) { |
41 | pinger->next = next; |
42 | pinger->pending = TRUE; |
43 | } |
44 | } |
45 | |
4a693cfc |
46 | Pinger pinger_new(Conf *conf, Backend *back, void *backhandle) |
39934deb |
47 | { |
48 | Pinger pinger = snew(struct pinger_tag); |
49 | |
4a693cfc |
50 | pinger->interval = conf_get_int(conf, CONF_ping_interval); |
39934deb |
51 | pinger->pending = FALSE; |
52 | pinger->back = back; |
53 | pinger->backhandle = backhandle; |
54 | pinger_schedule(pinger); |
55 | |
56 | return pinger; |
57 | } |
58 | |
4a693cfc |
59 | void pinger_reconfig(Pinger pinger, Conf *oldconf, Conf *newconf) |
39934deb |
60 | { |
4a693cfc |
61 | int newinterval = conf_get_int(newconf, CONF_ping_interval); |
62 | if (conf_get_int(oldconf, CONF_ping_interval) != newinterval) { |
63 | pinger->interval = newinterval; |
39934deb |
64 | pinger_schedule(pinger); |
65 | } |
66 | } |
67 | |
68 | void pinger_free(Pinger pinger) |
69 | { |
70 | expire_timer_context(pinger); |
71 | sfree(pinger); |
72 | } |