Example #1
0
        private async void ListenerAsync()
        {
            byte[]        buffer = new byte[1024];
            StringBuilder msg    = new StringBuilder();

            int badMessagesReceived = 0;

            Stream stream;

            if (_secure)
            {
                stream = _sslstream;
            }
            else
            {
                stream = _networkstream;
            }

            while (_client.Connected)
            {
                if (_networkstream.DataAvailable)
                {
                    Array.Clear(buffer, 0, buffer.Length);

                    try
                    {
                        await stream.ReadAsync(buffer, 0, buffer.Length);
                    }
                    catch (ObjectDisposedException)
                    {
                        Disconnect();
                        break;
                    }

                    string text = Encoding.UTF8.GetString(buffer).TrimEnd('\0');

                    msg.Append(text);

                    string msgstr = msg.ToString();
                    if (!msgstr.EndsWith("\r\n"))
                    {
                        bool all0 = true;
                        foreach (var b in buffer)
                        {
                            if (b != 0)
                            {
                                all0 = false;
                                break;
                            }
                        }

                        if (all0)
                        {
                            if (++badMessagesReceived <= 2)
                            {
                                continue;
                            }
                            else
                            {
                                Disconnect();
                                break;
                            }
                        }
                        else
                        {
                            var idx = msgstr.LastIndexOf("\r\n");
                            if (idx != -1)
                            {
                                idx += 2;
                                msg.Remove(0, idx);
                                msgstr = msgstr.Substring(0, idx);
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        msg.Clear();
                    }

                    string[] messages = msgstr.Split(_messageSeparators, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string message in messages)
                    {
                        var ircMsg = IrcMessage.FromRawMessage(message);

                        MessageReceived(this, new IrcMessageEventArgs(ircMsg));

                        switch (ircMsg.Command)
                        {
                        case IrcCommand.Ping:
                            EnqueueMessage(message.Replace("PING", "PONG"), true);
                            break;

                        case IrcCommand.Reconnect:
                            Disconnect();
                            break;
                        }
                    }
                }
                else
                {
                    Thread.Sleep(5);
                }
            }
        }
Example #2
0
 public IrcMessageEventArgs(IrcMessage message)
     : base()
 {
     Message = message;
 }