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.

EmptyDataJSONException
class EmptyDataJSONException

Exception, to be thrown when an API query failed, due to having received empty JSON data.

EmptyResponseException
class EmptyResponseException

Exception, to be thrown when an API query failed, with only an empty response received.

ErrorJSONException
class ErrorJSONException

A normal Exception but where its type conveys the specific context of a JSONValue having an "error" field.

HTTPQueryException
class HTTPQueryException

Exception, to be thrown when a web request, such as an API query, failed.

Querier
class Querier

Querier.

QueryResponseJSONException
class QueryResponseJSONException

Abstract class for web query JSON exceptions, to deduplicate catching.

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.

UnexpectedJSONException
class UnexpectedJSONException

A normal Exception but where its type conveys the specific context of a JSONValue having unexpected contents.

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.

issueSyncHTTPRequest
auto issueSyncHTTPRequest(HTTPRequest request)

Issues a synchronous HTTP request.

listenFiber
void listenFiber(Connection conn, bool* abort, Duration connectionLost, size_t bufferSize)

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.

HTTPQueryResponse
struct HTTPQueryResponse

Embodies the notion of a response to a web request.

HTTPRequest
struct HTTPRequest

Embodies the notion of an HTTP request.

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 immutable connectionLost = 600.seconds;
44 
45 auto listener = new Generator!ListenAttempt(() =>
46     listenFiber(
47         conn,
48         abort,
49         connectionLost));
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