예제 #1
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            ResponseBytes resp = message as ResponseBytes;
            string        msg  = Encoding.UTF8.GetString(resp.Bytes);

            Console.WriteLine(msg);
            context.Flush();

            ReferenceCountUtil.Release(message);
        }
예제 #2
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            RequestBytes req = message as RequestBytes;

            string msg = System.Text.Encoding.UTF8.GetString(req.Bytes);

            Console.WriteLine(context.Channel.RemoteAddress + "->Server :" + msg);
            ResponseBytes resp = new ResponseBytes(req.InvokeId);

            resp.SetBytes(1, System.Text.Encoding.UTF8.GetBytes("response from server"));
            resp.Status = 1;
            context.WriteAndFlushAsync(resp).Wait();
        }
예제 #3
0
        private void EncodeResponse(ResponseBytes response, IByteBuffer output)
        {
            byte sign     = ProtocolHeader.toSign(response.SerializerCode, ProtocolHeader.RESPONSE);
            byte status   = response.Status;
            long invokeId = response.Id;

            byte[] bytes  = response.Bytes;
            int    length = bytes.Length;

            output.WriteShort(ProtocolHeader.MAGIC)
            .WriteByte(sign)
            .WriteByte(status)
            .WriteLong(invokeId)
            .WriteInt(length)
            .WriteBytes(bytes);
        }
예제 #4
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            if (input is EmptyByteBuffer)
            {
                return;
            }

            switch (State)
            {
            case StateEnum.HEADER_MAGIC:
                checkMagic(input.ReadShort());             // MAGIC
                Checkpoint(StateEnum.HEADER_SIGN);
                break;

            case StateEnum.HEADER_SIGN:
                header.sign(input.ReadByte());             // 消息标志位
                Checkpoint(StateEnum.HEADER_STATUS);
                break;

            case StateEnum.HEADER_STATUS:
                header.status(input.ReadByte());           // 状态位
                Checkpoint(StateEnum.HEADER_ID);
                break;

            case StateEnum.HEADER_ID:
                header.id(input.ReadLong());               // 消息id
                Checkpoint(StateEnum.HEADER_BODY_LENGTH);
                break;

            case StateEnum.HEADER_BODY_LENGTH:
                var bodyLen = input.ReadInt();
                header.bodyLength(bodyLen);        // 消息体长度
                Checkpoint(StateEnum.BODY);
                break;

            case StateEnum.BODY:
                switch (header.messageCode())
                {
                case ProtocolHeader.HEARTBEAT:
                    break;

                case ProtocolHeader.REQUEST:

                    int    length = checkBodyLength(header.bodyLength());
                    byte[] bytes  = new byte[length];
                    input.ReadBytes(bytes);

                    RequestBytes request = new RequestBytes(header.id());
                    request.Timestamp = 1496732080L;
                    request.SetBytes(header.serializerCode(), bytes);
                    output.Add(request);

                    break;

                case ProtocolHeader.RESPONSE:
                    int    resplength = checkBodyLength(header.bodyLength());
                    byte[] respbytes  = new byte[resplength];
                    input.ReadBytes(respbytes);

                    ResponseBytes response = new ResponseBytes(header.id());
                    response.Status = header.status();
                    response.SetBytes(header.serializerCode(), respbytes);
                    output.Add(response);
                    break;
                }
                Checkpoint(StateEnum.HEADER_MAGIC);
                break;
            }
        }