public void TestMethod1() { ServerNumericReply snr = new ServerNumericReply(); snr.Nick = "badimebot"; Assert.IsTrue(snr.TryParse(":test.example.org 266 badimebot :This is a test message"), "Could not parse regular server message"); }
// Thread Pump for IRC messages private void _MessagePump() { ServerNumericReply snr = new ServerNumericReply(); ChatMessage msg = new ChatMessage(); msg.Nick = Nick; snr.Nick = Nick; while (_ircTcpClient.Connected && _incomingStream.EndOfStream == false) { if (_ct.IsCancellationRequested) { break; } string _incoming = null; try { _incoming = _incomingStream.ReadLine(); } catch (Exception e) { badimebot.Program.ConsoleError("Error Reading from server", e); System.Threading.Thread.Sleep(50); if (_ircTcpClient.Connected == false) { lock (_lockobject) { _state = ConnectionState.Disconnected; } Reconnect(); } else { break; // Connected but got a weird error, bail out } } if (_incoming == null) { badimebot.Program.ConsoleError("Read blank line from irc server"); break; // end? } irclog.WriteLine(_incoming); // Parse and fire events if (_incoming.StartsWith("PING")) { WriteToServerStream($"PONG {_incoming.Substring(5)}"); } else if (snr.TryParse(_incoming)) { if (snr.ReplyCode == 266) { _server_message_connected = true; // Set volatile flag that indicates connection complete and join established } if (_LookingforServerResponse && snr.ReplyCode == _LookingforServerResponseCode) { _LookingforServerResponse = false; _LookingforServerResponseEvent.Set(); } } else if (_incoming.Contains("PRIVMSG") && msg.TryParse(_incoming)) { if (msg.Channel == null) { this.PrivateMessageReceived?.Invoke(this, new MessageArgs() { From = msg.From, Message = msg.Message }); } else { this.ChannelMessageReceived?.Invoke(this, new MessageArgs() { From = msg.From, Message = msg.Message, Channel = msg.Channel }); } } else { // ?? badimebot.Program.ConsoleError($"Unknown server message: {_incoming}"); } //System.Threading.Thread.Sleep(20); System.Threading.Thread.Yield(); } Console.WriteLine("Message pump finished"); }