public override void ChannelActive(IChannelHandlerContext context) { RequestBytes req = new RequestBytes(); req.SetBytes(1, Encoding.UTF8.GetBytes("hello")); context.WriteAsync(req); context.Flush(); context.FireChannelActive(); }
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; } }