more refactoring of the settings (information hiding and code cleanup)
[TortoiseGit.git] / src / TortoisePlink / PINGER.C
blobb6fde2456b2c466c8ebc33a6e29e0c3d82a2134e
1 /*\r
2  * pinger.c: centralised module that deals with sending TS_PING\r
3  * keepalives, to avoid replicating this code in multiple backends.\r
4  */\r
5 \r
6 #include "putty.h"\r
7 \r
8 struct pinger_tag {\r
9     int interval;\r
10     int pending;\r
11     long next;\r
12     Backend *back;\r
13     void *backhandle;\r
14 };\r
16 static void pinger_schedule(Pinger pinger);\r
18 static void pinger_timer(void *ctx, long now)\r
19 {\r
20     Pinger pinger = (Pinger)ctx;\r
22     if (pinger->pending && now - pinger->next >= 0) {\r
23         pinger->back->special(pinger->backhandle, TS_PING);\r
24         pinger->pending = FALSE;\r
25         pinger_schedule(pinger);\r
26     }\r
27 }\r
29 static void pinger_schedule(Pinger pinger)\r
30 {\r
31     int next;\r
33     if (!pinger->interval) {\r
34         pinger->pending = FALSE;       /* cancel any pending ping */\r
35         return;\r
36     }\r
38     next = schedule_timer(pinger->interval * TICKSPERSEC,\r
39                           pinger_timer, pinger);\r
40     if (!pinger->pending || next < pinger->next) {\r
41         pinger->next = next;\r
42         pinger->pending = TRUE;\r
43     }\r
44 }\r
46 Pinger pinger_new(Config *cfg, Backend *back, void *backhandle)\r
47 {\r
48     Pinger pinger = snew(struct pinger_tag);\r
50     pinger->interval = cfg->ping_interval;\r
51     pinger->pending = FALSE;\r
52     pinger->back = back;\r
53     pinger->backhandle = backhandle;\r
54     pinger_schedule(pinger);\r
56     return pinger;\r
57 }\r
59 void pinger_reconfig(Pinger pinger, Config *oldcfg, Config *newcfg)\r
60 {\r
61     if (oldcfg->ping_interval != newcfg->ping_interval) {\r
62         pinger->interval = newcfg->ping_interval;\r
63         pinger_schedule(pinger);\r
64     }\r
65 }\r
67 void pinger_free(Pinger pinger)\r
68 {\r
69     expire_timer_context(pinger);\r
70     sfree(pinger);\r
71 }\r