Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancellation token #164

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions SharpSnmpLib/Messaging/Discoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ private void HandleMessage(byte[] buffer, int count, IPEndPoint remote)
/// <param name="broadcastAddress">The broadcast address.</param>
/// <param name="community">The community.</param>
/// <param name="interval">The discovering time interval, in milliseconds.</param>
/// <param name="cancellationToken">A cancellation token that can be used to signal the asynchronous operation should be canceled.</param>
/// <remarks><paramref name="broadcastAddress"/> must be an IPv4 address. IPv6 is not yet supported here.</remarks>
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval)
public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress, OctetString community, int interval, CancellationToken cancellationToken = default)
{
if (broadcastAddress == null)
{
Expand Down Expand Up @@ -325,7 +326,7 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress
{
udp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
var buffer = new ArraySegment<byte>(bytes);
await udp.SendToAsync(buffer, SocketFlags.None, broadcastAddress);
await udp.SendToAsync(buffer, SocketFlags.None, broadcastAddress, cancellationToken).ConfigureAwait(false);

var activeBefore = Interlocked.CompareExchange(ref _active, Active, Inactive);
if (activeBefore == Active)
Expand All @@ -336,8 +337,8 @@ public async Task DiscoverAsync(VersionCode version, IPEndPoint broadcastAddress

_bufferSize = udp.ReceiveBufferSize;
await Task.WhenAny(
ReceiveAsync(udp),
Task.Delay(interval));
ReceiveAsync(udp, cancellationToken),
Task.Delay(interval, cancellationToken)).ConfigureAwait(false);

Interlocked.CompareExchange(ref _active, Inactive, Active);
try
Expand All @@ -355,7 +356,7 @@ await Task.WhenAny(
}
}

private async Task ReceiveAsync(Socket socket)
private async Task ReceiveAsync(Socket socket, CancellationToken cancellationToken)
{
while (true)
{
Expand All @@ -370,8 +371,8 @@ private async Task ReceiveAsync(Socket socket)
EndPoint remote = new IPEndPoint(IPAddress.Any, 0);

var buffer = new byte[_bufferSize];
var result = await socket.ReceiveMessageFromAsync(new ArraySegment<byte>(buffer), SocketFlags.None, remote);
await Task.Factory.StartNew(() => HandleMessage(buffer, result.ReceivedBytes, (IPEndPoint) result.RemoteEndPoint))
var result = await socket.ReceiveMessageFromAsync(new ArraySegment<byte>(buffer), SocketFlags.None, remote, cancellationToken).ConfigureAwait(false);
await Task.Factory.StartNew(() => HandleMessage(buffer, result.ReceivedBytes, (IPEndPoint) result.RemoteEndPoint), cancellationToken)
.ConfigureAwait(false);
}
catch (SocketException ex)
Expand Down
6 changes: 4 additions & 2 deletions SharpSnmpLib/Messaging/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading;
using Lextm.SharpSnmpLib.Security;
using System.Threading.Tasks;

Expand Down Expand Up @@ -203,8 +204,9 @@ public ReportMessage GetResponse(int timeout, IPEndPoint receiver)
/// Gets the response.
/// </summary>
/// <param name="receiver">The receiver.</param>
/// <param name="cancellationToken">A cancellation token that can be used to signal the asynchronous operation should be canceled.</param>
/// <returns></returns>
public async Task<ReportMessage> GetResponseAsync(IPEndPoint receiver)
public async Task<ReportMessage> GetResponseAsync(IPEndPoint receiver, CancellationToken cancellationToken = default)
{
if (receiver == null)
{
Expand All @@ -213,7 +215,7 @@ public async Task<ReportMessage> GetResponseAsync(IPEndPoint receiver)

using (var socket = receiver.GetSocket())
{
return (ReportMessage)await _discovery.GetResponseAsync(receiver, Empty, socket).ConfigureAwait(false);
return (ReportMessage)await _discovery.GetResponseAsync(receiver, Empty, socket, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Loading