6 #include "silc-servers.h"
8 #define SILC_CLIENT_LAG_PING_ID 0x1337
10 static int timeout_tag;
11 static void lag_event_pong(SILC_SERVER_REC *server,
12 SilcClientCommandReplyContext cmd);
14 static void lag_get(SILC_SERVER_REC *server)
17 g_get_current_time(&server->lag_sent);
18 server->lag_last_check = time(NULL);
20 /* register pending callback & send ping */
21 silc_client_command_pending(server->conn, SILC_COMMAND_PING,
22 SILC_CLIENT_LAG_PING_ID,
23 (SilcCommandCb)lag_event_pong, (void *)server);
24 idp = silc_id_payload_encode(server->conn->remote_id, SILC_ID_SERVER);
25 silc_client_command_send(silc_client, server->conn,
26 SILC_COMMAND_PING, SILC_CLIENT_LAG_PING_ID,
27 1, 1, idp->data, idp->len);
28 silc_buffer_free(idp);
31 static void lag_event_pong(SILC_SERVER_REC *server,
32 SilcClientCommandReplyContext cmd)
36 if (cmd->error != SILC_STATUS_OK) {
38 /* if the ping failed for some reason, try it again */
44 if (server->lag_sent.tv_sec == 0) {
45 /* not expecting lag reply.. */
49 g_get_current_time(&now);
50 server->lag = (int) get_timeval_diff(&now, &server->lag_sent);
51 memset(&server->lag_sent, 0, sizeof(server->lag_sent));
53 signal_emit("server lag", 1, server);
56 static int sig_check_lag(void)
60 int lag_check_time, max_lag;
62 lag_check_time = settings_get_int("lag_check_time");
63 max_lag = settings_get_int("lag_max_before_disconnect");
65 if (lag_check_time <= 0)
69 for (tmp = servers; tmp != NULL; tmp = next) {
70 SILC_SERVER_REC *rec = tmp->data;
73 if (!IS_SILC_SERVER(rec))
76 if (rec->lag_sent.tv_sec != 0) {
77 /* waiting for lag reply */
78 if (max_lag > 1 && now-rec->lag_sent.tv_sec > max_lag) {
79 /* too much lag, disconnect */
80 signal_emit("server lag disconnect", 1, rec);
81 rec->connection_lost = TRUE;
82 server_disconnect((SERVER_REC *) rec);
84 } else if (rec->lag_last_check+lag_check_time < now &&
86 /* no commands in buffer - get the lag */
94 void silc_lag_init(void)
96 /* silc-client will need those... silc-plugin uses irc defaults */
97 settings_add_int("misc", "lag_check_time", 60);
98 settings_add_int("misc", "lag_max_before_disconnect", 300);
100 timeout_tag = g_timeout_add(1000, (GSourceFunc) sig_check_lag, NULL);
103 void silc_lag_deinit(void)
105 g_source_remove(timeout_tag);