Esempio n. 1
0
        public XeonClient(TcpClient client)
        {
            Client         = client;
            RemoteEndPoint = Client.Client.RemoteEndPoint.ToString();
            Stream         = Client.GetStream();

            // Handle Telnet events internally first.
            OnTelnet += (packet) =>
            {
                switch (packet.Command)
                {
                case Telnet.Command.SB:
                    switch (packet.Option)
                    {
                    case Telnet.Option.GMCP:
                        GMCP.GmcpData gmcp = GMCP.GmcpData.FromTelnetPacket(packet);
                        InvokeOnGMCP(gmcp);
                        break;

                    default:
                        InvokeOnTelnetSB(packet);
                        break;
                    }
                    break;

                case Telnet.Command.WILL:
                    if (!IsWaitingOn(packet.Option))
                    {
                        // Client initiated
                        SetOption(packet.Option, Telnet.TelnetOptionState.Waiting);
                        InvokeOnTelnetWill(packet.Option);
                    }
                    else
                    {
                        // Server initiated.
                        SetOption(packet.Option, Telnet.TelnetOptionState.Enabled);
                    }
                    break;

                case Telnet.Command.WONT:
                    if (!IsWaitingOn(packet.Option))
                    {
                        // Client initiated
                        SetOption(packet.Option, Telnet.TelnetOptionState.Waiting);
                        InvokeOnTelnetWont(packet.Option);
                    }
                    else
                    {
                        // Server initiated.
                        SetOption(packet.Option, Telnet.TelnetOptionState.Disabled);
                    }
                    break;

                case Telnet.Command.DO:
                    if (!IsWaitingOn(packet.Option))
                    {
                        // Client initiated
                        SetOption(packet.Option, Telnet.TelnetOptionState.Waiting);
                        InvokeOnTelnetDo(packet.Option);
                    }
                    else
                    {
                        // Server initiated.
                        SetOption(packet.Option, Telnet.TelnetOptionState.Enabled);
                    }
                    break;

                case Telnet.Command.DONT:
                    if (!IsWaitingOn(packet.Option))
                    {
                        // Client initiated
                        SetOption(packet.Option, Telnet.TelnetOptionState.Waiting);
                        InvokeOnTelnetDont(packet.Option);
                    }
                    else
                    {
                        // Server initiated.
                        SetOption(packet.Option, Telnet.TelnetOptionState.Disabled);
                    }
                    break;

                default:
                    InvokeOnTelnetUnhandled(packet);
                    break;
                }
            };

            Task t = new Task(async() =>
            {
                while (true)
                {
                    try
                    {
                        Client.Client.Send(new byte[1], 0);
                        int bytesRead = 0;
                        while (Stream.DataAvailable)
                        {
                            buffer         = new byte[1024];
                            int localBytes = await Stream.ReadAsync(buffer, 0, buffer.Length);
                            bytesRead     += localBytes;
                            if (buffer.Length > localBytes)
                            {
                                byte[] temp = new byte[localBytes];
                                Buffer.BlockCopy(buffer, 0, temp, 0, localBytes);
                                buffer = temp;
                            }
                            ClientBuffer.Add(buffer);
                            if (BufUtil.CountDelim(ClientBuffer.InternalBuffer, Telnet.IAC) > 0)
                            {
                                List <Telnet.TelnetPacket> packets = Telnet.Parse(ClientBuffer.InternalBuffer, out byte[] remaining);
                                ClientBuffer = new BufferBuilder();
                                packets.ForEach(packet =>
                                {
                                    InvokeOnTelnet(packet);
                                });
                                if (remaining.Length > 0)
                                {
                                    ClientBuffer.Add(remaining);
                                }
                            }
                        }
                        if (ClientBuffer.CanConsume())
                        {
                            byte[] buf   = BufUtil.StripEOL(ClientBuffer.Consume());
                            ClientBuffer = new BufferBuilder();
                            string data  = Encoding.UTF8.GetString(buf);
                            InvokeOnMessage(data);
                        }
                    }
                    catch (SocketException)
                    {
                        break;
                    }
                }
                Client.Close();
                InvokeOnDisconnect();
            });

            t.Start();
        }