public async Task DisposeWhileReceiving () { var task = Incoming.ReceiveAsync (new ByteBuffer (100), 0, 100).AsTask (); Incoming.Dispose (); // All we care about is that the task is marked as 'Complete'. _ = await Task.WhenAny (task).WithTimeout (1000); Assert.IsTrue (task.IsCompleted, "#1"); GC.KeepAlive (task.Exception); // observe the exception (if any) }
public void DisposeTwice() { // Setup SocketConnection connection = new SocketConnection(); connection.Dispose(); // Act and Verify Assert.DoesNotThrow(() => connection.Dispose()); }
public void Upload(string sourcePath, string destPath) { OpenDataConnection(); ExecuteCommand($"STOR {destPath}").Print(); var bytes = File.ReadAllBytes(sourcePath); _dataConnection.Send(bytes); _dataConnection.Dispose(); _controlConnection.Receive(50).Print(); }
public void DisposeWhileReceiving() { var task = Incoming.ReceiveAsync(new byte[100], 0, 100); Incoming.Dispose(); try { Assert.IsTrue(task.Wait(1000), "#1"); } catch (AggregateException ex) { Assert.IsInstanceOf <SocketException> (ex.InnerException, "#3"); } }
public void StopsTheClient() { var mock = new Mock <ISocketClient>(); var con = new SocketConnection(mock.Object, AuthTokens.None, Logger, null); con.Dispose(); mock.Verify(c => c.Stop(), Times.Once); }
public void Dispose() { _watcher?.Dispose(); _launcher.Teardown(); _socket?.Dispose(); }
public void Cleanup() { _pipeClient?.Dispose(); _pipeServer?.Dispose(); _socketClient.Dispose(); _client.Dispose(); _server.Dispose(); }
public async Task Start(TestType testType) { Interlocked.Increment(ref s_connectBeginCnt); SocketConnection conn = null; TcpClient client = null; switch (testType) { case TestType.Pipeline: _stopwatch.Start(); conn = await SocketConnection.ConnectAsync(_server, pipeOptions : s_pipeOptions); _protocol = new PipeFrameProtocol(conn.Input, conn.Output); break; case TestType.TcpSocket: client = new TcpClient(); _stopwatch.Start(); await client.ConnectAsync(((IPEndPoint)_server).Address, ((IPEndPoint)_server).Port); _protocol = new TcpFrameProtocol(client.Client); break; } Interlocked.Increment(ref s_connectFinishCnt); ConnectDuration = _stopwatch.Elapsed; _stopwatch.Restart(); try { for (int i = 0; i < _echoRound; i++) { Interlocked.Increment(ref s_writeBeginCnt); await _protocol.WriteAsync(_payload); Interlocked.Increment(ref s_writeFinishCnt); ReadOnlyMemory <byte> buffer = await _protocol.ReadAsync(); if (buffer.Length == 0) { return; } Interlocked.Increment(ref s_readFinishCnt); } EchoDuration = _stopwatch.Elapsed; } catch (Exception e) { Error = e; } finally { conn?.Dispose(); client?.Dispose(); } }
protected override void Dispose(bool disposing) { if (disposing) { // This will call base.Dispose(). _connection.Dispose(); } else { base.Dispose(disposing); } }
private void OpenDataConnection() { _dataConnection?.Dispose(); _controlConnection.Send("PASV"); var recv = _controlConnection.Receive(100).Message; var(ip, port) = CalculatePasvIpAddressFromResponse(recv); _dataConnection = new SocketConnection(IPAddress.Parse(ip), port); _dataConnection.Connect(); }
protected virtual void OnDestroy() { if (_proc != null && !_proc.HasExited) { _proc.Kill(); } if (_connection != null) { _connection.Dispose(); } }
async Task IDuplexMessageStream.CloseAsync() { await connection.Input.CompleteAsync().ConfigureAwait(false); await connection.Output.CompleteAsync().ConfigureAwait(false); connection.Dispose(); ReadCompleted = true; WriteCompleted = true; connection = null; }
private async Task HandleConnectionAsync(SocketConnection connection) { try { var middlewareTask = _dispatcher.OnConnection(connection); var transportTask = connection.StartAsync(); await transportTask; await middlewareTask; connection.Dispose(); } catch (Exception ex) { _trace.LogCritical(ex, $"Unexpected exception in {nameof(SocketTransport)}.{nameof(HandleConnectionAsync)}."); } }
private async void StartListen() { byte[] buffer = new byte[ReceiveChunkSize]; try { while (SocketConnection.State == WebSocketState.Open) { var stringResult = new StringBuilder(); WebSocketReceiveResult result; do { result = await SocketConnection.ReceiveAsync(new ArraySegment <byte>(buffer), _cancellationToken); if (result.MessageType == WebSocketMessageType.Close) { await SocketConnection.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); OnClose(); } else { var str = Encoding.UTF8.GetString(buffer, 0, result.Count); stringResult.Append(str); } } while (!result.EndOfMessage); OnMessage(stringResult.ToString()); } } catch (Exception ex) { OnClose(); OnConnectionError(ex.Message); } finally { SocketConnection.Dispose(); } }
public async Task Start() { _stopwatch.Start(); Interlocked.Increment(ref ConnectBeginCnt); SocketConnection conn = await SocketConnection.ConnectAsync(_server); Interlocked.Increment(ref ConnectFinishCnt); ConnectDuration = _stopwatch.Elapsed; _stopwatch.Restart(); try { FrameProtocol.FrameProtocol protocol = new FrameProtocol.FrameProtocol(conn.Input, conn.Output); for (int i = 0; i < _echoRound; i++) { Interlocked.Increment(ref WriteBeginCnt); await protocol.WriteAsync(_payload); Interlocked.Increment(ref WriteFinishCnt); (var imo, var len) = await protocol.ReadAsync(); Interlocked.Increment(ref ReadFinishCnt); using (imo) { if (len != _payload.Length) { throw new Exception("unexpect echo result"); } } } EchoDuration = _stopwatch.Elapsed; } catch (Exception e) { Error = e; } finally { conn.Dispose(); } }
public void Close(SocketConnection socket) { socket.Dispose(); }
private async ValueTask <SocketConnection> BuildSSLAsync(RequestBuilder builder) { var upstream = UpstreamProxy; var request = builder.Session.Request; var destination = request.RequestUri; if (upstream == null) { foreach (var ip in await Dns.GetHostAddressesAsync(destination.DnsSafeHost)) { try { return(await SocketConnection.ConnectAsync(new IPEndPoint(ip, destination.Port))); } catch { } } } else { var upstreamUri = upstream.GetProxy(destination); foreach (var upstreamIp in await Dns.GetHostAddressesAsync(upstreamUri.DnsSafeHost)) { SocketConnection connection = null; try { connection = await SocketConnection.ConnectAsync(new IPEndPoint(upstreamIp, upstreamUri.Port)); var output = connection.Output; WriteUtf8(output, $"CONNECT {destination.Host}:{destination.Port} HTTP/1.1\r\n"); foreach (var header in request.Headers) { WriteUtf8(output, $"{header.Key}: {string.Join("; ", header.Value)}\r\n"); } foreach (var header in builder.PendingHeaders) { WriteUtf8(output, $"{header.Key}: {string.Join("; ", header.Value)}\r\n"); } WriteUtf8(output, "\r\n"); await output.FlushAsync(); if (await TryGet200Async(connection.Input)) { return(connection); } connection.Dispose(); } catch { if (connection != null) { connection.Dispose(); } } } } return(null); }
public void Teardown() { Incoming?.Dispose(); Outgoing?.Dispose(); }