diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
| commit | 982781f1464c9b7a8133130433f83dbf1f59a2c0 (patch) | |
| tree | 8f96f9e9fa1e6bef8b8356037986ddc18673cade /doc/stream.html | |
| parent | 6789b83ff5c15296267f880d3b98cf8a1800c30a (diff) | |
| download | luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.gz luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.bz2 luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.zip | |
LuaSocket 2.0 User's Manual.
Diffstat (limited to 'doc/stream.html')
| -rw-r--r-- | doc/stream.html | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/doc/stream.html b/doc/stream.html new file mode 100644 index 0000000..296ca2e --- /dev/null +++ b/doc/stream.html | |||
| @@ -0,0 +1,173 @@ | |||
| 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>Streaming with Callbacks</h2> | ||
| 37 | |||
| 38 | <p> | ||
| 39 | HTTP and FTP transfers sometimes involve large amounts of information. | ||
| 40 | Sometimes an application needs to generate outgoing data in real time, | ||
| 41 | or needs to process incoming information as it is being received. To | ||
| 42 | address these problems, LuaSocket allows HTTP message bodies and FTP | ||
| 43 | file contents to be received or sent through the callback mechanism | ||
| 44 | outlined below. | ||
| 45 | </p> | ||
| 46 | |||
| 47 | <p> | ||
| 48 | Instead of returning the entire contents of a FTP file or HTTP message | ||
| 49 | body as strings 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 | callbacks</em> should be used when data needed by LuaSocket | ||
| 53 | is generated incrementally by the application. | ||
| 54 | </p> | ||
| 55 | |||
| 56 | <!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 57 | |||
| 58 | <p class=name id=receive_cb> | ||
| 59 | <b>receive_cb(</b>chunk, err<b>)</b> | ||
| 60 | </p> | ||
| 61 | |||
| 62 | <p class=description> | ||
| 63 | The callback provided by the user will be repeatedly called by the | ||
| 64 | library whenever new data is available. Each time it is called, the | ||
| 65 | callback receives successive chunks of downloaded data. | ||
| 66 | </p> | ||
| 67 | |||
| 68 | <p class=parameters> | ||
| 69 | <tt>Chunk</tt> contains the current chunk of data. | ||
| 70 | When the transmission is over, the function is called with an | ||
| 71 | empty string (i.e. <tt>""</tt>) as the <tt>chunk</tt>. If an error occurs, the | ||
| 72 | function receives <tt>nil</tt> as <tt>chunk</tt> and an error message as | ||
| 73 | <tt>err</tt>. | ||
| 74 | </p> | ||
| 75 | |||
| 76 | <p class=return> | ||
| 77 | The callback can abort transmission by returning | ||
| 78 | <tt>nil</tt> as its first return value. In that case, it can also return | ||
| 79 | an error message. Any non-<tt>nil</tt> return value proceeds with the | ||
| 80 | transmission. | ||
| 81 | </p> | ||
| 82 | |||
| 83 | <pre class=example> | ||
| 84 | -- The implementation of socket.callback.receive_concat | ||
| 85 | function Public.receive_concat(concat) | ||
| 86 | concat = concat or socket.concat.create() | ||
| 87 | local callback = function(chunk, err) | ||
| 88 | -- if not finished, add chunk | ||
| 89 | if chunk and chunk ~= "" then | ||
| 90 | concat:addstring(chunk) | ||
| 91 | return 1 | ||
| 92 | end | ||
| 93 | end | ||
| 94 | return callback, concat | ||
| 95 | end | ||
| 96 | </pre> | ||
| 97 | |||
| 98 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 99 | |||
| 100 | <p class=name> | ||
| 101 | <b>send_cb()</b> | ||
| 102 | </p> | ||
| 103 | |||
| 104 | <p class=description> | ||
| 105 | The callback provided by the user will be repeatedly called whenever the | ||
| 106 | library needs more data to be sent. | ||
| 107 | </p> | ||
| 108 | |||
| 109 | <p class=return> | ||
| 110 | Each time the callback is called, it | ||
| 111 | should return the next part of the information the library is expecting, | ||
| 112 | followed by the total number of bytes to be sent. | ||
| 113 | The callback can abort | ||
| 114 | the process at any time by returning <tt>nil</tt> followed by an | ||
| 115 | optional error message. | ||
| 116 | </p> | ||
| 117 | |||
| 118 | |||
| 119 | <p class=note> | ||
| 120 | Note: The need for the second return value comes from the fact that, with | ||
| 121 | the HTTP protocol for instance, the library needs to know in advance the | ||
| 122 | total number of bytes that will be sent. | ||
| 123 | </p> | ||
| 124 | |||
| 125 | <pre class=example> | ||
| 126 | -- The implementation of socket.callback.send_file | ||
| 127 | function Public.send_file(file) | ||
| 128 | local callback | ||
| 129 | -- if successfull, return the callback that reads from the file | ||
| 130 | if file then | ||
| 131 | -- get total size | ||
| 132 | local size = file:seek("end") | ||
| 133 | -- go back to start of file | ||
| 134 | file:seek("set") | ||
| 135 | callback = function() | ||
| 136 | -- send next block of data | ||
| 137 | local chunk = file:read(Public.BLOCKSIZE) | ||
| 138 | if not chunk then file:close() end | ||
| 139 | return chunk, size | ||
| 140 | end | ||
| 141 | -- else, return a callback that just aborts the transfer | ||
| 142 | else | ||
| 143 | callback = function() | ||
| 144 | -- just abort | ||
| 145 | return nil, "unable to open file" | ||
| 146 | end | ||
| 147 | end | ||
| 148 | return callback, file | ||
| 149 | end | ||
| 150 | </pre> | ||
| 151 | |||
| 152 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 153 | |||
| 154 | <div class=footer> | ||
| 155 | <hr> | ||
| 156 | <center> | ||
| 157 | <p class=bar> | ||
| 158 | <a href="home.html">home</a> · | ||
| 159 | <a href="home.html#down">download</a> · | ||
| 160 | <a href="introduction.html">introduction</a> · | ||
| 161 | <a href="reference.html">reference</a> | ||
| 162 | </p> | ||
| 163 | <p> | ||
| 164 | <small> | ||
| 165 | Last modified by Diego Nehab on <br> | ||
| 166 | Sat Aug 9 01:00:41 PDT 2003 | ||
| 167 | </small> | ||
| 168 | </p> | ||
| 169 | </center> | ||
| 170 | </div> | ||
| 171 | |||
| 172 | </body> | ||
| 173 | </html> | ||
