6 #include "silc-servers.h"
8 static int timeout_tag;
9 static SilcBool lag_event_pong(SilcClient client,
10 SilcClientConnection conn,
17 static void lag_get(SILC_SERVER_REC *server)
20 g_get_current_time(&server->lag_sent);
21 server->lag_last_check = time(NULL);
24 idp = silc_id_payload_encode(&server->conn->remote_id.u.server_id,
26 silc_client_command_send(silc_client, server->conn,
27 SILC_COMMAND_PING, lag_event_pong, server,
28 1, 1, silc_buffer_data(idp),
29 silc_buffer_len(idp));
30 silc_buffer_free(idp);
33 static SilcBool lag_event_pong(SilcClient client,
34 SilcClientConnection conn,
41 SILC_SERVER_REC *server = context;
44 if (status != SILC_STATUS_OK) {
45 /* if the ping failed for some reason, try it again */
50 if (server->lag_sent.tv_sec == 0) {
51 /* not expecting lag reply. */
55 g_get_current_time(&now);
56 server->lag = (int) get_timeval_diff(&now, &server->lag_sent);
57 memset(&server->lag_sent, 0, sizeof(server->lag_sent));
59 signal_emit("server lag", 1, server);
64 static int sig_check_lag(void)
68 int lag_check_time, max_lag;
70 lag_check_time = settings_get_time("lag_check_time") / 1000;
71 max_lag = settings_get_time("lag_max_before_disconnect") / 1000;
73 if (lag_check_time <= 0)
77 for (tmp = servers; tmp != NULL; tmp = next) {
78 SILC_SERVER_REC *rec = tmp->data;
81 if (!IS_SILC_SERVER(rec))
84 if (rec->lag_sent.tv_sec != 0) {
85 /* waiting for lag reply */
86 if (max_lag > 1 && now-rec->lag_sent.tv_sec > max_lag) {
87 /* too much lag, disconnect */
88 signal_emit("server lag disconnect", 1, rec);
89 rec->connection_lost = TRUE;
90 server_disconnect((SERVER_REC *) rec);
92 } else if (rec->lag_last_check+lag_check_time < now &&
94 /* no commands in buffer - get the lag */
102 void silc_lag_init(void)
104 /* silc-client will need those... silc-plugin uses irc defaults */
105 settings_add_time("misc", "lag_check_time", "1min");
106 settings_add_time("misc", "lag_max_before_disconnect", "5min");
108 timeout_tag = g_timeout_add(1000, (GSourceFunc) sig_check_lag, NULL);
111 void silc_lag_deinit(void)
113 g_source_remove(timeout_tag);