diff --git a/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs b/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs
index 68c7aa24ab4..ecd7b7ac719 100644
--- a/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs
+++ b/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs
@@ -16,6 +16,10 @@ namespace TechBot.IRCLibrary
///
public delegate void ChannelUserDatabaseChangedHandler(IrcChannel channel);
+ public delegate void OnConnectHandler ();
+ public delegate void OnDisconnectHandler();
+ public delegate void OnConnectionLostHandler();
+
///
/// An IRC client.
///
@@ -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
/// IAsyncResult object.
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();
+ }
}
///
@@ -498,6 +525,9 @@ namespace TechBot.IRCLibrary
connected = false;
tcpClient.Close();
tcpClient = null;
+
+ if (OnDisconnect != null)
+ OnDisconnect();
}
}
@@ -507,19 +537,37 @@ namespace TechBot.IRCLibrary
/// The message to be sent.
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();
+ }
}
///
diff --git a/irc/TechBot/TechBot.Library/TechBotIrcService.cs b/irc/TechBot/TechBot.Library/TechBotIrcService.cs
index ec5f70af967..489a742dfda 100644
--- a/irc/TechBot/TechBot.Library/TechBotIrcService.cs
+++ b/irc/TechBot/TechBot.Library/TechBotIrcService.cs
@@ -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;
diff --git a/irc/TechBot/TechBot/TechBotService.cs b/irc/TechBot/TechBot/TechBotService.cs
index 362ce849d0f..61c1e0f5cd1 100644
--- a/irc/TechBot/TechBot/TechBotService.cs
+++ b/irc/TechBot/TechBot/TechBotService.cs
@@ -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;