public override int EndRead(IAsyncResult asyncResult) { CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)asyncResult; if (customeAsyncResult.RealRecvSize < 0) { if (!customeAsyncResult.IsCompleted) { customeAsyncResult.IsCompleted = (true); ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set(); } return(0); } int num = _base.EndRead(asyncResult); if (num < 1) { return(0); } checked { _nextLength -= num; return(num); } }
/// <summary> /// 调用libuv读数据后的回调方法 websocket情况下 此时的 ExcuteUserCallbackFunc 的参数 customeAsyncResult 的 UserCallbackFunc 开始解析 /// </summary> /// <param name="sck"></param> /// <param name="data"></param> /// <param name="nread"></param> /// <param name="e"></param> /// <param name="state"></param> private void OnReadComplite(OwinSocket sck, byte[] data, int nread, Exception e, object state) { CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)state; if (nread < 1 || data == null || e != null) { customeAsyncResult.SocketIsErrOrClose = (true); customeAsyncResult.RealRecvSize = -1; _socketError = true; } else { int realRecvSize = Math.Min(nread, customeAsyncResult.RecvLength); Buffer.BlockCopy(data, 0, customeAsyncResult.RecvBuffer, customeAsyncResult.RecvOffset, realRecvSize); customeAsyncResult.RealRecvSize = realRecvSize; if (realRecvSize < nread) { int num3 = checked (nread - realRecvSize); _requestData._preLoadedBody = new byte[num3]; Buffer.BlockCopy(data, realRecvSize, _requestData._preLoadedBody, 0, num3); } } customeAsyncResult.IsCompleted = (true); ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set(); if (customeAsyncResult.UserCallbackFunc != null) { _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ExcuteUserCallbackFunc), customeAsyncResult); } }
private static void ExcuteUserCallbackFunc(object state) { CustomeAsyncResult customeAsyncResult = state as CustomeAsyncResult; if (customeAsyncResult == null) { return; } if (customeAsyncResult.UserCallbackFunc == null) { return; } customeAsyncResult.UserCallbackFunc(customeAsyncResult); }
/// <summary> /// 写完后 如果是websocket的话 直接再调用 读 ExcuteUserCallbackFunc writehandle 接口 回掉 /// </summary> /// <param name="owinSocket"></param> /// <param name="num"></param> /// <param name="ex"></param> /// <param name="obj"></param> private void OnWriteComplete(OwinSocket owinSocket, int num, Exception ex, object obj) { CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)obj; if (num != 0 || ex != null) { _socketError = true; customeAsyncResult.SocketIsErrOrClose = (true); } customeAsyncResult.IsCompleted = (true); ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set(); if (customeAsyncResult.UserCallbackFunc != null) { _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ExcuteUserCallbackFunc), customeAsyncResult); } }
private void WriteCallBack(IAsyncResult asyncResult) { _owinRepStream.EndWrite(asyncResult); CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)asyncResult; bool flag = customeAsyncResult.SocketIsTimeOut || customeAsyncResult.SocketIsErrOrClose; AsyncState asyncState = (AsyncState)asyncResult.AsyncState; if (asyncState.CancellationToken != CancellationToken.None && asyncState.CancellationToken.IsCancellationRequested) { return; } if (flag) { asyncState.TaskCompletionSource.SetException(new IOException()); return; } asyncState.TaskCompletionSource.SetResult(0); }
public override IAsyncResult BeginRead(byte[] recvBuffer, int recvOffset, int num, AsyncCallback userCallbackFunc, object obj) { OwinAsyncState owinAsyncState = new OwinAsyncState(); owinAsyncState.OwinStream = this; CheckDisposed(); CustomeAsyncResult customeAsyncResult = new CustomeAsyncResult(obj) { RecvBuffer = (recvBuffer), RecvOffset = (recvOffset), RecvLength = (num), UserCallbackFunc = (userCallbackFunc) }; if (_requestData._preLoadedBody != null && _requestData._preLoadedBody.Length > 0) { int num3 = Math.Min(num, _requestData._preLoadedBody.Length); Buffer.BlockCopy(_requestData._preLoadedBody, 0, customeAsyncResult.RecvBuffer, customeAsyncResult.RecvOffset, num3); int num4 = (_requestData._preLoadedBody.Length - num3); if (num4 < 1) { Array.Resize <byte>(ref _requestData._preLoadedBody, 0); _requestData._preLoadedBody = null; } else { byte[] array2 = new byte[num4]; Buffer.BlockCopy(_requestData._preLoadedBody, num3, array2, 0, num4); Array.Resize <byte>(ref _requestData._preLoadedBody, num4); Buffer.BlockCopy(array2, 0, _requestData._preLoadedBody, 0, num4); } customeAsyncResult.RealRecvSize = num3; customeAsyncResult.IsCompleted = (true); ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set(); if (customeAsyncResult.UserCallbackFunc != null) { _simpleThreadPool.UnsafeQueueUserWorkItem(new Action <object>(ExcuteUserCallbackFunc), customeAsyncResult); } return(customeAsyncResult); } owinAsyncState.CustomeAsyncResult = customeAsyncResult;// ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(owinAsyncState.CallBack), null); return(owinAsyncState.CustomeAsyncResult); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object state) { if (_nextLength < 1) { CustomeAsyncResult customeAsyncResult = new CustomeAsyncResult(state); customeAsyncResult.RealRecvSize = (-1); customeAsyncResult.IsCompleted = (true); customeAsyncResult.RecvBuffer = (buffer); customeAsyncResult.RecvLength = (count); customeAsyncResult.RecvOffset = (offset); customeAsyncResult.UserCallbackFunc = (asyncCallback); ((AutoResetEvent)customeAsyncResult.AsyncWaitHandle).Set(); if (asyncCallback != null) { asyncCallback.BeginInvoke(customeAsyncResult, null, null); } return(customeAsyncResult); } return(_base.BeginRead(buffer, offset, count, asyncCallback, state)); }
public override int EndRead(IAsyncResult asyncResult) { CustomeAsyncResult customeAsyncResult = asyncResult as CustomeAsyncResult; if (customeAsyncResult == null) { throw new ArgumentNullException("asyncResult"); } AutoResetEvent autoResetEvent = asyncResult.AsyncWaitHandle as AutoResetEvent; if (autoResetEvent == null) { throw new Exception("'IAsyncResult.AsyncWaitHandle' is null."); } if (!customeAsyncResult.IsCompleted) { autoResetEvent.WaitOne(); } return(customeAsyncResult.RealRecvSize); }
internal IAsyncResult Write(IList <WriteParam> list, AsyncCallback userCallbackFunc, object obj) { if (_closeed || _disposed || _socketError || list == null) { if (_socketError) { throw new SocketException(10054); } if (_closeed || _disposed) { throw new ObjectDisposedException("Stream"); } if (list == null) { throw new ArgumentNullException("buffers"); } } CustomeAsyncResult customeAsyncResult = new CustomeAsyncResult(obj) { UserCallbackFunc = (userCallbackFunc) }; if (list.Count == 1) { byte[] array = new byte[list[0].Length]; Buffer.BlockCopy(list[0].Buffer, list[0].Offset, array, 0, array.Length); _requestData.Socket.Write(array, new Action <OwinSocket, int, Exception, object>(OnWriteComplete), customeAsyncResult); return(customeAsyncResult); } if (list.Count == 2) { byte[] array2 = new byte[list[0].Length]; Buffer.BlockCopy(list[0].Buffer, list[0].Offset, array2, 0, array2.Length); byte[] array3 = new byte[list[1].Length]; Buffer.BlockCopy(list[1].Buffer, list[1].Offset, array3, 0, array3.Length); _requestData.Socket.WriteForPost(array2, array3, new Action <OwinSocket, int, Exception, object>(OnWriteComplete), customeAsyncResult); return(customeAsyncResult); } throw new Exception("count..."); }
private void AsyncCallback(IAsyncResult asyncResult) { TaskCompletionSource <int> taskCompletionSource = asyncResult.AsyncState as TaskCompletionSource <int>; if (taskCompletionSource == null) { return; } CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)asyncResult; if (customeAsyncResult.SocketIsErrOrClose) { taskCompletionSource.SetException(new Exception("socket error.")); return; } if (customeAsyncResult.SocketIsTimeOut) { taskCompletionSource.SetException(new Exception("write timeout.")); return; } taskCompletionSource.SetResult(1); }
private void ReadAsyncCallBack(IAsyncResult asyncResult) { CustomeAsyncResult customeAsyncResult = (CustomeAsyncResult)asyncResult; TaskCompletionSource <int> taskCompletionSource = (TaskCompletionSource <int>)customeAsyncResult.AsyncState; if (customeAsyncResult.SocketIsErrOrClose || customeAsyncResult.SocketIsTimeOut || customeAsyncResult.RealRecvSize < 0) { taskCompletionSource.SetException(new Exception("socket error.")); return; } int num = customeAsyncResult.RealRecvSize; if (num < 1) { num = 0; } checked { _nextLength -= num; taskCompletionSource.SetResult(num); } }