Пример #1
0
        public int Send(string data)
        {
            NetworkLogger.LogOutgoing(data);
            var binary = Encoding.GetEncoding(1252).GetBytes(data);

            return(Send(binary, binary.Length));
        }
Пример #2
0
        protected override void OnConnected()
        {
            var address = Socket.RemoteEndPoint.ToString().Split(':');

            NetworkLogger.LogConnected(address[0], int.Parse(address[1]));

            Send(string.Format("NICK {0}\r\nUSER {0} 0 * :{0}\r\n", MyNick));
        }
Пример #3
0
        protected override void OnData(byte[] buffer, int length)
        {
            var data = _buffer.ToString() + Encoding.GetEncoding(1252).GetString(buffer, 0, length);

            NetworkLogger.LogIncoming(data);
            _buffer.Clear();

            int pos;

            while ((pos = data.IndexOf('\n')) != -1)
            {
                var packet = data.Substring(0, pos);
                data = data.Substring(pos + 1);

                if (packet.Length > 0)
                {
                    if (packet.EndsWith("\r"))
                    {
                        packet = packet.Substring(0, packet.Length - 1);
                        if (packet.Length == 0)
                        {
                            continue;
                        }
                    }

                    // log packet
                    System.IO.File.AppendAllText("datalog.txt", string.Format("[{0}]\r\n{1}\r\n\r\n", DateTime.Now.ToString("HH:mm:ss"), packet));

                    string host = string.Empty;

                    if (packet[0] == ':')
                    {
                        if ((pos = packet.IndexOf(' ')) == -1)
                        {
                            System.IO.File.AppendAllText("datalog.txt", "[WARNING] DISCARDED (No content in packet)\r\n\r\n");
                            continue;
                        }

                        host = packet.Substring(1, pos - 1);

                        packet = packet.Substring(pos + 1);
                        if (packet.Length == 0)
                        {
                            continue;
                        }
                    }

                    string content = string.Empty;
                    if ((pos = packet.IndexOf(' ')) != -1)
                    {
                        content = packet.Substring(pos + 1);
                        packet  = packet.Substring(0, pos);
                    }

                    if (!PreprocessPacket(host, packet, content))
                    {
                        // do something ...
                        System.IO.File.AppendAllText("datalog.txt", "[WARNING] Preprocess packet failed\r\n\r\n");
                    }
                }
            }

            if (data.Length > 0)
            {
                _buffer.Append(data);
            }
        }
Пример #4
0
 protected override void OnDisconnected()
 {
     NetworkLogger.LogDisconnected();
 }