Add start and pre commit hooks
[TortoiseGit.git] / src / TortoisePlink / PINGER.C
blob071d02c7e39161e122a78e767a2f0eb8c14f3b36
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     unsigned 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, unsigned long now)\r
19 {\r
20     Pinger pinger = (Pinger)ctx;\r
22     if (pinger->pending && now == pinger->next) {\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(Conf *conf, Backend *back, void *backhandle)\r
47 {\r
48     Pinger pinger = snew(struct pinger_tag);\r
50     pinger->interval = conf_get_int(conf, CONF_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, Conf *oldconf, Conf *newconf)\r
60 {\r
61     int newinterval = conf_get_int(newconf, CONF_ping_interval);\r
62     if (conf_get_int(oldconf, CONF_ping_interval) != newinterval) {\r
63         pinger->interval = newinterval;\r
64         pinger_schedule(pinger);\r
65     }\r
66 }\r
68 void pinger_free(Pinger pinger)\r
69 {\r
70     expire_timer_context(pinger);\r
71     sfree(pinger);\r
72 }\r