algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network Namespace Reference

HTTP and WebSocket transport, OAuth tokens, and request pacing. More...

Classes

class  AsyncHttpClient
 An asynchronous HTTP/1.1 client over TLS that keeps one connection to one host, used by the broker clients. More...
struct  AuthBody
 An OAuth access token and its expiry, the base of each broker's token-response class. More...
class  BasicRateLimitBucket
 A rate limiter (the generic cell rate algorithm) that computes when a request may go; it neither waits nor reads the clock itself. More...
class  ClientCredentials
 OAuth client credentials (an app key and secret), the base of each broker's credentials class. More...
class  HttpClient
 A blocking HTTP/1.1 client over TLS that keeps one connection to one host. More...
struct  HttpClientOptions
 Deadlines, size limits and pacing for an HTTP client. More...
struct  HttpRequest
 An HTTP request for AsyncHttpClient. More...
struct  RateLimitBudget
 The request rate a connection may use. More...
class  RateLimitController
 The rate limiter of one connection (one app key): a RateLimitBucket on the steady clock, plus counters. More...
struct  RateLimitGroupStats
 A rate limiter's counters for one traffic class. More...
struct  RateLimitStats
 A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from RateLimitController::stats). More...
class  WebSocketSessionBase
 The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving, closing and reconnecting. More...
struct  WebSocketSessionOptions
 Options for a WebSocket session (WsSession, WssSession). More...
class  WsSession
 A WebSocket client over plain TCP (no TLS); derive from it and override on_message. More...
class  WssSession
 A WebSocket client over TLS; derive from it and override on_message. More...

Typedefs

using HttpResponse = http::response<http::string_body>
 The HTTP response type the transport clients return.
using RateLimitBucket = BasicRateLimitBucket<>
 A rate-limit bucket on the steady clock.
using WssSessionOptions = WebSocketSessionOptions
 Options for a WssSession.

Enumerations

enum class  RateLimitGroup {
  TRADING , AUTH , ACCOUNT , MARKET_DATA ,
  REFERENCE
}
 The traffic class of a request, for rate limiting and queue priority. The broker adapter that builds a request sets it. More...

Functions

BrokerError transport_error (std::string stage, beast::error_code ec)
 Converts a transport error into a BrokerError.
std::string make_host_header (const std::string &host, const std::string &port)
 Builds an HTTP Host header value.
void shutdown_ssl_stream (beast::ssl_stream< beast::tcp_stream > &stream, std::chrono::seconds timeout) noexcept
 Shuts a TLS stream down (best effort) and closes its socket.
void close_ssl_stream (beast::ssl_stream< beast::tcp_stream > &stream) noexcept
 Cancels a TLS stream's operations and closes its socket, without waiting for the TLS shutdown.
constexpr int rate_limit_priority (RateLimitGroup group) noexcept
 Returns a traffic class's queue priority.
constexpr std::string_view rate_limit_group_name (RateLimitGroup group) noexcept
 Returns a lowercase name for a traffic class, such as "market_data", for logs.
constexpr std::size_t rate_limit_group_index (RateLimitGroup group) noexcept
 Returns a traffic class's index, for per-class arrays.
constexpr int usable_burst (const RateLimitBudget &budget, RateLimitGroup group) noexcept
 Returns how much of the burst a traffic class may use.

Variables

constexpr std::array< RateLimitGroup, 5 > all_rate_limit_groups
 Every RateLimitGroup, in declaration order.
constexpr std::size_t rate_limit_group_count = all_rate_limit_groups.size()
 The number of traffic classes.

Detailed Description

HTTP and WebSocket transport, OAuth tokens, and request pacing.

Typedef Documentation

◆ HttpResponse

using algotradelib::network::HttpResponse = http::response<http::string_body>

The HTTP response type the transport clients return.

Definition at line 48 of file http_client.hpp.

◆ RateLimitBucket

A rate-limit bucket on the steady clock.

Definition at line 341 of file rate_limit.hpp.

◆ WssSessionOptions

Enumeration Type Documentation

◆ RateLimitGroup

The traffic class of a request, for rate limiting and queue priority. The broker adapter that builds a request sets it.

Enumerator
TRADING 

Order placement, amendment and cancellation.

AUTH 

Access-token issue and refresh.

ACCOUNT 

Holdings, working orders, order history and buying power.

MARKET_DATA 

Quotes, bars, order book and trades.

