aboutsummaryrefslogtreecommitdiff
path: root/doc/callback.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/callback.html')
-rw-r--r--doc/callback.html160
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>
48Instead of returning the entire contents of an entity 48Instead of returning the received contents
49as strings to the Lua application, the library allows the user to 49as a big to the Lua application, the library allows the user to
50provide a <em>receive callback</em> that will be called with successive 50provide a <em>receive callback</em> that will be called with successive
51chunks of data, as the data becomes available. Conversely, the <em>send 51chunks of data, as the data becomes available. Conversely, the <em>send
52callbacks</em> can be used when the application wants to incrementally 52callback</em> mechanism can be used when the application wants to incrementally provide LuaSocket with the data to be sent.
53provide 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>
59ret, 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>
63The callback provided by the user will be repeatedly called by the 62A receive callback will be repeatedly called by
64library whenever new data is available. Each time it is called, the 63LuaSocket wheneve new data is available. Each time it is called, the
65callback receives successive chunks of downloaded data. 64callback 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.
70When the transmission is over, the function is called with an 69When the transmission is over, the function is called with an
71empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>. 70empty string (i.e.&nbsp;<tt>""</tt>) in <tt>chunk</tt>.
72If an error occurs, the function receives <tt>nil</tt> 71If an error occurs, the function receives a <b><tt>nil</tt></b>
73as <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>
77The callback can abort transmission by returning <tt>nil</tt> as its first 76The callback can abort transmission by returning <b><tt>nil</tt></b> as its first
78return value, and an optional error message as the 77return value, and an optional error message as the
79second return value. If the application wants to continue receiving 78second return value. To continue receiving
80data, the function should return non-<tt>nil</tt> as it's first return 79data, the function should return non-<b><tt>nil</tt></b> as its first return
81value. In this case, the function can optionally return a 80value. Optionally, in this case, it may return a
82new callback function, to replace itself, as the second return value. 81second return value, with a new callback function to take its place.
83</p>
84
85<p class=note>
86Note: The <tt>callback</tt> module provides several standard receive callbacks, including the following:
87</p>
88
89<pre class=example>
90function 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
100end
101</pre>
102
103<p class=note>
104This function creates a new receive callback that concatenates all
105received chunks into a the same concat object, which can later be
106queried 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>
112chunk, 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>
116The callback provided by the user will be repeatedly called whenever the 91A send callback will be called whenever LuaSocket
117library needs more data to be sent. 92needs more data to be sent.
118</p> 93</p>
119 94
120<p class=return> 95<p class=return>
121Each time the callback is called, it should return the next chunk of data. It 96Each time the callback is called, it should return the next chunk of data. It
122can optionally return, as it's second return value, a new callback to replace 97can optionally return, as it's second return value, a new callback to replace
123itself. The callback can abort the process at any time by returning 98itself. 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>
128Note: Below is the implementation of the <tt>callback.send.file</tt>
129function. 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>
133function 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
142end
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>
150The module <tt>callback.lua</tt> provides several predefined callbacks to 107The Callback module provides several predefined callbacks to
151perform the most common input/output operations. Callbacks are provided 108perform the most common input/output operations. Callbacks are provided
152that send and receive contents of files and strings. Furthermore, 109that send and receive contents of files and strings. Furthermore,
153composition functions are provided to chain callbacks with filters, such as 110composition functions are provided to chain callbacks with filters, such as
154the filters defined in the <tt>mime.lua</tt> module. 111the filters defined in the <a href=mime.html>MIME</a> module.
112Together, these two modules provide a powerful interface to send and
113receive 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>
164This function creates a send callback that will return the contents 123This function creates a send callback that returns the contents
165of a the file, until the file has been entirely sent. When done, the 124of a file, chunk by chunk, until the file has been entirely sent.
166callback closes the file. 125When 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
172returned callback just aborts transmission with the error message. 131case, the function returns a callback that just aborts
132transmission 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>
180Note: The function is designed so that it directly accepts the return 140Note: This function is designed so that it directly accepts the return
181values of the <tt>io.open</tt> function. 141values 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<!--
158If <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>
200Returns a send callback for the string, or <tt>nil</tt> if the string is 164Returns 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>
205Note: If any function in the LuaSocket API receives a <tt>nil</tt> 169Note: A <tt>nil</tt></b>
206send callback, it assumes there is nothing to be sent. 170send 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>
216This function creates a send callback that will send 180This function creates a send callback that will filter
217all data that it gets from another callback, 181all the data it receives from another send callback, before
218after passing the data through a filter. 182sending 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.
226Returns a callback chained with the filter. 190Returns a callback chained with the filter.
227</p> 191</p>
228 192
229(write a note!) 193<p class=note>
194Note: Several filters are defined in the <a href=mime.html>MIME</a>
195module. Below is an example that creates a send callback that sends
196a file's contents encoded in the Base64 transfer content encoding.
197</p>
198
199<pre class=example>
200send_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>
210The call to <a href=mime.html#chain><tt>socket.mime.chain</tt></a>
211creates a chained filter that encodes it's input and then breaks it
212into lines. The call to <tt>socket.callback.chain</tt> creates a chained
213send callback that reads the file from disk and passes it through the
214filter before sending it.
215</p>
230 216
231<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 217<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
232 218