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; |
11 | long next; |
12 | Backend *back; |
13 | void *backhandle; |
14 | }; |
15 | |
16 | static void pinger_schedule(Pinger pinger); |
17 | |
18 | static void pinger_timer(void *ctx, long now) |
19 | { |
20 | Pinger pinger = (Pinger)ctx; |
21 | |
22 | if (pinger->pending && now - pinger->next >= 0) { |
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 | |
46 | Pinger pinger_new(Config *cfg, Backend *back, void *backhandle) |
47 | { |
48 | Pinger pinger = snew(struct pinger_tag); |
49 | |
50 | pinger->interval = cfg->ping_interval; |
51 | pinger->pending = FALSE; |
52 | pinger->back = back; |
53 | pinger->backhandle = backhandle; |
54 | pinger_schedule(pinger); |
55 | |
56 | return pinger; |
57 | } |
58 | |
59 | void pinger_reconfig(Pinger pinger, Config *oldcfg, Config *newcfg) |
60 | { |
61 | if (oldcfg->ping_interval != newcfg->ping_interval) { |
62 | pinger->interval = newcfg->ping_interval; |
63 | pinger_schedule(pinger); |
64 | } |
65 | } |
66 | |
67 | void pinger_free(Pinger pinger) |
68 | { |
69 | expire_timer_context(pinger); |
70 | sfree(pinger); |
71 | } |