connectFiber

Fiber function that tries to connect to IPs in the ips array of the passed Connection, yielding at certain points throughout the process to let the calling function do stuff in between connection attempts.

@safe @system
void
connectFiber
(,,
const bool* abort
)

Parameters

conn Connection

The current, unconnected Connection.

connectionRetries uint

How many times to attempt to connect before signalling that we should move on to the next IP.

abort bool*

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

Examples

//Connection conn;  // Address previously resolved with `resolveFiber`

auto connector = new Generator!ConnectionAttempt(() =>
    connectFiber(
        conn,
        10,
        abort));

connectorloop:
foreach (const attempt; connector)
{
    // attempt is a yielded `ConnectionAttempt`

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

    case connected:
        // Socket is connected, continue with normal routine
        break connectorloop;

    case delayThenReconnect:
    case delayThenNextIP:
        // Delay and retry
        Thread.sleep(5.seconds);
        break;

    case ipv6Failure:
        // Deal with it
        dealWithIPv6(attempt.error);
        break;

    case sslFailure:
    case error:
        // Failed to connect
        return;
    }
}

// Connection established