From 64275e09a21aa78076c1aaefc015e96c2864847b Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 18 Aug 2017 16:45:53 +0200 Subject: Attempts to deal with timeouts. libircclient does not seem to detect any issue when the connection has been dropped due to external causes. I added a test (getting the current channel topic)that should trigger if 5min go by without anything happening, hopefully this will cause libircclient to detect the connection problem, if not, implement a timeout from the lack of replies concerning the topic should not be too hard. --- src/irc/irc.c | 26 ++++++++++++++++++++++++++ src/irc/irc.h | 2 ++ src/main.c | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) (limited to 'src') diff --git a/src/irc/irc.c b/src/irc/irc.c index f1290f9..5779a7a 100644 --- a/src/irc/irc.c +++ b/src/irc/irc.c @@ -140,3 +140,29 @@ void JH_irc_handle_numeric_event ) { } + +int JH_irc_test_connection +( + struct JH_irc irc [const restrict static 1] +) +{ + if + ( + irc_cmd_topic + ( + irc->session, + JH_parameters_get_irc_channel(irc->params), + (const char *) NULL + ) + != 0 + ) + { + JH_S_ERROR(stderr, "Could not request IRC topic to test connection."); + + return -1; + } + + JH_S_DEBUG(stdout, 1, "Testing connection..."); + + return 0; +} diff --git a/src/irc/irc.h b/src/irc/irc.h index a6c2df3..67a4350 100644 --- a/src/irc/irc.h +++ b/src/irc/irc.h @@ -16,6 +16,8 @@ int JH_irc_initialize int JH_irc_connect (struct JH_irc irc [const restrict static 1]); +int JH_irc_test_connection (struct JH_irc irc [const restrict static 1]); + int JH_irc_send_message ( struct JH_irc irc [const restrict static 1], diff --git a/src/main.c b/src/main.c index 2646809..becd6a3 100644 --- a/src/main.c +++ b/src/main.c @@ -106,6 +106,9 @@ static int event_handling_loop struct timeval tv; fd_set in_set, out_set; int fd_max, error; + int timeouts; + + timeouts = 0; for (;;) { @@ -161,6 +164,24 @@ static int event_handling_loop strerror(error) ); } + else if (error == 0) + { + timeouts += 1; + + /* 1200 timeouts => 5min */ + if ((timeouts >= 1200) && (JH_irc_test_connection(irc) != 0)) + { + JH_S_ERROR(stderr, "Timed out."); + + JH_irc_finalize(irc); + + return -1; + } + } + else + { + timeouts = 0; + } if (JH_irc_post_select(irc, &in_set, &out_set) < 0) { -- cgit v1.2.3-70-g09d2