📄 callbacks.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Mozilla/4.77C-CCK-MCD Caldera Systems OpenLinux [en] (X11; U; Linux 2.4.2 i686) [Netscape]"> <title>OpenSLP Programmers Guide - Callbacks</title></head><body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000"><h2>Callbacks</h2><hr WIDTH="100%"><h3>What's a callback function?</h3>If you are new to asynchronous programming, or have never used callbackfunctions before, just think of the SLP callback functions as a piecesof 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 statusor results of an operation. This allows your program to do otherthings while data is being collected by the callback function. Callbackfunctions are required for all of the major SLP APIs for more informationsee <a href="SLPReg.html">SLPReg()</a>,<a href="SLPDereg.html">SLPDeReg()</a>,<a href="SLPDelAttrs.html">SLPDelAttrs()</a>,<a href="SLPFindSrvs.html">SLPFindSrvs()</a>,<a href="SLPFindAttrs.html">SLPFindAttrs()</a>,and <a href="SLPFindSrvTypes.html">SLPFindSrvTypes()</a>,<p>Callback functions must accept the parameters that the caller (the SLPlibrary) expects to pass to them. This is why callback function typesare defined. See <a href="SLPRegReport.html">SLPRegReport()</a>,<a href="SLPSrvURLCallback.html">SLPSrvURLCallback()</a>,<a href="SLPAttrCallback.html">SLPAttrCallback()</a>.<h3>What's different about SLP callback functions?</h3>Callbacks are an integral part of the SLP API. Developers usuallyassociate callbacks with asynchronous APIs, but the SLP API uses callbacksfor both synchronous and asynchronous operations. Whether the callbackis called synchronously or asynchronously, depends on the <tt><a href="SLPOpen.html#isasync">isasync</a></tt>parameter in the call to <tt><a href="SLPOpen.html">SLPOpen()</a></tt>. Remember the following rules and you should not have any problems withyour callback functions.<ul><li>Callback functions are called in both synchronous and asynchronous cases. The only difference is that in a synchronous case, the initiating function(SLPReg(), SLPFindSrvs(), etc) will block until all results are reportedto the callback function.</li><li>The memory passed in to callback functions is owned by the library. i.e. the callback must <tt>strdup()</tt> strings before using them permanentlybecause the memory passed in will be <tt>free()</tt>d by the library whenthe callback returns.</li><li><blink>Make your callback functions as efficient as possible.</blink> This is especially important when a call is made with an async <tt><a href="SLPTypes.html#SLPHandle">SLPHandle</a></tt>because results are not collected or collated by the library before thecallback function is called. In other words, in async mode,the library will call the callback each time a reply message is receiveduntil the request times out.</li><li>If the <tt>errcode</tt> upon entry to the callback is set to anything but<tt>SLP_OK</tt>,the rest of the parameters may be invalid. Check the error code first.</li><li>Use the <tt>cookie</tt> parameter. It is the best way to get informationto and from your callback.</li></ul><h3>How does OpenSLP library handle asynchronous operation?</h3>When an SLP library call is made with an SLPHandle that was opened in asyncmode, the library does everything it can with out blocking. It thencreates a thread (hopefully a user level thread) and returns SLP_OK. The newly created thread processes the request (possibly blocking to waitfor data to arrive from the network) and calls the callback function asdata is received.<p>An important thing to remember is that <i>no collection or collationof results is performed by the library when a call is initiated in asyncmode. </i>This means that the callback may be called multiple times withthe same result. This would happen for example if two SAs or DAsmaintained the same registration.<p>Currently all the code is in libslp to allow for asynchronous operationexcept for the calls to pthread_create(). The reason for this ismainly that no one has really needed asynchronous operation. If youfeel like you have a good reason to use asynchronous operation then pleasesend email to openslp-devel@lists.sourceforge.net.<h3>How does OpenSLP library handle synchronous operation?</h3>When an SLP library call is made with an SLPHandle that was opened in syncmode, the library will not create a thread. Instead, the callingthread will perform all processing (which may block) and report resultsto the callback function. When in sync mode, all of the results arecollated to ensure no duplicates are returned. The API function callwill not return until all results are finished being reported through thecallback.<h3>Why not just have separate synchronous and asynchronous APIs?</h3>That would have been good choice, but for some reason, the SLP designersthought their way would be better. OpenSLP API is just an implementationof a standardized specification described in RFC 2614<h3>Can I see some example code?</h3>Yes, example code can be found in the documentation for the <a href="SLPReg.html">SLPReg()</a>,<a href="SLPFindSrvs.html">SLPFindSrv()</a>,<a href="SLPFindAttrs.html">SLPFindAttrs()</a> and <a href="SLPFindSrvTypes.html">SLPFindSrvTypes()</a>functions.</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -