private void AsyncPollCallback(IAsyncResult result) { WatchSocket socket = (WatchSocket)result.AsyncState; bool ret = socket.EndPoll(result); if (socket.Stopping) { // If we're stopping, don't process any results, and don't begin a new poll. return; } if (ret) { PollInvokeableDelegate cb = new PollInvokeableDelegate(PollInvokeable); Invoke(cb, socket.SDRef); } // The user may have stopped the socket during the Invoke above if (!socket.Stopping) { AsyncCallback callback = new AsyncCallback(AsyncPollCallback); socket.BeginPoll(-1, SelectMode.SelectRead, callback, socket); } }
/// <summary> /// Starts polling the DNSService socket, and delegates /// data back to the primary DNSService API when data arrives /// on the socket. /// </summary> protected void SetupWatchSocket(IntPtr sdRef) { Int32 socketId = mDNSImports.DNSServiceRefSockFD(sdRef); WatchSocket socket = new WatchSocket(socketId, sdRef); sdRefToSocketMapping.Add(sdRef, socket); AsyncCallback callback = new AsyncCallback(AsyncPollCallback); IAsyncResult ar = socket.BeginPoll(-1, SelectMode.SelectRead, callback, socket); }
/// <summary> /// This method tears down a previously setup watch socket. /// </summary> protected void TeardownWatchSocket(IntPtr sdRef) { WatchSocket socket = (WatchSocket)sdRefToSocketMapping[sdRef]; if (socket != null) { socket.Stopping = true; // Note that we did not actually stop the poll. // This is because there is no way to actually stop the poll. // Our only option is to wait for the poll to finish. // And since we set the stopping variable, then no further action should be taken by the socket. // // This should be fine, since when the DNSServiceRefDeallocate(sdRef) method is invoked, // the socket will be shutdown, and the poll will complete. sdRefToSocketMapping.Remove(sdRef); } }