-Implement reconnect on connection lost as requested. Techbot will now try to re...
authorMarc Piulachs <marc.piulachs@live.com>
Wed, 7 May 2008 19:53:14 +0000 (19:53 +0000)
committerMarc Piulachs <marc.piulachs@live.com>
Wed, 7 May 2008 19:53:14 +0000 (19:53 +0000)
svn path=/trunk/; revision=33350

irc/TechBot/TechBot.IRCLibrary/IrcClient.cs
irc/TechBot/TechBot.Library/TechBotIrcService.cs
irc/TechBot/TechBot/TechBotService.cs

index 68c7aa2..ecd7b7a 100644 (file)
@@ -16,6 +16,10 @@ namespace TechBot.IRCLibrary
        /// </summary>\r
        public delegate void ChannelUserDatabaseChangedHandler(IrcChannel channel);\r
 \r
+    public delegate void OnConnectHandler ();\r
+    public delegate void OnDisconnectHandler();\r
+    public delegate void OnConnectionLostHandler();\r
+\r
        /// <summary>\r
        /// An IRC client.\r
        /// </summary>\r
@@ -202,6 +206,10 @@ namespace TechBot.IRCLibrary
 \r
                public event ChannelUserDatabaseChangedHandler ChannelUserDatabaseChanged;\r
 \r
+        public event OnConnectHandler OnConnect;\r
+        public event OnConnectionLostHandler OnConnectionLost;\r
+        public event OnDisconnectHandler OnDisconnect;\r
+\r
                #endregion\r
 \r
                #region Public properties\r
@@ -291,20 +299,39 @@ namespace TechBot.IRCLibrary
                /// <param name="ar">IAsyncResult object.</param>\r
                private void ReadComplete(IAsyncResult ar)\r
                {\r
-                       StateObject stateObject = (StateObject) ar.AsyncState;\r
-                       if (stateObject.Stream.CanRead)\r
-                       {\r
-                               int bytesReceived = stateObject.Stream.EndRead(ar);\r
-                               if (bytesReceived > 0)\r
-                               {\r
-                                       messageStream.Write(Encoding.GetString(stateObject.Buffer, 0, bytesReceived));\r
-                                       while (messageStream.DataAvailable)\r
-                                       {\r
-                                               OnMessageReceived(new IrcMessage(messageStream.Read()));\r
-                                       }\r
-                               }\r
-                       }\r
-                       Receive();\r
+            try\r
+            {\r
+                StateObject stateObject = (StateObject)ar.AsyncState;\r
+                if (stateObject.Stream.CanRead)\r
+                {\r
+                    int bytesReceived = stateObject.Stream.EndRead(ar);\r
+                    if (bytesReceived > 0)\r
+                    {\r
+                        messageStream.Write(Encoding.GetString(stateObject.Buffer, 0, bytesReceived));\r
+                        while (messageStream.DataAvailable)\r
+                        {\r
+                            OnMessageReceived(new IrcMessage(messageStream.Read()));\r
+                        }\r
+                    }\r
+                }\r
+             \r
+                Receive();\r
+            }\r
+            catch (SocketException)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            }\r
+            catch (IOException)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            }\r
+            catch (Exception)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            } \r
                }\r
 \r
                /// <summary>\r
@@ -498,6 +525,9 @@ namespace TechBot.IRCLibrary
                                connected = false;\r
                                tcpClient.Close();\r
                                tcpClient = null;\r
+\r
+                if (OnDisconnect != null)\r
+                    OnDisconnect();\r
                        }\r
                }\r
 \r
@@ -507,19 +537,37 @@ namespace TechBot.IRCLibrary
                /// <param name="message">The message to be sent.</param>\r
                public void SendMessage(IrcMessage message)\r
                {\r
-                       if (!connected)\r
-                       {\r
-                               throw new NotConnectedException();\r
-                       }\r
-                       \r
-                       /* Serialize sending messages */\r
-                       lock (typeof(IrcClient))\r
-                       {\r
-                               NetworkStream networkStream = tcpClient.GetStream();\r
-                               byte[] bytes = Encoding.GetBytes(message.Line);\r
-                               networkStream.Write(bytes, 0, bytes.Length);\r
-                               networkStream.Flush();\r
-                       }\r
+            try\r
+            {\r
+                if (!connected)\r
+                {\r
+                    throw new NotConnectedException();\r
+                }\r
+\r
+                /* Serialize sending messages */\r
+                lock (typeof(IrcClient))\r
+                {\r
+                    NetworkStream networkStream = tcpClient.GetStream();\r
+                    byte[] bytes = Encoding.GetBytes(message.Line);\r
+                    networkStream.Write(bytes, 0, bytes.Length);\r
+                    networkStream.Flush();\r
+                }\r
+            }\r
+            catch (SocketException)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            }\r
+            catch (IOException)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            }\r
+            catch (Exception)\r
+            {\r
+                if (OnConnectionLost != null)\r
+                    OnConnectionLost();\r
+            } \r
                }\r
 \r
                /// <summary>\r
index ec5f70a..489a742 100644 (file)
@@ -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;
index 362ce84..61c1e0f 100644 (file)
@@ -9,7 +9,7 @@ using System.Configuration.Install;
 \r
 namespace TechBot\r
 {\r
-       public class TechBotService : System.ServiceProcess.ServiceBase\r
+       public class TechBotService : ServiceBase\r
        {\r
                private Thread thread;\r
                private ServiceThread threadWorker;\r