REFERENCE 

Instrument reference data and trading calendars.

Definition at line 26 of file rate_limit.hpp.

Function Documentation

◆ close_ssl_stream()

void algotradelib::network::close_ssl_stream ( beast::ssl_stream< beast::tcp_stream > & stream)
inlinenoexcept

Cancels a TLS stream's operations and closes its socket, without waiting for the TLS shutdown.

Parameters
streamThe stream.

Definition at line 159 of file http_client.hpp.

Referenced by close_ssl_stream().

◆ make_host_header()

std::string algotradelib::network::make_host_header ( const std::string & host,
const std::string & port )
inline

Builds an HTTP Host header value.

Parameters
hostThe host name.
portThe port.
Returns
host, with :port appended unless the port is 443.

Definition at line 133 of file http_client.hpp.

Referenced by make_host_header().

◆ rate_limit_group_index()

std::size_t algotradelib::network::rate_limit_group_index ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a traffic class's index, for per-class arrays.

Parameters
groupThe class.
Returns
An index below rate_limit_group_count.

Definition at line 107 of file rate_limit.hpp.

Referenced by algotradelib::network::RateLimitStats::group(), algotradelib::network::RateLimitStats::group(), and rate_limit_group_index().

◆ rate_limit_group_name()

std::string_view algotradelib::network::rate_limit_group_name ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a lowercase name for a traffic class, such as "market_data", for logs.

Parameters
groupThe class.
Returns
The name; empty for a value that is not a RateLimitGroup.

Definition at line 78 of file rate_limit.hpp.

Referenced by rate_limit_group_name().

◆ rate_limit_priority()

int algotradelib::network::rate_limit_priority ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a traffic class's queue priority.

From highest: AUTH (every other request waits for a token), TRADING, ACCOUNT, MARKET_DATA, REFERENCE.

Parameters
groupThe class.
Returns
The priority; larger is served first.

Definition at line 57 of file rate_limit.hpp.

Referenced by rate_limit_priority().

◆ shutdown_ssl_stream()

void algotradelib::network::shutdown_ssl_stream ( beast::ssl_stream< beast::tcp_stream > & stream,
std::chrono::seconds timeout )
inlinenoexcept

Shuts a TLS stream down (best effort) and closes its socket.

The TLS shutdown is a blocking call that timeout does not bound, so it can wait for an unresponsive peer.

Parameters
streamThe stream.
timeoutSet as the stream's expiry, which does not bound the blocking shutdown.

Definition at line 146 of file http_client.hpp.

Referenced by shutdown_ssl_stream().

◆ transport_error()

BrokerError algotradelib::network::transport_error ( std::string stage,
beast::error_code ec )
inline

Converts a transport error into a BrokerError.

Parameters
stageWhere it failed (such as "connect"), stored as the error code.
ecThe error.
Returns
A CANCELLED error for an aborted operation, otherwise TRANSPORT. reached_broker_ is left at its default; the caller sets it.

Definition at line 117 of file http_client.hpp.

Referenced by transport_error().

◆ usable_burst()

int algotradelib::network::usable_burst ( const RateLimitBudget & budget,
RateLimitGroup group )
inlineconstexprnoexcept

Returns how much of the burst a traffic class may use.

Parameters
budgetThe budget.
groupThe class.
Returns
burst_ for TRADING and AUTH; otherwise burst_ - trading_reserve_, at least 1.

Definition at line 138 of file rate_limit.hpp.

Referenced by algotradelib::network::RateLimitController::acquire(), algotradelib::network::RateLimitController::delay_until(), and usable_burst().

Variable Documentation

◆ all_rate_limit_groups

std::array<RateLimitGroup, 5> algotradelib::network::all_rate_limit_groups
inlineconstexpr
Initial value:
{
}
@ MARKET_DATA
Quotes, bars, order book and trades.
@ ACCOUNT
Holdings, working orders, order history and buying power.
@ TRADING
Order placement, amendment and cancellation.
@ REFERENCE
Instrument reference data and trading calendars.
@ AUTH
Access-token issue and refresh.

Every RateLimitGroup, in declaration order.

Definition at line 40 of file rate_limit.hpp.

◆ rate_limit_group_count

std::size_t algotradelib::network::rate_limit_group_count = all_rate_limit_groups.size()
inlineconstexpr

The number of traffic classes.

Definition at line 100 of file rate_limit.hpp.