コード例 #1
0
ファイル: Utils.cs プロジェクト: shanyipeng/KissU
        private static StringBuilder AppendAddresses(this StringBuilder builder, IDnsMessage response)
        {
            if (!(response is IAddressedEnvelope <IDnsMessage>))
            {
                return(builder);
            }

            IAddressedEnvelope <IDnsMessage> envelope = (IAddressedEnvelope <IDnsMessage>)response;

            var addr = envelope.Sender;

            if (addr != null)
            {
                builder.Append("from: ")
                .Append(addr)
                .Append(", ");
            }

            addr = envelope.Recipient;
            if (addr != null)
            {
                builder.Append("to: ")
                .Append(addr)
                .Append(", ");
            }

            return(builder);
        }
コード例 #2
0
        protected override void Encode(IChannelHandlerContext context, IAddressedEnvelope <IDnsResponse> message, List <object> output)
        {
            EndPoint     recipient = message.Recipient;
            IDnsResponse response  = message.Content;

            if (response != null)
            {
                IByteBuffer buffer = AllocateBuffer(context, message);

                bool success = false;
                try
                {
                    EncodeHeader(response, buffer);
                    EncodeQuestions(response, buffer);
                    EncodeRecords(response, DnsSection.ANSWER, buffer);
                    EncodeRecords(response, DnsSection.AUTHORITY, buffer);
                    EncodeRecords(response, DnsSection.ADDITIONAL, buffer);
                    success = true;
                }
                finally
                {
                    if (!success)
                    {
                        buffer.Release();
                    }
                }

                output.Add(new DatagramPacket(buffer, null, recipient));
            }
        }
コード例 #3
0
        protected override void Encode(IChannelHandlerContext context, IAddressedEnvelope <ProtocolMessage> message, List <object> output)
        {
            Log.Debug(context.Name);

            var binary = MessagePackSerializer.SerializeUnsafe(message.Content);
            var buffer = context.Allocator.Buffer(binary.Count);

            buffer.WriteBytes(binary.Array, binary.Offset, binary.Count);

            output.Add(new DefaultAddressedEnvelope <IByteBuffer>(buffer, message.Recipient, message.Recipient));
        }
コード例 #4
0
        protected override void ChannelRead0(IChannelHandlerContext ctx, IAddressedEnvelope <ProtocolMessage> msg)
        {
            Log.Debug(ctx.Name);
            Log.Debug($"Received Datagram: {msg.Sender} => {msg.Recipient}");

            var response = behavior.Behavior(msg.Content);

            Log.Debug($"Sending Datagram: {response} {ctx.Channel.LocalAddress} => {msg.Sender}");

            response.Match(m => ignore(ctx.WriteAsync(new DefaultAddressedEnvelope <ProtocolMessage>(m, msg.Sender))),
                           () => unit);
        }
コード例 #5
0
        protected internal override void Encode(IChannelHandlerContext context, IAddressedEnvelope <T> message, List <object> output)
        {
            this.encoder.Encode(context, message.Content, output);
            if (output.Count != 1)
            {
                throw new EncoderException($"{this.encoder.GetType()} must produce only one message.");
            }

            if (!(output[0] is IByteBuffer content))
            {
                throw new EncoderException($"{this.encoder.GetType()} must produce only IByteBuffer.");
            }

            // Replace the ByteBuf with a DatagramPacket.
            output[0] = new DatagramPacket(content, message.Sender, message.Recipient);
        }
コード例 #6
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(false);
            }

            if (!base.Equals(obj))
            {
                return(false);
            }

            if (!(obj is IAddressedEnvelope <DatagramDnsQuery>))
            {
                return(false);
            }

            IAddressedEnvelope <DatagramDnsQuery> that = (IAddressedEnvelope <DatagramDnsQuery>)obj;

            if (Sender == null)
            {
                if (that.Sender != null)
                {
                    return(false);
                }
            }
            else if (!Sender.Equals(that.Sender))
            {
                return(false);
            }

            if (Recipient == null)
            {
                if (that.Recipient != null)
                {
                    return(false);
                }
            }
            else if (!Recipient.Equals(that.Recipient))
            {
                return(false);
            }

            return(true);
        }
コード例 #7
0
        /// <inheritdoc />
        protected internal override void Encode(IChannelHandlerContext context, IAddressedEnvelope <T> message, List <object> output)
        {
            this.encoder.Encode(context, message.Content, output);
            if (output.Count != 1)
            {
                CThrowHelper.ThrowEncoderException_MustProduceOnlyOneMsg(this.encoder.GetType());
            }

            var content = output[0] as IByteBuffer;

            if (content is null)
            {
                CThrowHelper.ThrowEncoderException_MustProduceOnlyByteBuf(this.encoder.GetType());
            }

            // Replace the ByteBuf with a DatagramPacket.
            output[0] = new DatagramPacket(content, message.Sender, message.Recipient);
        }
コード例 #8
0
        protected override void Decode(IChannelHandlerContext context, IAddressedEnvelope <IByteBuffer> message, List <object> output)
        {
            Log.Debug(context.Name);
            var input = message.Content;

            if (input.ReadableBytes > 0)
            {
                ProtocolMessage protocolMessage;

                if (input.HasArray)
                {
                    var segment = new ArraySegment <byte>(input.Array, input.ArrayOffset + input.ReaderIndex, input.ReadableBytes);
                    protocolMessage = MessagePackSerializer.Deserialize <ProtocolMessage>(segment);
                }
                else
                {
                    var buf = new byte[input.ReadableBytes];
                    input.ReadBytes(buf);
                    protocolMessage = MessagePackSerializer.Deserialize <ProtocolMessage>(buf);
                }

                output.Add(new DefaultAddressedEnvelope <ProtocolMessage>(protocolMessage, message.Sender, message.Recipient));
            }
        }
コード例 #9
0
 protected IByteBuffer AllocateBuffer(IChannelHandlerContext ctx,
                                      IAddressedEnvelope <IDnsResponse> message) => ctx.Allocator.Buffer(1024);
コード例 #10
0
 private IByteBuffer AllocateBuffer(IChannelHandlerContext ctx,
                                    IAddressedEnvelope <IDnsQuery> message) => ctx.Allocator.Buffer(1024);