diff options
Diffstat (limited to 'doc/stream.html')
| -rw-r--r-- | doc/stream.html | 89 |
1 files changed, 41 insertions, 48 deletions
diff --git a/doc/stream.html b/doc/stream.html index 296ca2e..b88cbb5 100644 --- a/doc/stream.html +++ b/doc/stream.html | |||
| @@ -36,21 +36,21 @@ | |||
| 36 | <h2 id=stream>Streaming with Callbacks</h2> | 36 | <h2 id=stream>Streaming with Callbacks</h2> |
| 37 | 37 | ||
| 38 | <p> | 38 | <p> |
| 39 | HTTP and FTP transfers sometimes involve large amounts of information. | 39 | HTTP, FTP, and SMTP transfers sometimes involve large amounts of |
| 40 | Sometimes an application needs to generate outgoing data in real time, | 40 | information. Sometimes an application needs to generate outgoing data |
| 41 | or needs to process incoming information as it is being received. To | 41 | in real time, or needs to process incoming information as it is being |
| 42 | address these problems, LuaSocket allows HTTP message bodies and FTP | 42 | received. To address these problems, LuaSocket allows HTTP and SMTP message |
| 43 | file contents to be received or sent through the callback mechanism | 43 | bodies and FTP file contents to be received or sent through the |
| 44 | outlined below. | 44 | callback mechanism outlined below. |
| 45 | </p> | 45 | </p> |
| 46 | 46 | ||
| 47 | <p> | 47 | <p> |
| 48 | Instead of returning the entire contents of a FTP file or HTTP message | 48 | Instead of returning the entire contents of an entity |
| 49 | body as strings to the Lua application, the library allows the user to | 49 | 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 | 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 | 51 | chunks of data, as the data becomes available. Conversely, the <em>send |
| 52 | callbacks</em> should be used when data needed by LuaSocket | 52 | callbacks</em> can be used when the application wants to incrementally |
| 53 | is generated incrementally by the application. | 53 | provide LuaSocket with the data to be sent. |
| 54 | </p> | 54 | </p> |
| 55 | 55 | ||
| 56 | <!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 56 | <!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
| @@ -68,21 +68,26 @@ callback receives successive chunks of downloaded data. | |||
| 68 | <p class=parameters> | 68 | <p class=parameters> |
| 69 | <tt>Chunk</tt> contains the current chunk of data. | 69 | <tt>Chunk</tt> contains the current chunk of data. |
| 70 | When the transmission is over, the function is called with an | 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 | 71 | empty string (i.e. <tt>""</tt>) as the <tt>chunk</tt>. |
| 72 | function receives <tt>nil</tt> as <tt>chunk</tt> and an error message as | 72 | If an error occurs, the function receives <tt>nil</tt> |
| 73 | <tt>err</tt>. | 73 | as <tt>chunk</tt> and an error message in <tt>err</tt>. |
| 74 | </p> | 74 | </p> |
| 75 | 75 | ||
| 76 | <p class=return> | 76 | <p class=return> |
| 77 | The callback can abort transmission by returning | 77 | The callback can abort transmission by returning <tt>nil</tt> as its first |
| 78 | <tt>nil</tt> as its first return value. In that case, it can also return | 78 | return value, and an optional error message as the |
| 79 | an error message. Any non-<tt>nil</tt> return value proceeds with the | 79 | second return value. If the application wants to continue receiving |
| 80 | transmission. | 80 | data, the function should return non-<tt>nil</tt> as it's first return |
| 81 | value. In this case, the function can optionally return a | ||
| 82 | new callback function, to replace itself, as the second return value. | ||
| 83 | </p> | ||
| 84 | |||
| 85 | <p class=note> | ||
| 86 | Note: The <tt>callback</tt> module provides several standard receive callbacks, including the following: | ||
| 81 | </p> | 87 | </p> |
| 82 | 88 | ||
| 83 | <pre class=example> | 89 | <pre class=example> |
| 84 | -- The implementation of socket.callback.receive_concat | 90 | function receive.concat(concat) |
| 85 | function Public.receive_concat(concat) | ||
| 86 | concat = concat or socket.concat.create() | 91 | concat = concat or socket.concat.create() |
| 87 | local callback = function(chunk, err) | 92 | local callback = function(chunk, err) |
| 88 | -- if not finished, add chunk | 93 | -- if not finished, add chunk |
| @@ -95,6 +100,12 @@ function Public.receive_concat(concat) | |||
| 95 | end | 100 | end |
| 96 | </pre> | 101 | </pre> |
| 97 | 102 | ||
| 103 | <p class=note> | ||
| 104 | This function creates a new receive callback that concatenates all | ||
| 105 | received chunks into a the same concat object, which can later be | ||
| 106 | queried for its contents. | ||
| 107 | </p> | ||
| 108 | |||
| 98 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 109 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
| 99 | 110 | ||
| 100 | <p class=name> | 111 | <p class=name> |
| @@ -107,45 +118,27 @@ library needs more data to be sent. | |||
| 107 | </p> | 118 | </p> |
| 108 | 119 | ||
| 109 | <p class=return> | 120 | <p class=return> |
| 110 | Each time the callback is called, it | 121 | Each time the callback is called, it should return the next chunk of data. It |
| 111 | should return the next part of the information the library is expecting, | 122 | can optionally return, as it's second return value, a new callback to replace |
| 112 | followed by the total number of bytes to be sent. | 123 | itself. The callback can abort the process at any time by returning |
| 113 | The callback can abort | 124 | <tt>nil</tt> followed by an optional error message. |
| 114 | the process at any time by returning <tt>nil</tt> followed by an | ||
| 115 | optional error message. | ||
| 116 | </p> | 125 | </p> |
| 117 | 126 | ||
| 118 | |||
| 119 | <p class=note> | 127 | <p class=note> |
| 120 | Note: The need for the second return value comes from the fact that, with | 128 | Note: Below is the implementation of the <tt>callback.send.file</tt> |
| 121 | the HTTP protocol for instance, the library needs to know in advance the | 129 | function. Given an open file handle, it returns a send callback that will send the contents of that file, chunk by chunk. |
| 122 | total number of bytes that will be sent. | ||
| 123 | </p> | 130 | </p> |
| 124 | 131 | ||
| 125 | <pre class=example> | 132 | <pre class=example> |
| 126 | -- The implementation of socket.callback.send_file | 133 | function send.file(file, io_err) |
| 127 | function Public.send_file(file) | 134 | -- if successful, return the callback that reads from the file |
| 128 | local callback | ||
| 129 | -- if successfull, return the callback that reads from the file | ||
| 130 | if file then | 135 | if file then |
| 131 | -- get total size | 136 | return function() |
| 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 | -- send next block of data |
| 137 | local chunk = file:read(Public.BLOCKSIZE) | 138 | return (file:read(BLOCKSIZE)) or "" |
| 138 | if not chunk then file:close() end | ||
| 139 | return chunk, size | ||
| 140 | end | 139 | end |
| 141 | -- else, return a callback that just aborts the transfer | 140 | -- else, return a callback that just aborts the transfer |
| 142 | else | 141 | else return fail(io_err or "unable to open file") end |
| 143 | callback = function() | ||
| 144 | -- just abort | ||
| 145 | return nil, "unable to open file" | ||
| 146 | end | ||
| 147 | end | ||
| 148 | return callback, file | ||
| 149 | end | 142 | end |
| 150 | </pre> | 143 | </pre> |
| 151 | 144 | ||
