Author: mpiulachs Date: Wed May 7 14:53:14 2008 New Revision: 33350
URL: http://svn.reactos.org/svn/reactos?rev=33350&view=rev Log: -Implement reconnect on connection lost as requested. Techbot will now try to re-connect every 60 seconds when a connection lost is detected.
Modified: trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs trunk/irc/TechBot/TechBot/TechBotService.cs
Modified: trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot.IRCLibrary/IrcC... ============================================================================== --- trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs [iso-8859-1] Wed May 7 14:53:14 2008 @@ -15,6 +15,10 @@ /// Delegate that notifies if the user database for a channel has changed. /// </summary> public delegate void ChannelUserDatabaseChangedHandler(IrcChannel channel); + + public delegate void OnConnectHandler (); + public delegate void OnDisconnectHandler(); + public delegate void OnConnectionLostHandler();
/// <summary> /// An IRC client. @@ -202,6 +206,10 @@
public event ChannelUserDatabaseChangedHandler ChannelUserDatabaseChanged;
+ public event OnConnectHandler OnConnect; + public event OnConnectionLostHandler OnConnectionLost; + public event OnDisconnectHandler OnDisconnect; + #endregion
#region Public properties @@ -291,20 +299,39 @@ /// <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 @@ connected = false; tcpClient.Close(); tcpClient = null; + + if (OnDisconnect != null) + OnDisconnect(); } }
@@ -507,19 +537,37 @@ /// <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>
Modified: trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot.Library/TechBot... ============================================================================== --- trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs [iso-8859-1] Wed May 7 14:53:14 2008 @@ -71,13 +71,27 @@
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,7 +103,35 @@
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()
Modified: trunk/irc/TechBot/TechBot/TechBotService.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot/TechBotService.... ============================================================================== --- trunk/irc/TechBot/TechBot/TechBotService.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot/TechBotService.cs [iso-8859-1] Wed May 7 14:53:14 2008 @@ -9,7 +9,7 @@
namespace TechBot { - public class TechBotService : System.ServiceProcess.ServiceBase + public class TechBotService : ServiceBase { private Thread thread; private ServiceThread threadWorker;