IO tools for Hy.
Flexible buffer data struct.
(setv buffer (Buffer b"hello"))
#(buffer.pos buffer.data) ; 0 b"hello"
(.read buffer 2) ; b"he"
#(buffer.pos buffer.data) ; 2 b"hello"
(.strip buffer)
#(buffer.pos buffer.data) ; 0 b"llo"
Buffer.readexactly
or Buffer.readuntil
will raise
BufferWantReadError
if there has no enough data, in this case, user
should revert buffer.pos, push more data to buffer, and then read
again.
DSL used to describe data structures.
(defstruct Short [int :len 2])
(Short.pack 1) ; b"\x00\x01"
(Short.unapck b"\x00\x01") ; 1
(defstruct ShortPair
[[first [int :len 2]]
[second [int :len 2]]])
(ShortPair.pack #(1 2)) ; b"\x00\x01\x00\x02"
(ShortPair.unpack b"\x00\x01\x00\x02") ; #(1 2)
(defstruct int2 [int :len 2])
(defstruct Short int2)
(defstruct ShortPair [[first Short] [second Short]])
See iotools.proto
or iotools-tls13.struct
for more examples.
Sync/Async stream abstraction, based on dash.do/a!
.
This module inspired by Python built-in ssl.SSLObject
: for any read
function f
that read data from read buffer, reapeatedly push new
data from stream to read buffer and call f
on read buffer, while
BufferWantReadError
was raised.
Sync/Async object alias, and basic stream implementation.
Alias:
(name/a! sleep)
:time.sleep
asyncio.sleep
(name/a! Lock)
:threading.Lock
asyncio.Look
(name/a! Queue)
:queue.Queue
asyncio.Queue
- …
Basic streams: IOStream
, FileStream
, BytesStream
,
SocketStream
, AIOStream
, (name/a! TCPStream)
.
Simple protocol implementation based on iotools.
ssl.SSLObject
wrapper.
(with [stream (SyncTCPStream.open host port)]
(let [connector (SyncSSLConnector :ssl-context (ssl.create-default-context)
:server-hostname host)]
(with [stream (.handshake connector stream)]
...)))
(with stream [SyncTCPStream.open host port]
(.write stream (HTTPRequest.pack "GET" "/" "HTTP/1.1" {"Host" host}))
(.flush stream)
(let [#(version status reason headers) (.read-loop stream HTTPResponse.read)
stream (if (= (-get headers "Transfer-Encoding") "chunked")
(SyncHTTPChunkedStream :next-layer stream)
stream)]
...))
(with [stream (SyncTCPStream.open proxy-host proxy-port)]
(let [connector (SyncHTTPProxyConnector :host host :port port)]
(with [stream (.handshake connector stream)]
...)))
(with [stream (SyncTCPStream.open host port)]
(let [connector (SyncWSConnector :host host)]
(with [stream (.handshake connector stream)]
...)))
Similar with iotools.proto.http
.