示例#1
0
        public override void ChannelActive(IChannelHandlerContext context)
        {
            RequestBytes req = new RequestBytes();

            req.SetBytes(1, Encoding.UTF8.GetBytes("hello"));
            context.WriteAsync(req);
            context.Flush();
            context.FireChannelActive();
        }
示例#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 EncodeRequest(RequestBytes request, IByteBuffer output)
        {
            byte sign     = ProtocolHeader.toSign(request.SerializerCode, ProtocolHeader.REQUEST);
            long invokeId = request.InvokeId;

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

            output.WriteShort(ProtocolHeader.MAGIC)
            .WriteByte(sign)
            .WriteByte(0x00)
            .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;
            }
        }