Beispiel #1
0
        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");
            }
        }
Beispiel #2
0
        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));
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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));
        }
Beispiel #6
0
        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));
        }
Beispiel #7
0
        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();
            }
        }
Beispiel #8
0
 public UvWriteResult(int status, UvException error)
 {
     Status = status;
     Error  = error;
 }
Beispiel #9
0
        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);
        }
Beispiel #10
0
        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"));
            }
        }
Beispiel #11
0
 public UvWebClientGetArgs(UvException exception, string data = null)
 {
     _exception = exception;
     _data = data;
 }
Beispiel #12
0
 public UvWebClientGetArgs(UvException exception, string data = null)
 {
     _exception = exception;
     _data      = data;
 }