Irc thread can now be properly terminated. Separate initialization from connecting. Add callback class for irc stuff.
This commit is contained in:
+1
-1
@@ -42,7 +42,7 @@ public:
|
|||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
// Signal that the thread should be terminated
|
// Signal that the thread should be terminated
|
||||||
void SignalTermination();
|
virtual void SignalTermination();
|
||||||
|
|
||||||
// Wait for the termination of the thread.
|
// Wait for the termination of the thread.
|
||||||
// Only one Thread should wait for the termination!
|
// Only one Thread should wait for the termination!
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2007 by Lothar May *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#include <net/irccallback.h>
|
||||||
|
|
||||||
|
|
||||||
|
IrcCallback::~IrcCallback()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
@@ -25,13 +25,16 @@ using namespace std;
|
|||||||
|
|
||||||
struct IrcContext
|
struct IrcContext
|
||||||
{
|
{
|
||||||
|
IrcContext(IrcThread &t) : ircThread(t), session(NULL), serverPort(0) {}
|
||||||
|
IrcThread &ircThread;
|
||||||
|
irc_session_t *session;
|
||||||
string serverAddress;
|
string serverAddress;
|
||||||
unsigned serverPort;
|
unsigned serverPort;
|
||||||
string nick;
|
string nick;
|
||||||
string channel;
|
string channel;
|
||||||
};
|
};
|
||||||
|
|
||||||
void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
|
void event_connect(irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count)
|
||||||
{
|
{
|
||||||
IrcContext *ctx = (IrcContext *) irc_get_ctx(session);
|
IrcContext *ctx = (IrcContext *) irc_get_ctx(session);
|
||||||
|
|
||||||
@@ -39,9 +42,10 @@ void event_connect (irc_session_t * session, const char * event, const char * or
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
IrcThread::IrcThread()
|
IrcThread::IrcThread(IrcCallback &callback)
|
||||||
|
: m_callback(callback)
|
||||||
{
|
{
|
||||||
m_context.reset(new IrcContext);
|
m_context.reset(new IrcContext(*this));
|
||||||
}
|
}
|
||||||
|
|
||||||
IrcThread::~IrcThread()
|
IrcThread::~IrcThread()
|
||||||
@@ -60,14 +64,9 @@ IrcThread::Init(const std::string &serverAddress, unsigned serverPort, bool ipv6
|
|||||||
context.serverPort = serverPort;
|
context.serverPort = serverPort;
|
||||||
context.nick = nick;
|
context.nick = nick;
|
||||||
context.channel = channel;
|
context.channel = channel;
|
||||||
}
|
|
||||||
|
|
||||||
void
|
// Initialize libirc stuff.
|
||||||
IrcThread::Main()
|
|
||||||
{
|
|
||||||
irc_callbacks_t callbacks;
|
irc_callbacks_t callbacks;
|
||||||
irc_session_t *s;
|
|
||||||
|
|
||||||
memset (&callbacks, 0, sizeof(callbacks));
|
memset (&callbacks, 0, sizeof(callbacks));
|
||||||
|
|
||||||
callbacks.event_connect = event_connect;
|
callbacks.event_connect = event_connect;
|
||||||
@@ -91,13 +90,31 @@ IrcThread::Main()
|
|||||||
//callbacks.event_dcc_chat_req
|
//callbacks.event_dcc_chat_req
|
||||||
//callbacks.event_dcc_send_req
|
//callbacks.event_dcc_send_req
|
||||||
|
|
||||||
s = irc_create_session(&callbacks);
|
context.session = irc_create_session(&callbacks);
|
||||||
|
|
||||||
|
if (context.session)
|
||||||
|
{
|
||||||
|
irc_set_ctx(context.session, &context);
|
||||||
|
// We want nicknames only, strip them from nick!host.
|
||||||
|
irc_option_set(context.session, LIBIRC_OPTION_STRIPNICKS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
IrcThread::SignalTermination()
|
||||||
|
{
|
||||||
|
Thread::SignalTermination();
|
||||||
|
irc_cmd_quit(GetContext().session, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
IrcThread::Main()
|
||||||
|
{
|
||||||
|
IrcContext &context = GetContext();
|
||||||
|
irc_session_t *s = context.session;
|
||||||
if (s)
|
if (s)
|
||||||
{
|
{
|
||||||
irc_set_ctx(s, &GetContext());
|
if (irc_connect(s, context.serverAddress.c_str(), context.serverPort, 0, context.nick.c_str(), 0, 0) == 0)
|
||||||
|
|
||||||
if (irc_connect(s, GetContext().serverAddress.c_str(), GetContext().serverPort, 0, GetContext().nick.c_str(), 0, 0) == 0)
|
|
||||||
irc_run(s);
|
irc_run(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,3 +133,9 @@ IrcThread::GetContext()
|
|||||||
return *m_context;
|
return *m_context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IrcCallback &
|
||||||
|
IrcThread::GetCallback()
|
||||||
|
{
|
||||||
|
return m_callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/***************************************************************************
|
||||||
|
* Copyright (C) 2007 by Lothar May *
|
||||||
|
* *
|
||||||
|
* This program is free software; you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* This program is distributed in the hope that it will be useful, *
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
|
* GNU General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program; if not, write to the *
|
||||||
|
* Free Software Foundation, Inc., *
|
||||||
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||||
|
***************************************************************************/
|
||||||
|
/* Callback interface for irc gui. */
|
||||||
|
|
||||||
|
#ifndef _IRCCALLBACK_H_
|
||||||
|
#define _IRCCALLBACK_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class IrcCallback
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~IrcCallback();
|
||||||
|
|
||||||
|
virtual void SignalIrcPlayerJoined(const std::string &nickName) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+7
-1
@@ -21,6 +21,7 @@
|
|||||||
#ifndef _IRCTHREAD_H_
|
#ifndef _IRCTHREAD_H_
|
||||||
#define _IRCTHREAD_H_
|
#define _IRCTHREAD_H_
|
||||||
|
|
||||||
|
#include <net/irccallback.h>
|
||||||
#include <core/thread.h>
|
#include <core/thread.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -29,12 +30,14 @@ struct IrcContext;
|
|||||||
class IrcThread : public Thread
|
class IrcThread : public Thread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IrcThread();
|
IrcThread(IrcCallback &callback);
|
||||||
virtual ~IrcThread();
|
virtual ~IrcThread();
|
||||||
|
|
||||||
// Set the parameters.
|
// Set the parameters.
|
||||||
void Init(const std::string &serverAddress, unsigned serverPort, bool ipv6, const std::string &nick, const std::string &channel);
|
void Init(const std::string &serverAddress, unsigned serverPort, bool ipv6, const std::string &nick, const std::string &channel);
|
||||||
|
|
||||||
|
virtual void SignalTermination();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
// Main function of the thread.
|
// Main function of the thread.
|
||||||
@@ -43,8 +46,11 @@ protected:
|
|||||||
const IrcContext &GetContext() const;
|
const IrcContext &GetContext() const;
|
||||||
IrcContext &GetContext();
|
IrcContext &GetContext();
|
||||||
|
|
||||||
|
IrcCallback &GetCallback();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::auto_ptr<IrcContext> m_context;
|
std::auto_ptr<IrcContext> m_context;
|
||||||
|
IrcCallback &m_callback;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user