Skip to content

Commit

Permalink
Merge branch 'SatoshiARA-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed Jan 10, 2016
2 parents 356d0c5 + 443d458 commit 23ad4e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<MandroidI18n />
<JavaMaximumHeapSize />
<JavaOptions />
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
4 changes: 4 additions & 0 deletions Sockets/Sockets.Implementation.NET/UdpSocketBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected Task SendToAsync(byte[] data, int length, string address, int port)
.WrapNativeSocketExceptions();
}


/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
Expand All @@ -141,7 +142,10 @@ private void Dispose(bool disposing)
if (disposing)
{
if (_backingUdpClient != null)
{
((IDisposable)_backingUdpClient).Dispose();
_backingUdpClient = null;
}
}
}

Expand Down
37 changes: 29 additions & 8 deletions Sockets/Sockets.Implementation.NET/UdpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public UdpSocketClient()
_backingUdpClient = new UdpClient
{
EnableBroadcast = true

};
}
catch (PlatformSocketException ex)
Expand All @@ -48,9 +49,11 @@ public Task ConnectAsync(string address, int port)
{
_messageCanceller = new CancellationTokenSource();

return Task
.Run(() => this._backingUdpClient.Connect(address, port))
.WrapNativeSocketExceptions();
return Task.Run(() => {
_backingUdpClient.Connect(address, port);
base.RunMessageReceiver(_messageCanceller.Token);
})
.WrapNativeSocketExceptions();
}

/// <summary>
Expand All @@ -59,10 +62,17 @@ public Task ConnectAsync(string address, int port)
/// </summary>
public Task DisconnectAsync()
{
return Task.Run(() =>
{
_messageCanceller.Cancel();
_backingUdpClient.Close();
return Task.Run(() => {
if (_messageCanceller != null)
{
_messageCanceller.Cancel();
_messageCanceller.Dispose();
_messageCanceller = null;
}
if (_backingUdpClient != null)
{
_backingUdpClient.Close();
}
});
}

Expand Down Expand Up @@ -110,5 +120,16 @@ public Task DisconnectAsync()
{
return base.SendToAsync(data, length, address, port);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public override void Dispose()
{
if (_messageCanceller != null && !_messageCanceller.IsCancellationRequested)
_messageCanceller.Cancel();

base.Dispose();
}
}
}
}

0 comments on commit 23ad4e8

Please sign in to comment.