eve.rt.fiber.socket

Fiber-Socket — Blocking-Style TCP on Top of Eve's Async TcpConnection

This module provides FiberSocket, a struct wrapper around Eve's callback-driven TcpConnection that turns every I/O operation into a fiber yield point. From the caller's perspective the API looks blocking; from the event loop's perspective it is just more callbacks.

Must be used from within a fiber that is managed by a FiberScheduler attached to an EventLoop.

Example (inside a fiber):

auto sock = FiberSocket.create(sched, loop);
scope (exit) sock.dispose();
sock.connect("example.com", 80);
sock.write(cast(const(ubyte)[])"GET / HTTP/1.0\r\n\r\n");
auto body = sock.read(4096);
sock.close();

Types 1

A fiber-friendly TCP connection.

Wraps a TcpConnection. All I/O methods yield the current fiber and resume when the operation completes or fails. Must be used from within a fiber scheduled by a FiberScheduler.

Data lifetime: the read() method returns a GC-owned copy of the received data. The caller does not need to worry about scope lifetime issues — FiberSocket copies internally before resuming the fiber.

EOF detection: read() returns an empty array when the remote end closes the connection. Callers should treat an empty return as end-of-stream.

Fields
private TcpConnection _conn
private FiberScheduler _sched
private EventLoop * _loop
Methods
FiberSocket create(FiberScheduler sched, ref EventLoop loop) static @trusted nothrowCreate a `FiberSocket` bound to the given scheduler and event loop.
FiberSocket adopt(FiberScheduler sched, ref EventLoop loop, Handle clientHandle) static @trusted nothrowCreate a `FiberSocket` by adopting an already-connected handle.
void connect(scope const(char)[] host, ushort port) @trustedConnect to a remote host. Yields until connected or an error occurs.
ubyte[] read(size_t maxBytes) @trustedRead up to `maxBytes` of data. Yields until data arrives or the connection closes.
void write(scope const(ubyte)[] data) @trustedWrite data to the connection. Yields if the OS send buffer is full, then retries until all data has been accepted.
void close() @trusted nothrowClose the underlying TCP connection.
void dispose() @trusted nothrowRelease all resources held by this socket.
bool isOpen() @property const @safe nothrow @nogcCheck whether the underlying connection is open.
IpAddress remoteAddress() @property const @safe nothrow @nogcGet the remote IP address.
IpAddress localAddress() @property const @safe nothrow @nogcGet the local IP address.