diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-25 06:51:43 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-25 06:51:43 +0000 |
| commit | c53dad98839f5139dcedc128d2545dd2542a7157 (patch) | |
| tree | f50f3aaae16eb8f7001037b1d70b0b7b5f7ee0b6 /doc/callback.html | |
| parent | 5c13076f8936ae6face0779f75c7e6f148e4989e (diff) | |
| download | luasocket-c53dad98839f5139dcedc128d2545dd2542a7157.tar.gz luasocket-c53dad98839f5139dcedc128d2545dd2542a7157.tar.bz2 luasocket-c53dad98839f5139dcedc128d2545dd2542a7157.zip | |
Starting the manual. Oh well.
Diffstat (limited to 'doc/callback.html')
| -rw-r--r-- | doc/callback.html | 348 |
1 files changed, 0 insertions, 348 deletions
diff --git a/doc/callback.html b/doc/callback.html deleted file mode 100644 index 98b4476..0000000 --- a/doc/callback.html +++ /dev/null | |||
| @@ -1,348 +0,0 @@ | |||
| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | ||
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> | ||
| 3 | <html> | ||
| 4 | |||
| 5 | <head> | ||
| 6 | <title>LuaSocket: Network support for the Lua language</title> | ||
| 7 | <link rel="stylesheet" href="reference.css" type="text/css"> | ||
| 8 | </head> | ||
| 9 | |||
| 10 | <body> | ||
| 11 | |||
| 12 | <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 13 | |||
| 14 | <div class=header> | ||
| 15 | <hr> | ||
| 16 | <center> | ||
| 17 | <table summary="LuaSocket logo"> | ||
| 18 | <tr><td align=center><a href="http://www.lua.org"> | ||
| 19 | <img border=0 alt="LuaSocket" src="luasocket.png"> | ||
| 20 | </a></td></tr> | ||
| 21 | <tr><td align=center valign=top>Network support for the Lua language | ||
| 22 | </td></tr> | ||
| 23 | </table> | ||
| 24 | <p class=bar> | ||
| 25 | <a href="home.html">home</a> · | ||
| 26 | <a href="home.html#download">download</a> · | ||
| 27 | <a href="introduction.html">introduction</a> · | ||
| 28 | <a href="reference.html">reference</a> | ||
| 29 | </p> | ||
| 30 | </center> | ||
| 31 | <hr> | ||
| 32 | </div> | ||
| 33 | |||
| 34 | <!-- stream ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 35 | |||
| 36 | <h2 id=stream>Callbacks</h2> | ||
| 37 | |||
| 38 | <p> | ||
| 39 | HTTP, FTP, and SMTP transfers sometimes involve large amounts of | ||
| 40 | information. Sometimes an application needs to generate outgoing data | ||
| 41 | in real time, or needs to process incoming information as it is being | ||
| 42 | received. To address these problems, LuaSocket allows HTTP and SMTP message | ||
| 43 | bodies and FTP file contents to be streamed through the | ||
| 44 | callback mechanism outlined below. | ||
| 45 | </p> | ||
| 46 | |||
| 47 | <p> | ||
| 48 | Instead of returning the received contents | ||
| 49 | as a big to the Lua application, the library allows the user to | ||
| 50 | provide a <em>receive callback</em> that will be called with successive | ||
| 51 | chunks of data, as the data becomes available. Conversely, the <em>send | ||
| 52 | callback</em> mechanism can be used when the application wants to incrementally provide LuaSocket with the data to be sent. | ||
| 53 | </p> | ||
| 54 | |||
| 55 | <!-- receive +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 56 | |||
| 57 | <p class=name id=receive> | ||
| 58 | <b>receive_cb(</b>chunk, err<b>)</b> | ||
| 59 | </p> | ||
| 60 | |||
| 61 | <p class=description> | ||
| 62 | A receive callback will be repeatedly called by | ||
| 63 | LuaSocket whenever new data is available. Each time it is called, the | ||
| 64 | callback receives successive chunks of downloaded data. | ||
| 65 | </p> | ||
| 66 | |||
| 67 | <p class=parameters> | ||
| 68 | <tt>Chunk</tt> contains the latest downloaded chunk of data. | ||
| 69 | When the transmission is over, the function is called with an | ||
| 70 | empty string (i.e. <tt>""</tt>) in <tt>chunk</tt>. | ||
| 71 | If an error occurs, the function receives a <b><tt>nil</tt></b> | ||
| 72 | <tt>chunk</tt> and an error message in <tt>err</tt>. | ||
| 73 | </p> | ||
| 74 | |||
| 75 | <p class=return> | ||
| 76 | The callback can abort transmission by returning <b><tt>nil</tt></b> as its first | ||
| 77 | return value, and an optional error message as the | ||
| 78 | second return value. To continue receiving | ||
| 79 | data, the function should return non-<b><tt>nil</tt></b> as its first return | ||
| 80 | value. Optionally, in this case, it may return a | ||
| 81 | second return value, with a new callback function to take its place. | ||
| 82 | </p> | ||
| 83 | |||
| 84 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 85 | |||
| 86 | <p class=name id=send> | ||
| 87 | <b>send_cb()</b> | ||
| 88 | </p> | ||
| 89 | |||
| 90 | <p class=description> | ||
| 91 | A send callback will be called whenever LuaSocket | ||
| 92 | needs more data to be sent. | ||
| 93 | </p> | ||
| 94 | |||
| 95 | <p class=return> | ||
| 96 | Each time the callback is called, it should return the next chunk of data. It | ||
| 97 | can optionally return, as it's second return value, a new callback to replace | ||
| 98 | itself. The callback can abort the process at any time by returning | ||
| 99 | <b><tt>nil</tt></b> followed by an optional error message. | ||
| 100 | </p> | ||
| 101 | |||
| 102 | <!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 103 | |||
| 104 | <h2 id=predefined>Predefined Callbacks</h2> | ||
| 105 | |||
| 106 | <p> | ||
| 107 | The Callback module provides several predefined callbacks to | ||
| 108 | perform the most common input/output operations. Callbacks are provided | ||
| 109 | that send and receive contents of files and strings. Furthermore, | ||
| 110 | composition functions are provided to chain callbacks with filters, such as | ||
| 111 | the filters defined in the <a href=mime.html>MIME</a> module. | ||
| 112 | Together, these two modules provide a powerful interface to send and | ||
| 113 | receive information. | ||
| 114 | </p> | ||
| 115 | |||
| 116 | <!-- done +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 117 | |||
| 118 | <p class=name id=done> | ||
| 119 | socket.callback.<b>done()</b> | ||
| 120 | </p> | ||
| 121 | |||
| 122 | <p class=description> | ||
| 123 | This function creates and returns a callback that successfully terminates | ||
| 124 | the transmission. | ||
| 125 | </p> | ||
| 126 | |||
| 127 | <!-- fail +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 128 | |||
| 129 | <p class=name id=fail> | ||
| 130 | socket.callback.<b>fail(</b>message<b>)</b> | ||
| 131 | </p> | ||
| 132 | |||
| 133 | <p class=description> | ||
| 134 | This function creates and returns a callback that aborts the | ||
| 135 | transmission with a given error <tt>message</tt>. | ||
| 136 | </p> | ||
| 137 | |||
| 138 | <!-- receive.concat +++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 139 | |||
| 140 | <p class=name id=receive.concat> | ||
| 141 | socket.callback.<b>receive.concat(</b>cat<b>)</b> | ||
| 142 | </p> | ||
| 143 | |||
| 144 | <p class=description> | ||
| 145 | This function creates a receive callback that stores whatever it receives | ||
| 146 | into a concat object. When done, the application can get the contents | ||
| 147 | received as a single string, directly from the concat object. | ||
| 148 | </p> | ||
| 149 | |||
| 150 | <p class=parameters> | ||
| 151 | <tt>Cat</tt> is the target concat object, or <b><tt>nil</tt></b>. | ||
| 152 | If <tt>cat</tt> is <b><tt>nil</tt></b>, the function creates its own | ||
| 153 | concat object. | ||
| 154 | </p> | ||
| 155 | |||
| 156 | <p class=return> | ||
| 157 | The function returns a receive callback for the file, and the concat object | ||
| 158 | that will be used to store the received contents. | ||
| 159 | </p> | ||
| 160 | |||
| 161 | <!-- receive.file +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 162 | |||
| 163 | <p class=name id=receive.file> | ||
| 164 | socket.callback.<b>receive.file(</b>file, io_err<b>)</b> | ||
| 165 | </p> | ||
| 166 | |||
| 167 | <p class=description> | ||
| 168 | This function creates a receive callback that stores whatever it receives | ||
| 169 | into a file. When done, the callback closes the file. | ||
| 170 | </p> | ||
| 171 | |||
| 172 | <p class=parameters> | ||
| 173 | <tt>File</tt> is a file handle opened for writing. If <tt>file</tt> is | ||
| 174 | <b><tt>nil</tt></b>, <tt>io_err</tt> can contain an error message. In this | ||
| 175 | case, the function returns a callback that just aborts | ||
| 176 | transmission with the error message. | ||
| 177 | </p> | ||
| 178 | |||
| 179 | <p class=return> | ||
| 180 | The function returns a receive callback for the file. | ||
| 181 | </p> | ||
| 182 | |||
| 183 | <p class=note> | ||
| 184 | Note: This function is designed so that it directly accepts the return | ||
| 185 | values of Lua's IO <tt>io.open</tt> library function. | ||
| 186 | </p> | ||
| 187 | |||
| 188 | <!-- receive.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 189 | |||
| 190 | <p class=name id=receive.chain> | ||
| 191 | socket.callback.<b>receive.chain(</b>filter, receive_cb<b>)</b> | ||
| 192 | </p> | ||
| 193 | |||
| 194 | <p class=description> | ||
| 195 | This function creates a receive callback that passes all received data | ||
| 196 | through a filter, before handing it to a given receive callback. | ||
| 197 | </p> | ||
| 198 | |||
| 199 | <p class=parameters> | ||
| 200 | <tt>Cat</tt> is the target concat object, or <b><tt>nil</tt></b>. | ||
| 201 | If <tt>cat</tt> is <b><tt>nil</tt></b>, the function creates its own | ||
| 202 | concat object. | ||
| 203 | </p> | ||
| 204 | |||
| 205 | <p class=return> | ||
| 206 | The function returns a receive callback for the file, and the concat object | ||
| 207 | that will be used to store the received contents. | ||
| 208 | </p> | ||
| 209 | |||
| 210 | <p class=note> | ||
| 211 | Note: Several filters are defined in the <a href=mime.html>MIME</a> | ||
| 212 | module. Below is an example that creates a receive callback that | ||
| 213 | creates a string from the received contents, after decoding the | ||
| 214 | Quoted-Printable transfer content encoding. | ||
| 215 | </p> | ||
| 216 | |||
| 217 | <pre class=example> | ||
| 218 | string_cb, concat = socket.callback.receive.concat() | ||
| 219 | receive_cb = socket.callback.receive.chain( | ||
| 220 | socket.mime.decode("quoted-printable"), | ||
| 221 | string_cb | ||
| 222 | ) | ||
| 223 | </pre> | ||
| 224 | |||
| 225 | <p class=note> | ||
| 226 | The call to <tt>callback.chain</tt> creates a chained | ||
| 227 | receive callback that decodes data using the | ||
| 228 | <tt><a href=mime.html#decode>mime.decode</a></tt> | ||
| 229 | Quoted-Printable MIME filter and | ||
| 230 | hands the decoded data to a concat receive callback. | ||
| 231 | The concatenated decoded data can be retrieved later | ||
| 232 | from the associated concat object. | ||
| 233 | </p> | ||
| 234 | |||
| 235 | <!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 236 | |||
| 237 | <p class=name id=send.file> | ||
| 238 | socket.callback.<b>send.file(</b>file, io_err<b>)</b> | ||
| 239 | </p> | ||
| 240 | |||
| 241 | <p class=description> | ||
| 242 | This function creates a send callback that returns the contents | ||
| 243 | of a file, chunk by chunk, until the file has been entirely sent. | ||
| 244 | When done, the callback closes the file. | ||
| 245 | </p> | ||
| 246 | |||
| 247 | <p class=parameters> | ||
| 248 | <tt>File</tt> is a file handle opened for reading. If <tt>file</tt> is | ||
| 249 | <b><tt>nil</tt></b>, <tt>io_err</tt> can contain an error message. In this | ||
| 250 | case, the function returns a callback that just aborts | ||
| 251 | transmission with the error message. | ||
| 252 | </p> | ||
| 253 | |||
| 254 | <p class=return> | ||
| 255 | The function returns a send callback for the file. | ||
| 256 | </p> | ||
| 257 | |||
| 258 | <p class=note> | ||
| 259 | Note: This function is designed so that it directly accepts the return | ||
| 260 | values of Lua's IO <tt>io.open</tt> library function. | ||
| 261 | </p> | ||
| 262 | |||
| 263 | <!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 264 | |||
| 265 | <p class=name id=send.string> | ||
| 266 | socket.callback.<b>send.string(</b>str, err<b>)</b> | ||
| 267 | </p> | ||
| 268 | |||
| 269 | <p class=description> | ||
| 270 | This function creates a send callback that will send | ||
| 271 | the contents of a string. | ||
| 272 | </p> | ||
| 273 | |||
| 274 | <p class=parameters> | ||
| 275 | <tt>Str</tt> is the string to be sent. | ||
| 276 | </p> | ||
| 277 | |||
| 278 | <p class=return> | ||
| 279 | It returns a send callback for the string, | ||
| 280 | or <b><tt>nil</tt></b> if <tt>str</tt> is <b><tt>nil</tt></b>. | ||
| 281 | </p> | ||
| 282 | |||
| 283 | <!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 284 | |||
| 285 | <p class=name id=send.chain> | ||
| 286 | socket.callback.<b>send.chain(</b>send_cb, filter<b>)</b> | ||
| 287 | </p> | ||
| 288 | |||
| 289 | <p class=description> | ||
| 290 | This function creates a send callback that will filter | ||
| 291 | all the data it receives from another send callback, before | ||
| 292 | sending it through. | ||
| 293 | </p> | ||
| 294 | |||
| 295 | <p class=parameters> | ||
| 296 | <tt>Send_cb</tt> is the send callback to be chained to <tt>filter</tt>. | ||
| 297 | </p> | ||
| 298 | |||
| 299 | <p class=return> | ||
| 300 | Returns a callback chained with the filter. | ||
| 301 | </p> | ||
| 302 | |||
| 303 | <p class=note> | ||
| 304 | Note: Several filters are defined in the <a href=mime.html>MIME</a> | ||
| 305 | module. Below is an example that creates a send callback that sends | ||
| 306 | a file's contents encoded in the Base64 transfer content encoding. | ||
| 307 | </p> | ||
| 308 | |||
| 309 | <pre class=example> | ||
| 310 | send_cb = socket.callback.send.chain( | ||
| 311 | socket.callback.send.file(io.open("input.bin")) | ||
| 312 | socket.mime.chain( | ||
| 313 | socket.mime.encode("base64"), | ||
| 314 | socket.mime.wrap("base64") | ||
| 315 | ) | ||
| 316 | ) | ||
| 317 | </pre> | ||
| 318 | |||
| 319 | <p class=note> | ||
| 320 | The call to <a href=mime.html#chain><tt>mime.chain</tt></a> | ||
| 321 | creates a chained filter that encodes it's input and then breaks it | ||
| 322 | into lines. The call to <tt>callback.chain</tt> creates a chained | ||
| 323 | send callback that reads the file from disk and passes it through the | ||
| 324 | filter before sending it. | ||
| 325 | </p> | ||
| 326 | |||
| 327 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 328 | |||
| 329 | <div class=footer> | ||
| 330 | <hr> | ||
| 331 | <center> | ||
| 332 | <p class=bar> | ||
| 333 | <a href="home.html">home</a> · | ||
| 334 | <a href="home.html#down">download</a> · | ||
| 335 | <a href="introduction.html">introduction</a> · | ||
| 336 | <a href="reference.html">reference</a> | ||
| 337 | </p> | ||
| 338 | <p> | ||
| 339 | <small> | ||
| 340 | Last modified by Diego Nehab on <br> | ||
| 341 | Sat Aug 9 01:00:41 PDT 2003 | ||
| 342 | </small> | ||
| 343 | </p> | ||
| 344 | </center> | ||
| 345 | </div> | ||
| 346 | |||
| 347 | </body> | ||
| 348 | </html> | ||
