Very graceful auto-reconnect for the server irc bot. Will give up after two attempts within 60 seconds.
This commit is contained in:
@@ -31,6 +31,7 @@ using namespace std;
|
||||
|
||||
#define IRC_WAIT_TERMINATION_MSEC 500
|
||||
#define IRC_MAX_RENAME_TRIES 5
|
||||
#define IRC_MIN_RECONNECT_INTERVAL_SEC 60
|
||||
|
||||
#define IRC_RENAME_ATTACH "|Lobby"
|
||||
#define IRC_MAX_NICK_LEN 16
|
||||
@@ -252,7 +253,9 @@ irc_event_numeric(irc_session_t * session, unsigned irc_event, const char * /*or
|
||||
}
|
||||
|
||||
IrcThread::IrcThread(IrcCallback &callback)
|
||||
: m_callback(callback), m_terminationTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start)
|
||||
: m_callback(callback),
|
||||
m_terminationTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start),
|
||||
m_lastConnectTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::manual_start)
|
||||
{
|
||||
m_context.reset(new IrcContext(*this));
|
||||
}
|
||||
@@ -278,7 +281,43 @@ IrcThread::Init(const std::string &serverAddress, unsigned serverPort, bool ipv6
|
||||
context.nick = nick;
|
||||
context.channel = channel;
|
||||
context.channelPassword = channelPassword;
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::SendChatMessage(const std::string &msg)
|
||||
{
|
||||
IrcContext &context = GetContext();
|
||||
irc_cmd_msg(context.session, context.channel.c_str(), msg.c_str());
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::SignalTermination()
|
||||
{
|
||||
Thread::SignalTermination();
|
||||
irc_cmd_quit(GetContext().session, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::Main()
|
||||
{
|
||||
do
|
||||
{
|
||||
if (IrcInit())
|
||||
IrcMain(); // Will loop until terminated.
|
||||
} while (!ShouldTerminate() && GetContext().session && !irc_is_connected(GetContext().session) && m_lastConnectTimer.elapsed().total_seconds() > IRC_MIN_RECONNECT_INTERVAL_SEC);
|
||||
}
|
||||
|
||||
bool
|
||||
IrcThread::IrcInit()
|
||||
{
|
||||
bool retVal = false;
|
||||
|
||||
IrcContext &context = GetContext();
|
||||
if (context.session)
|
||||
{
|
||||
irc_destroy_session(context.session);
|
||||
context.session = NULL;
|
||||
}
|
||||
// Initialize libirc stuff.
|
||||
irc_callbacks_t callbacks;
|
||||
memset (&callbacks, 0, sizeof(callbacks));
|
||||
@@ -311,31 +350,19 @@ IrcThread::Init(const std::string &serverAddress, unsigned serverPort, bool ipv6
|
||||
irc_set_ctx(context.session, &context);
|
||||
// We want nicknames only, strip them from nick!host.
|
||||
irc_option_set(context.session, LIBIRC_OPTION_STRIPNICKS);
|
||||
retVal = true;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::SendChatMessage(const std::string &msg)
|
||||
{
|
||||
IrcContext &context = GetContext();
|
||||
irc_cmd_msg(context.session, context.channel.c_str(), msg.c_str());
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::SignalTermination()
|
||||
{
|
||||
Thread::SignalTermination();
|
||||
irc_cmd_quit(GetContext().session, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::Main()
|
||||
IrcThread::IrcMain()
|
||||
{
|
||||
m_lastConnectTimer.restart();
|
||||
IrcContext &context = GetContext();
|
||||
|
||||
irc_session_t *s = context.session;
|
||||
if (s)
|
||||
{
|
||||
|
||||
bool connected = false;
|
||||
if (context.useIPv6)
|
||||
connected = irc_connect6(s, context.serverAddress.c_str(), context.serverPort, 0, context.nick.c_str(), 0, 0) == 0;
|
||||
@@ -393,7 +420,6 @@ IrcThread::Main()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
IrcThread::HandleIrcError(int errorCode)
|
||||
|
||||
@@ -48,6 +48,8 @@ protected:
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main();
|
||||
bool IrcInit();
|
||||
void IrcMain();
|
||||
|
||||
void HandleIrcError(int errorCode);
|
||||
|
||||
@@ -59,6 +61,7 @@ private:
|
||||
IrcCallback &m_callback;
|
||||
|
||||
boost::timers::portable::microsec_timer m_terminationTimer;
|
||||
boost::timers::portable::microsec_timer m_lastConnectTimer;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user