kameloso.net

Functionality related to connecting to a server over the Internet.

Includes Fibers that help with connecting to and reading full string lines from a server.

Having them as Fibers means a program can do 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.

delegateResolve
auto delegateResolve(Connection conn, string address, ushort port, bool useIPv6, void delegate(ResolveAttempt) onSuccessDg, void delegate(ResolveAttempt) onRetryDg, bool delegate(ResolveAttempt) onFailureDg, bool* abort)

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

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.

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 void onSuccessDg(ResolveAttempt attempt)
9 {
10     writeln("Resolved IPs: ", conn.ips);
11 }
12 
13 void onRetryDg(ResolveAttempt attempt)
14 {
15     writeln("Retrying...");
16 }
17 
18 bool onFailureDg(ResolveAttempt attempt)
19 {
20     writeln("Failed to resolve!");
21     return false;
22 }
23 
24 immutable actionAfterResolve = delegateResolve(
25     conn: conn,
26     address: "example.com",
27     port: 80,
28     useIPv6: true,
29     onSuccessDg: &onSuccessDg,
30     onRetryDg: &onRetryDg,
31     onFailureDg: &onFailureDg,
32     abort: abort);
33 
34 if (actionAfterResolve != Next.continue_) return;
35 
36 enum connectionRetries = 10;
37 
38 auto connector = new Generator!ConnectionAttempt(() =>
39     connectFiber(
40         conn,
41         connectionRetries,
42         abort));
43 
44 connectorloop:
45 foreach (const attempt; connector)
46 {
47     // attempt is a yielded `ConnectionAttempt`
48     // as above
49 }
50 
51 // Connection established
52 
53 immutable connectionLost = 600.seconds;
54 
55 auto listener = new Generator!ListenAttempt(() =>
56     listenFiber(
57         conn,
58         abort,
59         connectionLost));
60 
61 listener.call();
62 
63 foreach (const attempt; listener)
64 {
65     // attempt is a yielded `ListenAttempt`
66     // as above
67     doThingsWithLineFromServer(attempt.line);
68     // program logic goes here
69 }

Meta