diff options
Diffstat (limited to 'doc/callback.html')
-rw-r--r-- | doc/callback.html | 160 |
1 files changed, 73 insertions, 87 deletions
diff --git a/doc/callback.html b/doc/callback.html index 2c7ecd5..94af8ff 100644 --- a/doc/callback.html +++ b/doc/callback.html | |||
@@ -45,113 +45,72 @@ callback mechanism outlined below. | |||
45 | </p> | 45 | </p> |
46 | 46 | ||
47 | <p> | 47 | <p> |
48 | Instead of returning the entire contents of an entity | 48 | Instead of returning the received contents |
49 | as strings to the Lua application, the library allows the user to | 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 | 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> can be used when the application wants to incrementally | 52 | callback</em> mechanism can be used when the application wants to incrementally provide LuaSocket with the data to be sent. |
53 | provide LuaSocket with the data to be sent. | ||
54 | </p> | 53 | </p> |
55 | 54 | ||
56 | <!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 55 | <!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
57 | 56 | ||
58 | <p class=name id=receive_cb> | 57 | <p class=name id=receive> |
59 | ret, err_or_f = <b>receive_cb(</b>chunk, err<b>)</b> | 58 | <b>receive_cb(</b>chunk, err<b>)</b> |
60 | </p> | 59 | </p> |
61 | 60 | ||
62 | <p class=description> | 61 | <p class=description> |
63 | The callback provided by the user will be repeatedly called by the | 62 | A receive callback will be repeatedly called by |
64 | library whenever new data is available. Each time it is called, the | 63 | LuaSocket wheneve new data is available. Each time it is called, the |
65 | callback receives successive chunks of downloaded data. | 64 | callback receives successive chunks of downloaded data. |
66 | </p> | 65 | </p> |
67 | 66 | ||
68 | <p class=parameters> | 67 | <p class=parameters> |
69 | <tt>Chunk</tt> contains the current chunk of data. | 68 | <tt>Chunk</tt> contains the latest downloaded chunk of data. |
70 | When the transmission is over, the function is called with an | 69 | When the transmission is over, the function is called with an |
71 | empty string (i.e. <tt>""</tt>) as the <tt>chunk</tt>. | 70 | empty string (i.e. <tt>""</tt>) in <tt>chunk</tt>. |
72 | If an error occurs, the function receives <tt>nil</tt> | 71 | If an error occurs, the function receives a <b><tt>nil</tt></b> |
73 | as <tt>chunk</tt> and an error message in <tt>err</tt>. | 72 | <tt>chunk</tt> and an error message in <tt>err</tt>. |
74 | </p> | 73 | </p> |
75 | 74 | ||
76 | <p class=return> | 75 | <p class=return> |
77 | The callback can abort transmission by returning <tt>nil</tt> as its first | 76 | The callback can abort transmission by returning <b><tt>nil</tt></b> as its first |
78 | return value, and an optional error message as the | 77 | return value, and an optional error message as the |
79 | second return value. If the application wants to continue receiving | 78 | second return value. To continue receiving |
80 | data, the function should return non-<tt>nil</tt> as it's first return | 79 | data, the function should return non-<b><tt>nil</tt></b> as its first return |
81 | value. In this case, the function can optionally return a | 80 | value. Optionally, in this case, it may return a |
82 | new callback function, to replace itself, as the second return value. | 81 | second return value, with a new callback function to take its place. |
83 | </p> | ||
84 | |||
85 | <p class=note> | ||
86 | Note: The <tt>callback</tt> module provides several standard receive callbacks, including the following: | ||
87 | </p> | ||
88 | |||
89 | <pre class=example> | ||
90 | function receive.concat(concat) | ||
91 | concat = concat or socket.concat.create() | ||
92 | local callback = function(chunk, err) | ||
93 | -- if not finished, add chunk | ||
94 | if chunk and chunk ~= "" then | ||
95 | concat:addstring(chunk) | ||
96 | return 1 | ||
97 | end | ||
98 | end | ||
99 | return callback, concat | ||
100 | end | ||
101 | </pre> | ||
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> | 82 | </p> |
108 | 83 | ||
109 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 84 | <!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
110 | 85 | ||
111 | <p class=name> | 86 | <p class=name id=send> |
112 | chunk, err_or_r = <b>send_cb()</b> | 87 | <b>send_cb()</b> |
113 | </p> | 88 | </p> |
114 | 89 | ||
115 | <p class=description> | 90 | <p class=description> |
116 | The callback provided by the user will be repeatedly called whenever the | 91 | A send callback will be called whenever LuaSocket |
117 | library needs more data to be sent. | 92 | needs more data to be sent. |
118 | </p> | 93 | </p> |
119 | 94 | ||
120 | <p class=return> | 95 | <p class=return> |
121 | Each time the callback is called, it should return the next chunk of data. It | 96 | Each time the callback is called, it should return the next chunk of data. It |
122 | can optionally return, as it's second return value, a new callback to replace | 97 | can optionally return, as it's second return value, a new callback to replace |
123 | itself. The callback can abort the process at any time by returning | 98 | itself. The callback can abort the process at any time by returning |
124 | <tt>nil</tt> followed by an optional error message. | 99 | <b><tt>nil</tt></b> followed by an optional error message. |
125 | </p> | ||
126 | |||
127 | <p class=note> | ||
128 | Note: Below is the implementation of the <tt>callback.send.file</tt> | ||
129 | function. Given an open file handle, it returns a send callback that will send the contents of that file, chunk by chunk. | ||
130 | </p> | 100 | </p> |
131 | 101 | ||
132 | <pre class=example> | ||
133 | function send.file(file, io_err) | ||
134 | -- if successful, return the callback that reads from the file | ||
135 | if file then | ||
136 | return function() | ||
137 | -- send next block of data | ||
138 | return (file:read(BLOCKSIZE)) or "" | ||
139 | end | ||
140 | -- else, return a callback that just aborts the transfer | ||
141 | else return fail(io_err or "unable to open file") end | ||
142 | end | ||
143 | </pre> | ||
144 | |||
145 | <!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 102 | <!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
146 | 103 | ||
147 | <h2 id=predefined>Predefined Callbacks</h2> | 104 | <h2 id=predefined>Predefined Callbacks</h2> |
148 | 105 | ||
149 | <p> | 106 | <p> |
150 | The module <tt>callback.lua</tt> provides several predefined callbacks to | 107 | The Callback module provides several predefined callbacks to |
151 | perform the most common input/output operations. Callbacks are provided | 108 | perform the most common input/output operations. Callbacks are provided |
152 | that send and receive contents of files and strings. Furthermore, | 109 | that send and receive contents of files and strings. Furthermore, |
153 | composition functions are provided to chain callbacks with filters, such as | 110 | composition functions are provided to chain callbacks with filters, such as |
154 | the filters defined in the <tt>mime.lua</tt> module. | 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. | ||
155 | </p> | 114 | </p> |
156 | 115 | ||
157 | <!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 116 | <!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
@@ -161,15 +120,16 @@ the filters defined in the <tt>mime.lua</tt> module. | |||
161 | </p> | 120 | </p> |
162 | 121 | ||
163 | <p class=description> | 122 | <p class=description> |
164 | This function creates a send callback that will return the contents | 123 | This function creates a send callback that returns the contents |
165 | of a the file, until the file has been entirely sent. When done, the | 124 | of a file, chunk by chunk, until the file has been entirely sent. |
166 | callback closes the file. | 125 | When done, the callback closes the file. |
167 | </p> | 126 | </p> |
168 | 127 | ||
169 | <p class=parameters> | 128 | <p class=parameters> |
170 | <tt>File</tt> is a file open for reading. If <tt>file</tt> is | 129 | <tt>File</tt> is a file opened for reading. If <tt>file</tt> is |
171 | <tt>nil</tt>, <tt>io_err</tt> can contain an error message and the | 130 | <b><tt>nil</tt></b>, <tt>io_err</tt> can contain an error message. In this |
172 | returned callback just aborts transmission with the error message. | 131 | case, the function returns a callback that just aborts |
132 | transmission with the error message. | ||
173 | </p> | 133 | </p> |
174 | 134 | ||
175 | <p class=return> | 135 | <p class=return> |
@@ -177,14 +137,14 @@ Returns a send callback for the file. | |||
177 | </p> | 137 | </p> |
178 | 138 | ||
179 | <p class=note> | 139 | <p class=note> |
180 | Note: The function is designed so that it directly accepts the return | 140 | Note: This function is designed so that it directly accepts the return |
181 | values of the <tt>io.open</tt> function. | 141 | values of the <tt>io.open</tt> function. |
182 | </p> | 142 | </p> |
183 | 143 | ||
184 | <!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 144 | <!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
185 | 145 | ||
186 | <p class=name id=send.string> | 146 | <p class=name id=send.string> |
187 | <b>send.string(</b>str<b>)</b> | 147 | <b>send.string(</b>str, err<b>)</b> |
188 | </p> | 148 | </p> |
189 | 149 | ||
190 | <p class=description> | 150 | <p class=description> |
@@ -193,29 +153,33 @@ the contents of a string. | |||
193 | </p> | 153 | </p> |
194 | 154 | ||
195 | <p class=parameters> | 155 | <p class=parameters> |
196 | <tt>Str</tt> is the string to be sent. | 156 | <tt>Str</tt> is the string to be sent. |
157 | <!-- | ||
158 | If <tt>str</tt> is | ||
159 | <b><tt>nil</tt></b>, <tt>err</tt> can optionally contain an error message. | ||
160 | --> | ||
197 | </p> | 161 | </p> |
198 | 162 | ||
199 | <p class=return> | 163 | <p class=return> |
200 | Returns a send callback for the string, or <tt>nil</tt> if the string is | 164 | Returns a send callback for the string, or <b><tt>nil</tt></b> if the string is |
201 | <tt>nil</tt>. | 165 | <b><tt>nil</tt></b>. |
202 | </p> | 166 | </p> |
203 | 167 | ||
204 | <p class=note> | 168 | <p class=note> |
205 | Note: If any function in the LuaSocket API receives a <tt>nil</tt> | 169 | Note: A <tt>nil</tt></b> |
206 | send callback, it assumes there is nothing to be sent. | 170 | send callback is equivalent to a callback that returns the empty string. |
207 | </p> | 171 | </p> |
208 | 172 | ||
209 | <!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 173 | <!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
210 | 174 | ||
211 | <p class=name id=send.string> | 175 | <p class=name id=send.chain> |
212 | <b>send.chain(</b>send_cb, filter<b>)</b> | 176 | <b>send.chain(</b>send_cb, filter<b>)</b> |
213 | </p> | 177 | </p> |
214 | 178 | ||
215 | <p class=description> | 179 | <p class=description> |
216 | This function creates a send callback that will send | 180 | This function creates a send callback that will filter |
217 | all data that it gets from another callback, | 181 | all the data it receives from another send callback, before |
218 | after passing the data through a filter. | 182 | sending it through. |
219 | </p> | 183 | </p> |
220 | 184 | ||
221 | <p class=parameters> | 185 | <p class=parameters> |
@@ -226,7 +190,29 @@ after passing the data through a filter. | |||
226 | Returns a callback chained with the filter. | 190 | Returns a callback chained with the filter. |
227 | </p> | 191 | </p> |
228 | 192 | ||
229 | (write a note!) | 193 | <p class=note> |
194 | Note: Several filters are defined in the <a href=mime.html>MIME</a> | ||
195 | module. Below is an example that creates a send callback that sends | ||
196 | a file's contents encoded in the Base64 transfer content encoding. | ||
197 | </p> | ||
198 | |||
199 | <pre class=example> | ||
200 | send_cb = socket.callback.send.chain( | ||
201 | socket.callback.send.file(io.open("input.bin")) | ||
202 | socket.mime.chain( | ||
203 | socket.mime.encode("base64"), | ||
204 | socket.mime.wrap("base64") | ||
205 | ) | ||
206 | ) | ||
207 | </pre> | ||
208 | |||
209 | <p class=note> | ||
210 | The call to <a href=mime.html#chain><tt>socket.mime.chain</tt></a> | ||
211 | creates a chained filter that encodes it's input and then breaks it | ||
212 | into lines. The call to <tt>socket.callback.chain</tt> creates a chained | ||
213 | send callback that reads the file from disk and passes it through the | ||
214 | filter before sending it. | ||
215 | </p> | ||
230 | 216 | ||
231 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 217 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
232 | 218 | ||