Skip to content

Commit

Permalink
Throw IOException when SslStream is not authenticated or disposed.
Browse files Browse the repository at this point in the history
  • Loading branch information
xinchen10 committed Jun 2, 2022
1 parent facbd01 commit 9f78add
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
<FileAlignment>512</FileAlignment>
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>
<NoWarn>CS1734,CS1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected override void OnListen()
for (int i = 0; i < addresses.Count; ++i)
{
this.listenSockets[i] = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
this.listenSockets[i].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
this.listenSockets[i].Bind(new IPEndPoint(addresses[i], this.transportSettings.Port));
this.listenSockets[i].Listen(this.transportSettings.TcpBacklog);

Expand Down
36 changes: 34 additions & 2 deletions Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.Azure.Amqp.Transport
{
using System;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Amqp.X509;
Expand Down Expand Up @@ -78,7 +79,20 @@ public override bool WriteAsync(TransportAsyncCallbackArgs args)
}
}

IAsyncResult result = this.sslStream.BeginWrite(buffer.Array, buffer.Offset, buffer.Count, onWriteComplete, this);
IAsyncResult result;
try
{
result = this.sslStream.BeginWrite(buffer.Array, buffer.Offset, buffer.Count, onWriteComplete, this);
}
catch (ObjectDisposedException ode)
{
throw new IOException($"Transport '{this}' is closed", ode);
}
catch (InvalidOperationException ioe)
{
throw new IOException($"Transport '{this}' is valid for write operations.", ioe);
}

bool completedSynchronously = result.CompletedSynchronously;
if (completedSynchronously)
{
Expand All @@ -94,7 +108,20 @@ public override bool ReadAsync(TransportAsyncCallbackArgs args)
Fx.Assert(args.Buffer != null, "must have buffer to read");
Fx.Assert(this.readState.Args == null, "Cannot read when a read is still in progress");
this.readState.Args = args;
IAsyncResult result = this.sslStream.BeginRead(args.Buffer, args.Offset, args.Count, onReadComplete, this);
IAsyncResult result;
try
{
result = this.sslStream.BeginRead(args.Buffer, args.Offset, args.Count, onReadComplete, this);
}
catch (ObjectDisposedException ode)
{
throw new IOException($"Transport '{this}' is closed", ode);
}
catch (InvalidOperationException ioe)
{
throw new IOException($"Transport '{this}' is valid for read operations.", ioe);
}

bool completedSynchronously = result.CompletedSynchronously;
if (completedSynchronously)
{
Expand Down Expand Up @@ -252,6 +279,11 @@ void HandleOperationComplete(IAsyncResult result, bool write, bool syncComplete)
}
catch (Exception exception) when (!Fx.IsFatal(exception))
{
if (exception is InvalidOperationException)
{
exception = new IOException($"Transport '{this}' is valid for IO operations.", exception);
}

args.Exception = exception;
}

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Amqp/Properties/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("2.4.0.0")]
[assembly: AssemblyInformationalVersion("2.5.10")]
[assembly: AssemblyInformationalVersion("2.5.12")]
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
Expand Down

0 comments on commit 9f78add

Please sign in to comment.