private void HeartbeatThread() { const int PING_INTERVAL = 60; _logger.Log(LogLevel.Debug, "Heartbeat thread started"); // Ping the server regulary to detect if the socket is dead DateTime nextPing = DateTime.Now.AddSeconds(PING_INTERVAL); while (true) { if (!_client.IsConnected) { if (!connecting) { TryReconnect(); } } else { if (nextPing < DateTime.Now) { _logger.Log(LogLevel.Debug, "Pinging {_hostname}", _hostname); _client.Ping(_hostname); nextPing = DateTime.Now.AddSeconds(PING_INTERVAL); } } Thread.Sleep(1000); } }
void Run() { Client = new StandardIrcClient(); DrawCommandLine(); var Info = new IrcUserRegistrationInfo(); Info.NickName = Settings.Default.Name; Info.RealName = Settings.Default.Realname; Info.UserName = Settings.Default.Realname + "Bot"; using (var connectedEvent = new ManualResetEventSlim(false)) { Client.Connected += (sender2, e2) => connectedEvent.Set(); Client.Connect(new Uri(Settings.Default.Server), Info); if (!connectedEvent.Wait(10000)) { Client.Dispose(); PrintLine(string.Format("Connection to {0} timed out.", Settings.Default.Server), ConsoleColor.Red); return; } PrintLine(string.Format("Connected to {0}", Settings.Default.Server)); // *** POST-INIT Client.MotdReceived += delegate(Object Sender, EventArgs E) { PrintLine("Joining Channels..."); Client.Channels.Join(Settings.Default.Channel); }; // *** DEBUG OUTPUT Client.RawMessageReceived += delegate(Object Sender, IrcRawMessageEventArgs Event) { PrintLine(Event.RawContent); }; // *** PING Client.PingReceived += delegate(Object Sender, IrcPingOrPongReceivedEventArgs Event) { Client.Ping(Event.Server); }; // *** CHANNEL JOINING Client.LocalUser.JoinedChannel += delegate(Object Sender, IrcChannelEventArgs Event) { Event.Channel.MessageReceived += Channel_MessageReceived; SayInChannel(OnJoinActions); }; // *** REJOIN AFTER KICK Client.LocalUser.LeftChannel += delegate(Object Sender, IrcChannelEventArgs Event) { Client.Channels.Join(Event.Channel.Name); }; Int32 Counter = 0; while (Client.IsConnected) { Thread.Sleep(5); if (DCTimer > 0) { DCTimer--; if (DCTimer == 0) { Client.Disconnect(); } } if (++Counter == 12000) { PrintLine("Manual Ping"); Client.Ping(); Counter = 0; } while (Console.KeyAvailable) { Console.SetCursorPosition(0, 0); ConsoleKeyInfo Key = Console.ReadKey(true); switch (Key.Key) { case ConsoleKey.Enter: ConsoleCommand(); CommandLine = ""; break; case ConsoleKey.Backspace: if (CommandLine.Length > 0) { CommandLine = CommandLine.Substring(0, CommandLine.Length - 1); } break; default: CommandLine = CommandLine + Key.KeyChar; break; } DrawCommandLine(); } } } DrawCommandLine(); }