What's a callback function?If you are new to asynchronous programming, or have never used callback functions before, just think of the SLP callback functions as a pieces of code the you must write but never call directly. Yep, that's right, you will probably never call your callback function directly, instead, it will be called by the library when it is ready to report the status or results of an operation. This allows your program to do other things while data is being collected by the callback function. Callback functions are required for all of the major SLP APIs for more information see SLPReg(), SLPDeReg(), SLPDelAttrs(), SLPFindSrvs(), SLPFindAttrs(), and SLPFindSrvTypes(),
Callback functions must accept the parameters that the caller (the SLP library) expects to pass to them. This is why callback function types are defined. See SLPRegReport(), SLPSrvURLCallback(), SLPAttrCallback().
What's different about SLP callback functions?Callbacks are an integral part of the SLP API. Developers usually associate callbacks with asynchronous APIs, but the SLP API uses callbacks for both synchronous and asynchronous operations. Whether the callback is called synchronously or asynchronously, depends on the isasync parameter in the call to SLPOpen(). Remember the following rules and you should not have any problems with your callback functions.
How does OpenSLP library handle asynchronous operation?When an SLP library call is made with an SLPHandle that was opened in async mode, the library does everything it can with out blocking. It then creates a thread (hopefully a user level thread) and returns SLP_OK. The newly created thread processes the request (possibly blocking to wait for data to arrive from the network) and calls the callback function as data is received.
An important thing to remember is that no collection or collation of results is performed by the library when a call is initiated in async mode. This means that the callback may be called multiple times with the same result. This would happen for example if two SAs or DAs maintained the same registration.
Currently all the code is in libslp to allow for asynchronous operation except for the calls to pthread_create(). The reason for this is mainly that no one has really needed asynchronous operation. If you feel like you have a good reason to use asynchronous operation then please send email to firstname.lastname@example.org.
How does OpenSLP library handle synchronous operation?When an SLP library call is made with an SLPHandle that was opened in sync mode, the library will not create a thread. Instead, the calling thread will perform all processing (which may block) and report results to the callback function. When in sync mode, all of the results are collated to ensure no duplicates are returned. The API function call will not return until all results are finished being reported through the callback.
Why not just have separate synchronous and asynchronous APIs?That would have been good choice, but for some reason, the SLP designers thought their way would be better. OpenSLP API is just an implementation of a standardized specification described in RFC 2614
Can I see some example code?Yes, example code can be found in the documentation for the SLPReg(), SLPFindSrv(), SLPFindAttrs() and SLPFindSrvTypes() functions.