turbo.iosimple – Simple Callback-less asynchronous sockets¶
A simple interface for the IOStream class without the callback spaghetti, but still the async backend (the yield is done internally):
turbo.ioloop.instance():add_callback(function()
local stream = turbo.iosimple.dial("tcp://turbolua.org:80")
stream:write("GET / HTTP/1.0\r\n\r\n")
local data = stream:read_until_close()
print(data)
turbo.ioloop.instance():close()
end):start()
-
iosimple.
dial
(address, ssl, io)¶ Connect to a host using a simple URL pattern.
Parameters: - address (String) – The address to connect to in URL form, e.g:
"tcp://turbolua.org:80"
. - ssl (Boolean or Table) – Option to connect with SSL. Takes either a boolean true or a table with options, options described below.
- io (IOLoop object) – IOLoop class instance to use for event processing. If none is set then the global instance is used, see the ioloop.instance() function.
Available SSL options:
Boolean, if ssl param is set to
true
, it will be equal to a SSL option table like this{verify=true}
. If not argument is given or nil then SSL will not be used at all.A table may be used to give additional options instead of just a “enable” button:
key_file
(String) - Path to SSL / HTTPS key file.cert_file
(String) - Path to SSL / HTTPS certificate file.ca_cert_file
(String) - Path to SSL / HTTPS CA certificate verify location, if not given builtin is used, which is copied from Ubuntu 12.10.verify
(Boolean) SSL / HTTPS verify servers certificate. Default is true.
- address (String) – The address to connect to in URL form, e.g:
IOSimple class¶
A alternative to the IOStream class that were added in version 2.0. The goal
of this class is to further simplify the way that we use Turbo. The IOStream class
is based on callbacks, and while this to some may be the optimum way it might not
be for others. You could always use the async.task()
function to wrap it in a
coroutine and yield it. To save you the hassle a new class has been made.
All functions may raise errors. All functions yield to the IOLoop internally. You may catch errors with xpcall or pcall.
-
IOSimple
(stream)¶ Wrap a IOStream class instance with a simpler IO. If you are not wrapping consider using
iosimple.dial()
.Parameters: stream ( IOStream object
) – A stream already connected. If not consider usingiosimple.dial()
.Return type: IOSimple object
-
IOSimple:read_until(delimiter)
Read until delimiter. Delimiter is plain text, and does not support Lua patterns. See read_until_pattern for that functionality. read_until should be used instead of read_until_pattern wherever possible because of the overhead of doing pattern matching.
Parameters: delimiter (String) – Delimiter sequence, text or binary. Return type: String
-
IOSimple:read_until_pattern(pattern)
Read until pattern is matched, then return with received data. If you only are doing plain text matching then using read_until is recommended for less overhead.
Parameters: pattern (String) – Lua pattern string. Return type: String
-
IOSimple:read_bytes(num_bytes)
Read the given number of bytes.
Parameters: num_bytes (Number) – The amount of bytes to read. Return type: String
-
IOSimple:read_until_close()
Reads all data from the socket until it is closed.
Return type: String
-
IOSimple:write(data)
Write the given data to this stream. Returns when the data has been written to socket.
-
IOSimple:close()
Close this stream and its socket.
-
IOSimple:get_iostream()
Returns the IOStream instance used by the IOSimple instance.
Return type: IOStream object