示例#1
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var msg = (ThriftMessage)message;
            var bp  = new TBinaryProtocol.Factory();
            var t   = new TThriftTransport(context.Channel, msg.Content, ThriftTransportType.Framed);
            var tp  = bp.GetProtocol(t);
            var mh  = tp.ReadMessageBegin();

            tp.ReadMessageEnd();
            var buf = msg.Content;

            byte[] req = new byte[buf.ReadableBytes];
            buf.ReadBytes(req);
            //ReadOnlySpan<byte> bytesBuffer = req;
            //ReadOnlySpan<sbyte> sbytesBuffer = MemoryMarshal.Cast<byte, sbyte>(bytesBuffer);
            //sbyte[] signed = sbytesBuffer.ToArray();
            string body = Encoding.UTF8.GetString(req);

            Trace.WriteLine($"recv order: {body}");
            string      currentTime = string.Compare(body, "query time", true) == 0 ? DateTime.Now.ToLongTimeString() : "bad order";
            IByteBuffer resp        = Unpooled.CopiedBuffer(currentTime, Encoding.UTF8);

            context.WriteAsync(resp);
            //string body = new string(signed, 0, req.Length, Encoding.UTF8);
            //base.ChannelRead(context, message);
        }
        protected IByteBuffer TryDecodeUnframedMessage(IChannelHandlerContext ctx,
                                                       IChannel channel,
                                                       IByteBuffer buffer,
                                                       TProtocolFactory inputProtocolFactory)
        {
            // Perform a trial decode, skipping through
            // the fields, to see whether we have an entire message available.

            int messageLength           = 0;
            int messageStartReaderIndex = buffer.ReaderIndex;

            try
            {
                using (TThriftTransport decodeAttemptTransport = new TThriftTransport(channel, buffer, ThriftTransportType.Unframed))
                {
                    int initialReadBytes = decodeAttemptTransport.GetReadByteCount();
                    using (TProtocol inputProtocol =
                               inputProtocolFactory.GetProtocol(decodeAttemptTransport))
                    {
                        // Skip through the message
                        inputProtocol.ReadMessageBegin();
                        TProtocolUtil.Skip(inputProtocol, TType.Struct);
                        inputProtocol.ReadMessageEnd();

                        messageLength = decodeAttemptTransport.GetReadByteCount() - initialReadBytes;
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                // No complete message was decoded: ran out of bytes
                return(null);
            }
            catch (TTransportException)
            {
                // No complete message was decoded: ran out of bytes
                return(null);
            }
            finally
            {
                if (buffer.ReaderIndex - messageStartReaderIndex > this._opts.MaxFrameSize)
                {
                    ctx.FireExceptionCaught(new TooLongFrameException("Maximum frame size of " + this._opts.MaxFrameSize + " exceeded"));
                }

                buffer.SetReaderIndex(messageStartReaderIndex);
            }

            if (messageLength <= 0)
            {
                return(null);
            }

            // We have a full message in the read buffer, slice it off
            IByteBuffer messageBuffer =
                ExtractFrame(buffer, messageStartReaderIndex, messageLength);

            buffer.SetReaderIndex(messageStartReaderIndex + messageLength);
            return(messageBuffer);
        }