1

I'm using SharpSNMP (8.5, the latest version from NuGet) asynchronously to poll a device. It's pretty easy and working apparently fine but there's one thing I can't figure out even from looking at the source code for SharpSnmpLib: Where the type of the object supplied to the callback method is specified.

For example:

Here I crate a new SNMP GET request and call BeginGetResponse.

var message = new GetRequestMessage(RequestCounter.NextId, VersionCode.V2, community, oids);
var udpSocket = SNMPAddress.GetSocket();

// pass my GetRequestMessage as the state object
var v = message.BeginGetResponse(SNMPAddress, new UserRegistry(), udpSocket, new AsyncCallback(HandlePollCompletion), message);

The method message.BeginGetResponse is defined in the SnmpMessageExtension class and ultimately calls BeginReceive on the socket (following snippet is from the SharpSNMP code):

public static IAsyncResult BeginGetResponse(this ISnmpMessage request, IPEndPoint receiver, UserRegistry registry, Socket udpSocket, AsyncCallback callback, object state)
{

    // ** code snipped from example for readability **

    var ar = udpSocket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, callback, state);
    return new SnmpMessageAsyncResult(ar, udpSocket, registry, receiver, buffer);
}

This returns a SnmpMessageAsyncResult object but this is never passed to the call to udpSocket.BeginReceive so I can't see how it then can later turn up passed to the callback method.

When the reply comes in, my handler method HandlePollCompletion is called:

private void HandlePollCompletion(IAsyncResult ar)
{
    // grab handle to original message
    GetRequestMessage message = (GetRequestMessage)ar.AsyncState;

    // end the async call
    try 
    {
        var response = message.EndGetResponse(ar);
    } 
    catch (Exception ex) {}

    // process reply here
}

If I put a breakpoint in, I can see the object ar that is passed to HandlePollCompletion(IAsyncResult ar) has the type SnmpMessageAsyncResult.

But as far as I can see, HandlePollResult(IAsyncResult ar) is called by the socket i.e. not from within SharpSNMP.

So by what mechanism is the object passed to HandlePollCompletion(IAsyncResult ar) a SnmpMessageAsyncResult? Perhaps I don't understand the async model as well as I thought I did..

Thanks for your insight, Giles.

1 Answers1

0

Generally speaking, you don't need to care much about that.

Begin/End pair is now obsolete and you should use the Async method with async/await.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • OK. Thanks. I was just intrigued. Regarding the Async methods, I see they're in Github but apparently not in the latest version on NuGet; Are you planning an updated release soon or should I pull the latest code from Github and compile the DLL(s) myself? – Giles at work Jan 27 '16 at 08:55
  • @Gilesatwork the master branch should be marked as 9.0 RC soon. Yes, a new release is coming pretty soon. – Lex Li Jan 27 '16 at 10:32
  • @Gilesatwork 9.0 RC1 is here, https://sharpsnmplib.codeplex.com/releases/view/116908 RTM will be soon published. – Lex Li Feb 02 '16 at 11:21
  • Thanks - grabbed the RC and am having a play now. Most appreciated. – Giles at work Feb 03 '16 at 14:00
  • @Gilesatwork I have published the final build today, but one critical issue about async support was found a few days ago. It would lead to high memory fragmentation, so I am afraid would be a road blocker for you. Will try to address that in 9.1 or 9.5 In a few weeks (with big design changes). – Lex Li Feb 12 '16 at 15:31