Usage
The transport module interface is designed to be minimal and similar to existing networking libraries in C++.
The library interface is fully synchronous, but the transport runtime is executed in a separate asynchronous thread to allow for high-performance networking.
The library is uses spdlog
for logging.
Command types
Clients and servers designed to work together must share the same command types. These command types define the unique id of the command and whether it is reliable or not.
constexpr auto ReliableMsgType = woke::CommandType(1, woke::CommandTypeFlag::Reliable);
constexpr auto RegularMsgType = woke::CommandType(2);
Deserialization is left to the user to implement, but the library provides utility functions to work with the serialize.h
library.
struct MessageData {
uint32_t value;
bool flag = true;
template <typename Stream>
bool Serialize(Stream &stream)
{
serialize_bits(stream, value, 32);
serialize_bool(stream, flag);
serialize_align(stream);
return true;
}
};
Server
Server is created with a listen port, and can accept new connections with the accept()
methods.
woke::Server server(13042);
server.start(); // Starts the server thread
while (true) {
auto connection = server.accept();
if (connection.isClosed()) {
break;
}
std::thread(handleClient, std::move(connection)).detach();
}
Client
Client is created with a remote address and port, and can connect to the server with the connect()
method.
woke::Client client(argv[1], 13042);
auto connection = client.connect();
Connection
The connection object is used to send and receive messages.
MessageData data{42, true};
connection.send(woke::Command::serialize<MessageData>(ReliableMsgType, data));
for (const auto &command : connection.receive_all()) {
if (command.type() == ReliableMsgType) {
auto msg = command.deserialize<MessageData>();
}
Examples
See the client.cpp
and server.cpp
examples for more information.