예제 #1
0
        public void Route(Stats stats)
        {
            switch (stats.Kind)
            {
            case StatsKind.Event:
                this._serializers.EventSerializer.SerializeTo(ref stats.Event, _serializedMetric);
                break;

            case StatsKind.Metric:
                if (!RouteMetric(ref stats.Metric))
                {
                    return;
                }

                break;

            case StatsKind.ServiceCheck:
                this._serializers.ServiceCheckSerializer.SerializeTo(ref stats.ServiceCheck, _serializedMetric);
                break;

            default:
                throw new ArgumentException($"{stats.Kind} is not supported");
            }

            _bufferBuilder.Add(_serializedMetric);
        }
 public void Add()
 {
     Assert.AreEqual(12, _bufferBuilder.Capacity);
     _bufferBuilder.Add(CreateSerializedMetric('1', 3));
     _bufferBuilder.Add(CreateSerializedMetric('2', 3));
     _bufferBuilder.Add(CreateSerializedMetric('3', 3));
     Assert.Null(_handler.Buffer);
     _bufferBuilder.Add(CreateSerializedMetric('4', 3));
     Assert.AreEqual(3, _bufferBuilder.Length);
     Assert.AreEqual(_handler.Buffer, Encoding.UTF8.GetBytes("111\n222\n333"));
 }
예제 #3
0
 public void Add()
 {
     Assert.AreEqual(12, _bufferBuilder.Capacity);
     Assert.True(_bufferBuilder.Add(new string('1', 3)));
     Assert.True(_bufferBuilder.Add(new string('2', 3)));
     Assert.True(_bufferBuilder.Add(new string('3', 3)));
     Assert.Null(_handler.Buffer);
     Assert.True(_bufferBuilder.Add(new string('4', 3)));
     Assert.AreEqual(3, _bufferBuilder.Length);
     Assert.AreEqual(_handler.Buffer, Encoding.UTF8.GetBytes("111\n222\n333"));
 }
예제 #4
0
 public void FlushStatsMetric(StatsMetric metric)
 {
     _serializer.SerializeTo(ref metric, _serializedMetric);
     _bufferBuilder.Add(_serializedMetric);
 }
예제 #5
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();
        }