mirror of
https://github.com/reactos/reactos.git
synced 2025-02-22 08:25:03 +00:00
-Implement reconnect on connection lost as requested. Techbot will now try to re-connect every 60 seconds when a connection lost is detected.
svn path=/trunk/; revision=33350
This commit is contained in:
parent
1e922dc43f
commit
98d47d1fc4
3 changed files with 119 additions and 29 deletions
|
@ -16,6 +16,10 @@ namespace TechBot.IRCLibrary
|
|||
/// </summary>
|
||||
public delegate void ChannelUserDatabaseChangedHandler(IrcChannel channel);
|
||||
|
||||
public delegate void OnConnectHandler ();
|
||||
public delegate void OnDisconnectHandler();
|
||||
public delegate void OnConnectionLostHandler();
|
||||
|
||||
/// <summary>
|
||||
/// An IRC client.
|
||||
/// </summary>
|
||||
|
@ -202,6 +206,10 @@ namespace TechBot.IRCLibrary
|
|||
|
||||
public event ChannelUserDatabaseChangedHandler ChannelUserDatabaseChanged;
|
||||
|
||||
public event OnConnectHandler OnConnect;
|
||||
public event OnConnectionLostHandler OnConnectionLost;
|
||||
public event OnDisconnectHandler OnDisconnect;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public properties
|
||||
|
@ -291,20 +299,39 @@ namespace TechBot.IRCLibrary
|
|||
/// <param name="ar">IAsyncResult object.</param>
|
||||
private void ReadComplete(IAsyncResult ar)
|
||||
{
|
||||
StateObject stateObject = (StateObject) ar.AsyncState;
|
||||
if (stateObject.Stream.CanRead)
|
||||
{
|
||||
int bytesReceived = stateObject.Stream.EndRead(ar);
|
||||
if (bytesReceived > 0)
|
||||
{
|
||||
messageStream.Write(Encoding.GetString(stateObject.Buffer, 0, bytesReceived));
|
||||
while (messageStream.DataAvailable)
|
||||
{
|
||||
OnMessageReceived(new IrcMessage(messageStream.Read()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Receive();
|
||||
try
|
||||
{
|
||||
StateObject stateObject = (StateObject)ar.AsyncState;
|
||||
if (stateObject.Stream.CanRead)
|
||||
{
|
||||
int bytesReceived = stateObject.Stream.EndRead(ar);
|
||||
if (bytesReceived > 0)
|
||||
{
|
||||
messageStream.Write(Encoding.GetString(stateObject.Buffer, 0, bytesReceived));
|
||||
while (messageStream.DataAvailable)
|
||||
{
|
||||
OnMessageReceived(new IrcMessage(messageStream.Read()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Receive();
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -498,6 +525,9 @@ namespace TechBot.IRCLibrary
|
|||
connected = false;
|
||||
tcpClient.Close();
|
||||
tcpClient = null;
|
||||
|
||||
if (OnDisconnect != null)
|
||||
OnDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -507,19 +537,37 @@ namespace TechBot.IRCLibrary
|
|||
/// <param name="message">The message to be sent.</param>
|
||||
public void SendMessage(IrcMessage message)
|
||||
{
|
||||
if (!connected)
|
||||
{
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
|
||||
/* Serialize sending messages */
|
||||
lock (typeof(IrcClient))
|
||||
{
|
||||
NetworkStream networkStream = tcpClient.GetStream();
|
||||
byte[] bytes = Encoding.GetBytes(message.Line);
|
||||
networkStream.Write(bytes, 0, bytes.Length);
|
||||
networkStream.Flush();
|
||||
}
|
||||
try
|
||||
{
|
||||
if (!connected)
|
||||
{
|
||||
throw new NotConnectedException();
|
||||
}
|
||||
|
||||
/* Serialize sending messages */
|
||||
lock (typeof(IrcClient))
|
||||
{
|
||||
NetworkStream networkStream = tcpClient.GetStream();
|
||||
byte[] bytes = Encoding.GetBytes(message.Line);
|
||||
networkStream.Write(bytes, 0, bytes.Length);
|
||||
networkStream.Flush();
|
||||
}
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (OnConnectionLost != null)
|
||||
OnConnectionLost();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -71,13 +71,27 @@ namespace TechBot.Library
|
|||
|
||||
m_IrcClient = new IrcClient();
|
||||
m_IrcClient.Encoding = Encoding.GetEncoding("iso-8859-1");
|
||||
m_IrcClient.OnConnect += new OnConnectHandler(m_IrcClient_OnConnect);
|
||||
m_IrcClient.OnConnectionLost += new OnConnectionLostHandler(m_IrcClient_OnConnectionLost);
|
||||
m_IrcClient.OnDisconnect += new OnDisconnectHandler(m_IrcClient_OnDisconnect);
|
||||
m_IrcClient.MessageReceived += new MessageReceivedHandler(client_MessageReceived);
|
||||
m_IrcClient.ChannelUserDatabaseChanged += new ChannelUserDatabaseChangedHandler(client_ChannelUserDatabaseChanged);
|
||||
|
||||
Connect();
|
||||
}
|
||||
|
||||
void m_IrcClient_OnConnect()
|
||||
{
|
||||
Console.WriteLine("Connected...");
|
||||
}
|
||||
|
||||
private void Connect()
|
||||
{
|
||||
Console.WriteLine("Connecting to {0} port {1}",
|
||||
hostname,
|
||||
port);
|
||||
m_IrcClient.Connect(hostname, port);
|
||||
Console.WriteLine("Connected...");
|
||||
|
||||
m_IrcClient.Register(botname, password, null);
|
||||
Console.WriteLine("Registered as {0}...", botname);
|
||||
JoinChannels();
|
||||
|
@ -89,9 +103,37 @@ namespace TechBot.Library
|
|||
|
||||
PartChannels();
|
||||
m_IrcClient.Diconnect();
|
||||
}
|
||||
|
||||
void m_IrcClient_OnDisconnect()
|
||||
{
|
||||
Console.WriteLine("Disconnected...");
|
||||
}
|
||||
|
||||
void m_IrcClient_OnConnectionLost()
|
||||
{
|
||||
//Dispose old connection
|
||||
Disconnect();
|
||||
|
||||
//Sleep for 1 minute
|
||||
Thread.Sleep(1000 * 60);
|
||||
|
||||
//Try to reconnect
|
||||
Connect();
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
try
|
||||
{
|
||||
m_IrcClient.Diconnect();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
isStopped = true;
|
||||
|
|
|
@ -9,7 +9,7 @@ using System.Configuration.Install;
|
|||
|
||||
namespace TechBot
|
||||
{
|
||||
public class TechBotService : System.ServiceProcess.ServiceBase
|
||||
public class TechBotService : ServiceBase
|
||||
{
|
||||
private Thread thread;
|
||||
private ServiceThread threadWorker;
|
||||
|
|
Loading…
Reference in a new issue