kameloso.net

Functionality related to connecting to a server over the Internet.

Includes Fibers that help with resolving the address of, connecting to, and reading full string lines from a server.

Having them as Fibers means a program can do address resolution, connecting and reading while retaining the ability to do other stuff concurrently. This means you can conveniently run code in between each connection attempt, for instance, without breaking the program's flow.

Members

Classes

SSLException
class SSLException

Exception thrown when OpenSSL functions return a non-1 error code, such as when the OpenSSL context could not be setup, or when it could not establish an SSL connection from an otherwise live connection.

SSLFileException
class SSLFileException

Exception thrown when a certificate or a private key file could not be found.

SocketSendException
class SocketSendException

Exception thrown when a socket send action returned Socket.ERROR.

Functions

connectFiber
void connectFiber(Connection conn, uint connectionRetries, bool abort)

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.

listenFiber
void listenFiber(Connection conn, bool abort, int connectionLost)

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

resolveFiber
void resolveFiber(Connection conn, string address, ushort port, bool useIPv6, bool abort)

Given an address and a port, resolves these and populates the array of unique Address IPs inside the passed Connection.

Structs

Connection
struct Connection

Functions and state needed to maintain a connection.

ConnectionAttempt
struct ConnectionAttempt

Embodies the idea of a connection attempt.

ListenAttempt
struct ListenAttempt

Embodies the idea of a listening attempt.

ResolveAttempt
struct ResolveAttempt

Embodies the idea of an address resolution attempt.

Examples

import std.concurrency : Generator;

Connection conn;
bool abort;  // Set to true if something goes wrong

conn.reset();

bool useIPv6 = false;
enum resolveAttempts = 10;

auto resolver = new Generator!ResolveAttempt(() =>
    resolveFiber(conn, "irc.libera.chat", 6667, useIPv6, resolveAttempts, abort));

resolver.call();

resolveloop:
foreach (const attempt; resolver)
{
    // attempt is a yielded `ResolveAttempt`
    // switch on `attempt.state`, deal with it accordingly
}

// Resolution done

enum connectionRetries = 10;

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

connector.call();

connectorloop:
foreach (const attempt; connector)
{
    // attempt is a yielded `ConnectionAttempt`
    // switch on `attempt.state`, deal with it accordingly
}

// Connection established

enum timeoutSeconds = 600;

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

listener.call();

foreach (const attempt; listener)
{
    // attempt is a yielded `ListenAttempt`
    doThingsWithLineFromServer(attempt.line);
    // program logic goes here
}

Meta