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

Connection
class Connection

Functions and state needed to maintain a connection.

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.

openSSLIsInstalled
auto openSSLIsInstalled()

Returns whether OpenSSL is installed on the system or not. Only really relevant on Windows.

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

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

1 import std.concurrency : Generator;
2 
3 Connection conn;
4 bool* abort;  // Set to true if something goes wrong
5 
6 conn.reset();
7 
8 auto resolver = new Generator!ResolveAttempt(() =>
9     resolveFiber(
10         conn,
11         "irc.libera.chat",
12         6667,
13         useIPv6: false,
14         abort));
15 
16 resolveloop:
17 foreach (const attempt; resolver)
18 {
19     // attempt is a yielded `ResolveAttempt`
20     // switch on `attempt.state`, deal with it accordingly
21     // it may be `typeof(attempt.state).noop` on the first iteration
22 }
23 
24 // Resolution done
25 
26 enum connectionRetries = 10;
27 
28 auto connector = new Generator!ConnectionAttempt(() =>
29     connectFiber(
30         conn,
31         connectionRetries,
32         abort));
33 
34 connectorloop:
35 foreach (const attempt; connector)
36 {
37     // attempt is a yielded `ConnectionAttempt`
38     // as above
39 }
40 
41 // Connection established
42 
43 enum connectionLostSeconds = 600;
44 
45 auto listener = new Generator!ListenAttempt(() =>
46     listenFiber(
47         conn,
48         abort,
49         connectionLostSeconds));
50 
51 listener.call();
52 
53 foreach (const attempt; listener)
54 {
55     // attempt is a yielded `ListenAttempt`
56     // as above
57     doThingsWithLineFromServer(attempt.line);
58     // program logic goes here
59 }

Meta