private void OnListenPipe(UvStreamHandle pipe, int status, UvException error) { if (status < 0) { return; } var dispatchPipe = new UvPipeHandle(Log); // Add to the list of created pipes for disposal tracking _createdPipes.Add(dispatchPipe); try { dispatchPipe.Init(Thread.Loop, Thread.QueueCloseHandle, true); pipe.Accept(dispatchPipe); // Ensure client sends _pipeMessage before adding pipe to _dispatchPipes. var readContext = new PipeReadContext(this); dispatchPipe.ReadStart( (handle, status2, state) => ((PipeReadContext)state).AllocCallback(handle, status2), (handle, status2, state) => ((PipeReadContext)state).ReadCallback(handle, status2), readContext); } catch (UvException ex) { dispatchPipe.Dispose(); Log.LogError(0, ex, "ListenerPrimary.OnListenPipe"); } }
private static void ConnectCallback(UvConnectRequest req, int status, UvException exception, object state) { var client = (UvDuplexPipeClient)state; var connection = new UvConnection(client._connectSocket, client._thread, client._connectSocket.GetPeerIPEndPoint(), client._connectSocket.GetSockIPEndPoint()); client._connectTcs.TrySetResult(connection); }
public UvWriteResult GetResult() { var exception = _exception; var status = _status; // Reset the awaitable state _exception = null; _status = 0; _callback = null; return(new UvWriteResult(status, exception)); }
private static void ConnectionCallback(UvStreamHandle stream, int status, UvException error, object state) { var listener = (Listener)state; if (error != null) { listener.Log.LogError(0, error, "Listener.ConnectionCallback"); } else if (!listener._closed) { listener.OnConnection(stream, status); } }
public UvWriteResult GetResult() { Debug.Assert(_callback == _callbackCompleted); var exception = _exception; var status = _status; // Reset the awaitable state _exception = null; _status = 0; _callback = null; return(new UvWriteResult(status, exception)); }
private Exception LogAndWrapReadError(UvException uvError) { if (uvError.StatusCode == LibuvConstants.ECANCELED) { // The operation was canceled by the server not the client. No need for additional logs. return(new ConnectionAbortedException(uvError.Message, uvError)); } if (LibuvConstants.IsConnectionReset(uvError.StatusCode)) { // Log connection resets at a lower (Debug) level. Log.ConnectionReset(ConnectionId); return(new ConnectionResetException(uvError.Message, uvError)); } // This is unexpected. Log.ConnectionError(ConnectionId, uvError); return(new IOException(uvError.Message, uvError)); }
private async Task ConnectedCallback(UvConnectRequest connect, int status, UvException error, TaskCompletionSource <int> tcs) { connect.Dispose(); if (error != null) { tcs.SetException(error); return; } var writeReq = new UvWriteReq(Log); try { DispatchPipe.ReadStart( (handle, status2, state) => ((ListenerSecondary)state)._buf, (handle, status2, state) => ((ListenerSecondary)state).ReadStartCallback(handle, status2), this); writeReq.Init(Thread); var result = await writeReq.WriteAsync( DispatchPipe, new ArraySegment <ArraySegment <byte> >(new[] { new ArraySegment <byte>(_pipeMessage) })); if (result.Error != null) { tcs.SetException(result.Error); } else { tcs.SetResult(0); } } catch (Exception ex) { DispatchPipe.Dispose(); tcs.SetException(ex); } finally { writeReq.Dispose(); } }
public UvWriteResult(int status, UvException error) { Status = status; Error = error; }
private static void ConnectCallback(UvConnectRequest connect, int status, UvException error, TaskCompletionSource <int> tcs) { var listener = (ListenerSecondary)tcs.Task.AsyncState; _ = listener.ConnectedCallback(connect, status, error, tcs); }
public async Task Handles_Abrupt_Disconnects_Gracefully() { using (var server = TestServerFactory.Create(app => { var store = Substitute.For <ITusStore>(); store.FileExistAsync("testfile", Arg.Any <CancellationToken>()).Returns(true); store.GetUploadOffsetAsync("testfile", Arg.Any <CancellationToken>()).Returns(5); store.GetUploadLengthAsync("testfile", Arg.Any <CancellationToken>()).Returns(10); // IOException with an inner HttpListenerException is a client disconnect. // ReSharper disable once JoinDeclarationAndInitializer - Set depending on .NET version Exception innerException; #if netfull innerException = new HttpListenerException(); #else innerException = new UvException("Test", -4077); #endif store.AppendDataAsync("testfile", Arg.Any <Stream>(), Arg.Any <CancellationToken>()) .Throws(new IOException("Test exception", innerException)); app.UseTus(request => new DefaultTusConfiguration { Store = store, UrlPath = "/files" }); })) { var response = await server .CreateRequest("/files/testfile") .And(m => m.AddBody()) .AddTusResumableHeader() .AddHeader("Upload-Offset", "5") .SendAsync("PATCH"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var body = await response.Content.ReadAsStreamAsync(); body.Length.ShouldBe(0); } // IOException without an inner HttpListenerException is not a disconnect. using (var server = TestServerFactory.Create(app => { var store = Substitute.For <ITusStore>(); store.FileExistAsync("testfile", Arg.Any <CancellationToken>()).Returns(true); store.GetUploadOffsetAsync("testfile", Arg.Any <CancellationToken>()).Returns(5); store.GetUploadLengthAsync("testfile", Arg.Any <CancellationToken>()).Returns(10); store.AppendDataAsync("testfile", Arg.Any <Stream>(), Arg.Any <CancellationToken>()) .Throws(new IOException("Test exception")); app.UseTus(request => new DefaultTusConfiguration { Store = store, UrlPath = "/files" }); })) { // ReSharper disable once AccessToDisposedClosure await Should.ThrowAsync <IOException>(async() => await server .CreateRequest("/files/testfile") .And(m => m.AddBody()) .AddTusResumableHeader() .AddHeader("Upload-Offset", "5") .SendAsync("PATCH")); } }
public UvWebClientGetArgs(UvException exception, string data = null) { _exception = exception; _data = data; }