diff --git a/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.xml b/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.xml index eb1dc0b..4a12ecd 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.xml +++ b/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.xml @@ -488,7 +488,7 @@ await TaskEx.FromAsync( (c, s) => ((Transaction)s).BeginCommit(c, s), (a) => ((Transaction)a.AsyncState).EndCommit(a), - transaction); + transaction).ConfigureAwait(false); diff --git a/src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs b/src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs index b73e523..68c0892 100644 --- a/src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs +++ b/src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs @@ -54,7 +54,7 @@ public async void Close() shuttingDown.Cancel(); ; this.IsOpen = false; - await this.listener.CloseAsync(TimeSpan.FromSeconds(5)); + await this.listener.CloseAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false); } public void Dispose() @@ -100,7 +100,7 @@ public async Task Open() }; } - await listener.OpenAsync(shuttingDown.Token); + await listener.OpenAsync(shuttingDown.Token).ConfigureAwait(false); this.IsOpen = true; AcceptLoopAsync().Fork(this); @@ -112,7 +112,7 @@ async Task AcceptLoopAsync() { try { - var hybridConnectionStream = await listener.AcceptConnectionAsync(); + var hybridConnectionStream = await listener.AcceptConnectionAsync().ConfigureAwait(false); if (hybridConnectionStream == null) { // we only get null if trhe listener is shutting down @@ -144,10 +144,10 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr for (int read = 0; read < versionPreamble.Length;) { var r = await hybridConnectionStream.ReadAsync(versionPreamble, read, - versionPreamble.Length - read); + versionPreamble.Length - read).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); return; } @@ -162,20 +162,20 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr // length indicator and then that number of bytes with of UTF-8 encoded // port-name string. var portNameBuffer = new byte[256]; - var r = await hybridConnectionStream.ReadAsync(portNameBuffer, 0, 1); + var r = await hybridConnectionStream.ReadAsync(portNameBuffer, 0, 1).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); return; } for (int read = 0; read < portNameBuffer[0];) { r = await hybridConnectionStream.ReadAsync(portNameBuffer, read + 1, - portNameBuffer[0] - read); + portNameBuffer[0] - read).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); return; } @@ -188,8 +188,8 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr { // if we don't understand the version, we write a 0.0 version preamble back and shut down the connection versionPreamble = new byte[] { 0, 0, 0 }; - await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length); - await CloseConnection(hybridConnectionStream); + await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false); + await CloseConnection(hybridConnectionStream).ConfigureAwait(false); return; } @@ -212,28 +212,28 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr { // bad datagram indicator versionPreamble = new byte[] { 0, 0, 1 }; - await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length); - await CloseConnection(hybridConnectionStream); + await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false); + await CloseConnection(hybridConnectionStream).ConfigureAwait(false); return; } else if (!(forwarder is UdpRemoteForwarder) && versionPreamble[2] == 1) { // mismatch versionPreamble = new byte[] { 0, 0, 255 }; - await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length); - await CloseConnection(hybridConnectionStream); + await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false); + await CloseConnection(hybridConnectionStream).ConfigureAwait(false); return; } // write out 1.0 and handle the stream versionPreamble = new byte[] { 1, 0, versionPreamble[2] }; - await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length); - await forwarder.HandleConnectionAsync(hybridConnectionStream); - await CloseConnection(hybridConnectionStream); + await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false); + await forwarder.HandleConnectionAsync(hybridConnectionStream).ConfigureAwait(false); + await CloseConnection(hybridConnectionStream).ConfigureAwait(false); } else { - await CloseConnection(hybridConnectionStream); + await CloseConnection(hybridConnectionStream).ConfigureAwait(false); } } } @@ -247,8 +247,8 @@ static async Task CloseConnection(HybridConnectionStream hybridConnectionStream) { using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1))) { - await hybridConnectionStream.ShutdownAsync(cts.Token); - await hybridConnectionStream.CloseAsync(cts.Token); + await hybridConnectionStream.ShutdownAsync(cts.Token).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false); } } } diff --git a/src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs b/src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs index 6b7fae2..a141cd5 100644 --- a/src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs +++ b/src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs @@ -129,7 +129,7 @@ async Task AcceptSocketLoopAsync() try { - socket = await this.socketListener.AcceptAsync(); + socket = await this.socketListener.AcceptAsync().ConfigureAwait(false); } catch (ObjectDisposedException) { @@ -192,7 +192,7 @@ async Task BridgeSocketConnectionAsync(Socket socket) socket.SendTimeout = 60000; var tcpstream = new NetworkStream(socket); - using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync()) + using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync().ConfigureAwait(false)) { // read and write 4-byte header hybridConnectionStream.WriteTimeout = 60000; @@ -206,18 +206,18 @@ async Task BridgeSocketConnectionAsync(Socket socket) /*stream */ 0, (byte)portNameBytes.Length }; - await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length); - await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length); + await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false); + await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length).ConfigureAwait(false); byte[] replyPreamble = new byte[3]; for (int read = 0; read < replyPreamble.Length;) { var r = await hybridConnectionStream.ReadAsync(replyPreamble, read, - replyPreamble.Length - read); + replyPreamble.Length - read).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); throw new InvalidOperationException($"Malformed preamble from server"); } read += r; @@ -226,8 +226,8 @@ async Task BridgeSocketConnectionAsync(Socket socket) if (!(replyPreamble[0] == 1 && replyPreamble[1] == 0 && replyPreamble[2] == 0)) { // version not supported - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); throw new InvalidOperationException($"Unsupported protocol version: Server reply {replyPreamble[0]} {replyPreamble[1]} {replyPreamble[2]}"); } @@ -243,11 +243,11 @@ await Task.WhenAll( .ContinueWith((t)=>socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted), StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream?.Shutdown(), socketAbort.Token)) - .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted); + .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false); using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1))) { - await hybridConnectionStream.CloseAsync(cts.Token); + await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false); } } catch diff --git a/src/Microsoft.Azure.Relay.Bridge/SocketRemoteForwarder.cs b/src/Microsoft.Azure.Relay.Bridge/SocketRemoteForwarder.cs index cac8706..84edbdf 100644 --- a/src/Microsoft.Azure.Relay.Bridge/SocketRemoteForwarder.cs +++ b/src/Microsoft.Azure.Relay.Bridge/SocketRemoteForwarder.cs @@ -28,7 +28,7 @@ public async Task HandleConnectionAsync(Microsoft.Azure.Relay.HybridConnectionSt { socket.SendBufferSize = socket.ReceiveBufferSize = 65536; socket.SendTimeout = 60000; - await socket.ConnectAsync(new UnixDomainSocketEndPoint(targetServer)); + await socket.ConnectAsync(new UnixDomainSocketEndPoint(targetServer)).ConfigureAwait(false); var tcpstream = new NetworkStream(socket); CancellationTokenSource socketAbort = new CancellationTokenSource(); @@ -38,7 +38,7 @@ await Task.WhenAll( .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted), StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream.Shutdown(), socketAbort.Token)) - .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted); + .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false); } } } diff --git a/src/Microsoft.Azure.Relay.Bridge/StreamPump.cs b/src/Microsoft.Azure.Relay.Bridge/StreamPump.cs index ce11609..4757a9e 100644 --- a/src/Microsoft.Azure.Relay.Bridge/StreamPump.cs +++ b/src/Microsoft.Azure.Relay.Bridge/StreamPump.cs @@ -25,7 +25,7 @@ public static async Task RunAsync(Stream source, Stream target, Action shutdownA int bytesRead; bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false); - + if (bytesRead == 0) { shutdownAction?.Invoke(); diff --git a/src/Microsoft.Azure.Relay.Bridge/TaskEx.cs b/src/Microsoft.Azure.Relay.Bridge/TaskEx.cs index 8789eb7..55c7d74 100644 --- a/src/Microsoft.Azure.Relay.Bridge/TaskEx.cs +++ b/src/Microsoft.Azure.Relay.Bridge/TaskEx.cs @@ -34,7 +34,7 @@ public static void Fork(this Task thisTask) /// await TaskEx.FromAsync( /// (c, s) => ((Transaction)s).BeginCommit(c, s), /// (a) => ((Transaction)a.AsyncState).EndCommit(a), - /// transaction); + /// transaction).ConfigureAwait(false).ConfigureAwait(false); /// public static Task FromAsync(Func begin, Action end, object state = null) { diff --git a/src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs b/src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs index 56b71d8..14571d0 100644 --- a/src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs +++ b/src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs @@ -127,7 +127,7 @@ async Task AcceptSocketLoopAsync() try { - socket = await this.tcpListener.AcceptTcpClientAsync(); + socket = await this.tcpListener.AcceptTcpClientAsync().ConfigureAwait(false); } catch (ObjectDisposedException) { @@ -191,7 +191,7 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient) var tcpstream = tcpClient.GetStream(); var socket = tcpClient.Client; - using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync()) + using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync().ConfigureAwait(false)) { // read and write version preamble hybridConnectionStream.WriteTimeout = 60000; @@ -205,18 +205,18 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient) /*stream mode*/ 0, (byte)portNameBytes.Length }; - await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length); - await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length); - + await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false); + await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length).ConfigureAwait(false); + byte[] replyPreamble = new byte[3]; for (int read = 0; read < replyPreamble.Length;) { var r = await hybridConnectionStream.ReadAsync(replyPreamble, read, - replyPreamble.Length - read); + replyPreamble.Length - read).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); return; } read += r; @@ -225,8 +225,8 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient) if (!(replyPreamble[0] == 1 && replyPreamble[1] == 0 && replyPreamble[2] == 0)) { // version not supported - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); return; } @@ -242,11 +242,11 @@ await Task.WhenAll( .ContinueWith((t)=>socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted), StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream?.Shutdown(), socketAbort.Token)) - .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted); + .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false); using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1))) { - await hybridConnectionStream.CloseAsync(cts.Token); + await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false); } } catch diff --git a/src/Microsoft.Azure.Relay.Bridge/TcpRemoteForwarder.cs b/src/Microsoft.Azure.Relay.Bridge/TcpRemoteForwarder.cs index e5fd6f3..c6707a9 100644 --- a/src/Microsoft.Azure.Relay.Bridge/TcpRemoteForwarder.cs +++ b/src/Microsoft.Azure.Relay.Bridge/TcpRemoteForwarder.cs @@ -93,7 +93,7 @@ await Task.WhenAll( () => client.Client.Shutdown(SocketShutdown.Send), socketAbort.Token) .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted), StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream.Shutdown(), socketAbort.Token)) - .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted); + .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false); } diff --git a/src/Microsoft.Azure.Relay.Bridge/UdpLocalForwardBridge.cs b/src/Microsoft.Azure.Relay.Bridge/UdpLocalForwardBridge.cs index 2760803..61a3b02 100644 --- a/src/Microsoft.Azure.Relay.Bridge/UdpLocalForwardBridge.cs +++ b/src/Microsoft.Azure.Relay.Bridge/UdpLocalForwardBridge.cs @@ -137,7 +137,7 @@ async Task ReceiveDatagramAsync() try { - datagram = await this.udpClient.ReceiveAsync(); + datagram = await this.udpClient.ReceiveAsync().ConfigureAwait(false); } catch (ObjectDisposedException) { @@ -153,12 +153,12 @@ async Task ReceiveDatagramAsync() { using (var ct = new CancellationTokenSource(TimeSpan.FromSeconds(1))) { - await route.SendAsync(datagram, ct.Token); + await route.SendAsync(datagram, ct.Token).ConfigureAwait(false); } } else { - var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync(); + var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync().ConfigureAwait(false); // read and write version preamble hybridConnectionStream.WriteTimeout = 60000; @@ -171,18 +171,18 @@ async Task ReceiveDatagramAsync() /*dgram*/ 1, (byte)portNameBytes.Length }; - await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length); - await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length); + await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false); + await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length).ConfigureAwait(false); byte[] replyPreamble = new byte[3]; for (int read = 0; read < replyPreamble.Length;) { var r = await hybridConnectionStream.ReadAsync(replyPreamble, read, - replyPreamble.Length - read); + replyPreamble.Length - read).ConfigureAwait(false); if (r == 0) { - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); return; } read += r; @@ -191,8 +191,8 @@ async Task ReceiveDatagramAsync() if (!(replyPreamble[0] == 1 && replyPreamble[1] == 0 && replyPreamble[2] == 1)) { // version not supported - await hybridConnectionStream.ShutdownAsync(CancellationToken.None); - await hybridConnectionStream.CloseAsync(CancellationToken.None); + await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); + await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false); return; } @@ -202,7 +202,7 @@ async Task ReceiveDatagramAsync() newRoute.StartReceiving(); using (var ct = new CancellationTokenSource(TimeSpan.FromSeconds(1))) { - await newRoute.SendAsync(datagram, ct.Token); + await newRoute.SendAsync(datagram, ct.Token).ConfigureAwait(false); } } } @@ -247,8 +247,8 @@ public async Task SendAsync(UdpReceiveResult datagram, CancellationToken ct) ms.Write(datagram.Buffer, 0, datagram.Buffer.Length); if (!ct.IsCancellationRequested) { - await target.WriteAsync(buffer, 0, (int)ms.Length, ct); - await target.FlushAsync(ct); + await target.WriteAsync(buffer, 0, (int)ms.Length, ct).ConfigureAwait(false); + await target.FlushAsync(ct).ConfigureAwait(false); } } } @@ -265,7 +265,7 @@ public void StartReceiving() int read = 0; while (read < 2) { - var r = await target.ReadAsync(length, 0, 2, this.routeCancel.Token); + var r = await target.ReadAsync(length, 0, 2, this.routeCancel.Token).ConfigureAwait(false); if (r == 0) { return; @@ -280,14 +280,14 @@ public void StartReceiving() int toRead = length[0] * 256 + length[1]; while (read < toRead) { - var r = await target.ReadAsync(buffer, 0, toRead, this.routeCancel.Token); + var r = await target.ReadAsync(buffer, 0, toRead, this.routeCancel.Token).ConfigureAwait(false); if (r == 0) { return; } read += r; } - await client.SendAsync(buffer, toRead, this.IPEndPoint); + await client.SendAsync(buffer, toRead, this.IPEndPoint).ConfigureAwait(false); } while (!this.routeCancel.IsCancellationRequested); }); diff --git a/src/Microsoft.Azure.Relay.Bridge/UdpRemoteForwarder.cs b/src/Microsoft.Azure.Relay.Bridge/UdpRemoteForwarder.cs index 0fc5509..42cc73c 100644 --- a/src/Microsoft.Azure.Relay.Bridge/UdpRemoteForwarder.cs +++ b/src/Microsoft.Azure.Relay.Bridge/UdpRemoteForwarder.cs @@ -73,7 +73,7 @@ await Task.WhenAll( Send(hybridConnectionStream, client, socketAbort) .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted), Receive(hybridConnectionStream, client, socketAbort) - .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted)); + .ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted)).ConfigureAwait(false); } } @@ -89,7 +89,7 @@ async Task Send(Stream source, UdpClient target, CancellationTokenSource cancell int read = 0; while (read < 2) { - var r = await source.ReadAsync(length, 0, 2, cancellationToken.Token); + var r = await source.ReadAsync(length, 0, 2, cancellationToken.Token).ConfigureAwait(false); if (r == 0) { return; @@ -103,14 +103,14 @@ async Task Send(Stream source, UdpClient target, CancellationTokenSource cancell int toRead = length[0] * 256 + length[1]; while (read < toRead) { - var r = await source.ReadAsync(buffer, 0, toRead, cancellationToken.Token); + var r = await source.ReadAsync(buffer, 0, toRead, cancellationToken.Token).ConfigureAwait(false); if (r == 0) { return; } read += r; } - await target.SendAsync(buffer, toRead); + await target.SendAsync(buffer, toRead).ConfigureAwait(false); } while (!cancellationToken.IsCancellationRequested); } @@ -127,7 +127,7 @@ async Task Receive(Stream source, UdpClient target, CancellationTokenSource canc { while (!cancellationToken.IsCancellationRequested) { - var datagram = await target.ReceiveAsync(); + var datagram = await target.ReceiveAsync().ConfigureAwait(false); cancellationToken.CancelAfter(maxIdleTime); var buffer = new byte[datagram.Buffer.Length + 2]; using (var ms = new MemoryStream(buffer)) @@ -136,7 +136,7 @@ async Task Receive(Stream source, UdpClient target, CancellationTokenSource canc ms.Write(datagram.Buffer, 0, datagram.Buffer.Length); if (!cancellationToken.IsCancellationRequested) { - await source.WriteAsync(buffer, 0, (int)ms.Length, cancellationToken.Token); + await source.WriteAsync(buffer, 0, (int)ms.Length, cancellationToken.Token).ConfigureAwait(false); } } } diff --git a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/BridgeTest.cs b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/BridgeTest.cs index 6cac789..db1e7e2 100644 --- a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/BridgeTest.cs +++ b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/BridgeTest.cs @@ -68,20 +68,21 @@ public void TcpBridge() } }); - var s = new TcpClient(); - s.Connect("127.0.97.1", 29876); - var sstream = s.GetStream(); - using (var w = new StreamWriter(sstream)) + using (var s = new TcpClient()) { - w.WriteLine("Hello!"); - w.Flush(); - using (var b = new StreamReader(sstream)) + s.Connect("127.0.97.1", 29876); + var sstream = s.GetStream(); + using (var w = new StreamWriter(sstream)) { - Assert.Equal("Hello!", b.ReadLine()); + w.WriteLine("Hello!"); + w.Flush(); + using (var b = new StreamReader(sstream)) + { + Assert.Equal("Hello!", b.ReadLine()); + } } } - s.Dispose(); l.Stop(); } finally @@ -118,41 +119,54 @@ public void UdpBridge() try { // now try to use it - var l = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.97.2"), 29877)); - l.ReceiveAsync().ContinueWith(async (t) => + using (var l = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.97.2"), 29877))) { - var c = t.Result; - var stream = c.Buffer; - using (var b = new StreamReader(new MemoryStream(stream))) + l.ReceiveAsync().ContinueWith(async (t) => { - var text = b.ReadLine(); - var m1 = new MemoryStream(); - using (var w = new StreamWriter(m1)) + var c = t.Result; + var stream = c.Buffer; + using (var mr = new MemoryStream(stream)) { - w.WriteLine(text); - w.Flush(); - await l.SendAsync(m1.GetBuffer(), (int)m1.Length, c.RemoteEndPoint); + using (var b = new StreamReader(mr)) + { + var text = b.ReadLine(); + using (var mw = new MemoryStream()) + { + using (var w = new StreamWriter(mw)) + { + w.WriteLine(text); + w.Flush(); + await l.SendAsync(mw.GetBuffer(), (int)mw.Length, c.RemoteEndPoint); + } + } + } } - } - }); + }); - var s = new UdpClient(); - s.Connect("127.0.97.1", 29876); - MemoryStream m = new MemoryStream(); - using (var w = new StreamWriter(m)) - { - w.WriteLine("Hello!"); - w.Flush(); - s.Send(m.GetBuffer(), (int)m.Length); - IPEndPoint addr = null; - var buf = s.Receive(ref addr); - using (var b = new StreamReader(new MemoryStream(buf))) + using (var s = new UdpClient()) { - Assert.Equal("Hello!", b.ReadLine()); + s.Connect("127.0.97.1", 29876); + using (MemoryStream mw = new MemoryStream()) + { + using (var w = new StreamWriter(mw)) + { + w.WriteLine("Hello!"); + w.Flush(); + s.Send(mw.GetBuffer(), (int)mw.Length); + + IPEndPoint addr = null; + var buf = s.Receive(ref addr); + using (var mr = new MemoryStream(buf)) + { + using (var b = new StreamReader(mr)) + { + Assert.Equal("Hello!", b.ReadLine()); + } + } + } + } } } - - s.Dispose(); } finally { @@ -191,44 +205,48 @@ public void SocketBridge() Host host = new Host(cfg); host.Start(); - try { // now try to use it - var l = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); - l.Bind(new UnixDomainSocketEndPoint(rf.LocalSocket)); - l.Listen(5); - - l.AcceptAsync().ContinueWith((t) => + using (var l = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP)) { - var c = t.Result; - using (var b = new StreamReader(new NetworkStream(c))) - { - var text = b.ReadLine(); - using (var w = new StreamWriter(new NetworkStream(c))) - { - w.WriteLine(text); - w.Flush(); - } - } - }); + l.Bind(new UnixDomainSocketEndPoint(rf.LocalSocket)); + l.Listen(5); + l.AcceptAsync().ContinueWith((t) => + { + var c = t.Result; + using (var b = new StreamReader(new NetworkStream(c))) + { + var text = b.ReadLine(); + using (var w = new StreamWriter(new NetworkStream(c))) + { + w.WriteLine(text); + w.Flush(); + } + } + }); - var s = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); - s.Connect(new UnixDomainSocketEndPoint(lf.BindLocalSocket)); - var ns = new NetworkStream(s); - using (var w = new StreamWriter(ns)) - { - w.WriteLine("Hello!"); - w.Flush(); - using (var b = new StreamReader(ns)) + using (var s = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP)) { - string line = b.ReadLine(); - Assert.Equal("Hello!", line); + s.Connect(new UnixDomainSocketEndPoint(lf.BindLocalSocket)); + using (var ns = new NetworkStream(s)) + { + using (var w = new StreamWriter(ns)) + { + w.WriteLine("Hello!"); + w.Flush(); + using (var b = new StreamReader(ns)) + { + string line = b.ReadLine(); + Assert.Equal("Hello!", line); + } + } + } + s.Close(0); + l.Close(0); } } - s.Close(0); - l.Close(0); } finally { @@ -271,23 +289,23 @@ public void TcpBridgeBadListener() l.Stop(); }); - var s = new TcpClient(); - s.Connect("127.0.97.1", 29876); - s.NoDelay = true; - s.Client.Blocking = true; - using (var w = s.GetStream()) + using (var s = new TcpClient()) { - byte[] bytes = new byte[1024 * 1024]; - Assert.Throws(() => + s.Connect("127.0.97.1", 29876); + s.NoDelay = true; + s.Client.Blocking = true; + using (var w = s.GetStream()) { - for (int i = 0; i < 5; i++) + byte[] bytes = new byte[1024 * 1024]; + Assert.Throws(() => { - w.Write(bytes, 0, bytes.Length); - } - }); + for (int i = 0; i < 5; i++) + { + w.Write(bytes, 0, bytes.Length); + } + }); + } } - - s.Dispose(); } finally {