listenFiber

A Socket-reading Generator. It reads and yields full string lines.

It maintains its own buffer into which it receives from the server, though not necessarily full lines. It thus keeps filling the buffer until it finds a newline character, yields ListenAttempts back to the caller of the Fiber, checks for more lines to yield, and if none yields an attempt with a ListenAttempt.State denoting that nothing was read and that a new attempt should be made later.

@safe @system
void
listenFiber
(
size_t bufferSize = BufferSize.socketReceive * 2
)

Parameters

bufferSize

What size static array to use as buffer. Defaults to twice of BufferSize.socketReceive for now.

conn Connection

Connection whose Socket it reads from the server with.

abort bool

Reference "abort" flag, which -- if set -- should make the function return and the Fiber terminate.

connectionLost int

How many seconds may pass before we consider the connection lost. Optional, defaults to Timeout.connectionLost.

Yields: ListenAttempts with information about the line received in its member values.

Examples

//Connection conn;  // Address previously connected established with

enum timeoutSeconds = 600;

auto listener = new Generator!ListenAttempt(() => listenFiber(conn, abort, timeoutSeconds));

listener.call();

foreach (const attempt; listener)
{
    // attempt is a yielded `ListenAttempt`

    with (ListenAttempt.State)
    final switch (attempt.state)
    {
    case prelisten:
        assert(0, "shouldn't happen");

    case isEmpty:
    case timeout:
        // Reading timed out or nothing was read, happens
        break;

    case hasString:
        // A line was successfully read!
        // program logic goes here
        doThings(attempt.line);
        break;

    case warning:
        // Recoverable
        warnAboutSomething(attempt.error);
        break;

    case error:
        // Unrecoverable
        dealWithError(attempt.error);
        return;
    }
}

Meta