ASIO Client Server Framework
Loading...
Searching...
No Matches
acsf Namespace Reference

Everything in the framework lives in this namespace. More...

Classes

class  Client
 A TCP client that connects to an acsf::Server and exchanges length-prefixed messages. More...
struct  Log
 One log entry. Every field is optional and only the fields that are set appear in the output. More...
class  Logger
 Writes log entries to rotating files in the background. Used internally by Server and Client. More...
struct  LoggerConfig
 Logging settings passed to acsf::Server and acsf::Client. More...
struct  Message
 A message received from the other side. More...
class  Server
 A TCP server that accepts many clients and exchanges length-prefixed messages with them. More...

Enumerations

enum class  LogLevel { DEBUG , INFO , WARN , ERROR }
 Severity of a log entry. More...
enum class  TimeoutType {
  Read , ReadBody , Send , HeartbeatTimeout ,
  HeartbeatRate
}
 Selects a timeout for Server::setTimeout(). More...

Functions

std::string levelToString (LogLevel level)
 Converts a LogLevel to its upper case name, e.g. "WARN".
void setMaxLogFileSize (std::uintmax_t newSize)
 Sets the size at which log files are rotated.
void setTimeout (std::chrono::milliseconds &targetTimeout, std::chrono::milliseconds timeout)
 Changes one of the global timeouts.
std::uint32_t networkToHostLong (uint32_t bytes)
 Converts a 32 bit value from network (big endian) to host byte order.
std::uint32_t hostToNetworkLong (uint32_t bytes)
 Converts a 32 bit value from host to network (big endian) byte order.
std::optional< std::vector< char > > getMessageFrame (std::string_view body, bool isHeartBeat)
 Builds the bytes put on the wire: a 4 byte big endian header followed by the body. The top bit of the header marks a heartbeat; the other 31 bits hold the body length.
std::optional< std::pair< std::uint32_t, bool > > parseHeader (const std::array< char, HeaderSize > &header)
 Reads a 4 byte header.

Variables

constexpr std::size_t HeaderSize = 4
 Size in bytes of the length header in front of every message.
constexpr std::uint32_t MaxMessageSize = 1024 * 1024
 Largest allowed message body in bytes (1 MB).
std::uintmax_t MAX_LOG_FILE_SIZE = 10 * 1024 * 1024
 Default size in bytes at which a log file is rotated. Change with setMaxLogFileSize().
std::chrono::milliseconds READ_TIMEOUT_MS {15000}
 Global timeouts in milliseconds, shared by every server and client. Change them with setTimeout().

Detailed Description

Everything in the framework lives in this namespace.

Enumeration Type Documentation

◆ LogLevel

enum class acsf::LogLevel
strong

Severity of a log entry.

Enumerator
DEBUG 

Detailed information for debugging.

INFO 

Normal events.

WARN 

Something unexpected that the framework recovered from.

ERROR 

A failure.

◆ TimeoutType

enum class acsf::TimeoutType
strong

Selects a timeout for Server::setTimeout().

Enumerator
Read 

Wait for the next message to start arriving.

ReadBody 

Wait for the rest of a message after its header.

Send 

Time allowed to send one message.

HeartbeatTimeout 

Silence allowed before a connection is dropped.

HeartbeatRate 

How often heartbeats are sent.

Function Documentation

◆ getMessageFrame()

std::optional< std::vector< char > > acsf::getMessageFrame ( std::string_view body,
bool isHeartBeat )
inline

Builds the bytes put on the wire: a 4 byte big endian header followed by the body. The top bit of the header marks a heartbeat; the other 31 bits hold the body length.

Parameters
bodyMessage body.
isHeartBeatTrue for a heartbeat (the body may then be empty).
Returns
The frame, or std::nullopt if the body is too large or is empty and not a heartbeat.

◆ parseHeader()

std::optional< std::pair< std::uint32_t, bool > > acsf::parseHeader ( const std::array< char, HeaderSize > & header)
inline

Reads a 4 byte header.

Parameters
headerThe header bytes.
Returns
The body length and whether it is a heartbeat, or std::nullopt if the length is invalid.

◆ setMaxLogFileSize()

void acsf::setMaxLogFileSize ( std::uintmax_t newSize)
inline

Sets the size at which log files are rotated.

Parameters
newSizeSize in bytes. Applies to loggers created afterwards.

◆ setTimeout()

void acsf::setTimeout ( std::chrono::milliseconds & targetTimeout,
std::chrono::milliseconds timeout )
inline

Changes one of the global timeouts.

Parameters
targetTimeoutThe timeout to change: READ_TIMEOUT_MS, READ_BODY_TIMEOUT_MS, SEND_TIMEOUT_MS, SERVER_HEARTBEAT_TIMEOUT_MS or HEARTBEAT_RATE.
timeoutNew value in milliseconds. 0 turns that timeout off.
Examples
main_example.cpp.

Variable Documentation

◆ READ_TIMEOUT_MS

std::chrono::milliseconds acsf::READ_TIMEOUT_MS {15000}
inline

Global timeouts in milliseconds, shared by every server and client. Change them with setTimeout().

  • READ_TIMEOUT_MS: max wait for the next message to start arriving (default 15000)
  • READ_BODY_TIMEOUT_MS: max wait for the rest of a message once its header arrived (default 60000)
  • SEND_TIMEOUT_MS: max time to finish sending one message (default 60000)
  • SERVER_HEARTBEAT_TIMEOUT_MS: max time a connection may stay silent before it is dropped (default 45000)
  • HEARTBEAT_RATE: how often heartbeats are sent (default 15000)
Examples
main_example.cpp.