/// <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; //}); }
/// <summary> /// 编码方法 /// </summary> /// <param name="context"></param> /// <param name="cumulation"></param> /// <param name="outputList"></param> protected abstract void CallDecode(ChannelHandlerContext context, ByteBuf buf, List <object> outputList);
/// <summary> /// 读取字节 /// </summary> /// <param name=""></param> public void GetBytes(ByteBuf buff) { Array.Copy(Data, ReaderIndex, buff.Data, buff.WriterIndex, buff.WritableBytes); }
/// <summary> /// 写字节数组 /// </summary> /// <param name="buff"></param> /// <param name="srcIndex"></param> /// <param name="len"></param> public void WriteBytes(ByteBuf buff, int srcIndex, int len) { WriteBytes(buff.Data, srcIndex, len); }
/// <summary> /// 写字节数组 /// </summary> /// <param name="buff"></param> public void WriteBytes(ByteBuf buff) { WriteBytes(buff, buff.ReaderIndex, buff.ReadableBytes); }