/// <summary> /// 处理数据接受 /// </summary> /// <param name="e"></param> /// <returns></returns> private bool OnReceive(SocketAsyncEventArgs e) { if (!IsConnected) { return(false); } int size = e.BytesTransferred; if (size > 0) { // Notify Receive Data } isReceiving = false; if (e.SocketError == SocketError.Success) { if (size > 0) { ByteBuf buff = (e.UserToken as ByteBuf); buff.WriterIndex += size; buff.Retain(); ChannelPipeline.FireChannelRead(e.UserToken); return(true); } else { Disconnect(); } } else { ChannelPipeline.FireExceptionCaught(new Exception(e.SocketError.ToString())); } return(false); }
public override void FireChannelRead(ChannelHandlerContext context, object msg) { //Task.Run(() => //{ RequestMessage message = msg as RequestMessage; string response = $"hello world 111"; byte[] bodyBytes = Encoding.UTF8.GetBytes(response); byte[] commandBytes = Encoding.UTF8.GetBytes(message.Command); byte[] array = new byte[32]; Array.Copy(commandBytes, array, commandBytes.Length); ByteBuf buff = ByteBufHelper.DefaultByteBufPool.Allocate(); buff.Retain(); buff.WriteInt(36 + bodyBytes.Length); buff.WriteBytes(array); buff.WriteInt(0); buff.WriteBytes(bodyBytes); context.Write(buff); return; //}); }