/// <summary> /// libuv读取成功后回调函数 /// </summary> /// <param name="handle">clientHandle</param> /// <param name="nread">读取的长度</param> /// <param name="error">Exception</param> /// <param name="state">回调参数</param> private void ReadCallback(UvStreamHandle handle, int nread, Exception error, object state) { if (_actionIndex > 0) { ActionStoreManage.Remove(_actionIndex, new Action(ReadStop)); _actionIndex = 0; } bool flag = error == null && nread > 0; //bool flag2 = nread == 0 || nread == -4077 || nread == -4095; ReadState readState = (ReadState)state; try { if (flag) { readState.Callback(this, _memoryInfo.Buffer, nread, null, readState.OtheState); } else { readState.Callback(this, null, -1, error, readState.OtheState); } } finally { handle.Stop(); //AllocCallback 开辟的内存空间回收 RecoverMemoryInfo(); } }
private void ReadForPost(object state) { ReadState readState = (ReadState)state; try { _actionIndex = ActionStoreManage.Add(new Action(ReadStop)); _clientHandle.Read(new Func <UvStreamHandle, int, object, LibUv.BufferStruct>(AllocCallback), new Action <UvStreamHandle, int, Exception, object>(ReadCallback), readState); } catch (Exception error) { ActionStoreManage.Remove(_actionIndex, new Action(ReadStop)); _actionIndex = 0; readState.Callback(this, null, -1, error, readState.OtheState); } }
private static void ReadMore(ReadState state) { var buffer = new byte[BufferSize]; state.Socket.AsyncReceive(buffer, (len) => { DataFragment fragment = null; if (!state.Buffers.Any()) { fragment = new DataFragment(buffer); state.ExpectedLength = fragment.Length; } state.Buffers.Add(buffer.Take(len).ToArray()); state.CurrentLength += len; if (state.CurrentLength < state.ExpectedLength) { ReadMore(state); } else { if (fragment == null) { var ba = state.Buffers.SelectMany(b => b).ToArray(); fragment = new DataFragment(ba); } state.Fragments.Add(fragment); if (!fragment.Fin) { ReadMore(new ReadState(state.Socket, state.Callback, state.Fragments)); } else { state.Callback(state.Fragments); } } }); }