#include <chrono>
#include <iostream>
#include <thread>
#include "server.h"
#include "client.h"
int main() {
const std::uint16_t port = 15555;
acsf::setTimeout(acsf::SERVER_HEARTBEAT_TIMEOUT_MS, std::chrono::milliseconds(5000));
serverLog.
folder =
"LOG_FOLDER/server";
[] { std::cout << "[server] a client disconnected\n"; },
true,
false,
serverLog);
std::thread serverThread([&server] {
while (auto msg = server.receive()) {
std::cout << "[server] got from client " << msg->isFrom << ": " << msg->payload << "\n";
server.send(msg->isFrom, "echo: " + msg->payload);
}
});
std::this_thread::sleep_for(std::chrono::milliseconds(200));
clientLog.
folder =
"LOG_FOLDER/client";
[] { std::cout << "[client] connection closed\n"; },
false,
true,
clientLog);
if (auto ec = client.start()) {
std::cerr << "[client] could not connect: " << ec.message() << "\n";
server.stop();
serverThread.join();
return 1;
}
for (int i = 0; i < 3; ++i) {
client.send("hello " + std::to_string(i));
}
for (int i = 0; i < 3; ++i) {
auto reply = client.receive();
if (!reply) break;
std::cout << "[client] got: " << reply->payload << "\n";
}
std::this_thread::sleep_for(std::chrono::seconds(3));
client.stop();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
server.stop();
serverThread.join();
std::cout << "done (see LOG_FOLDER for the log files)\n";
return 0;
}
A TCP client that connects to an acsf::Server and exchanges length-prefixed messages.
Definition client.h:30
A TCP server that accepts many clients and exchanges length-prefixed messages with them.
Definition server.h:31
void start()
Starts accepting clients on the I/O thread.
Definition server.h:110
std::chrono::milliseconds READ_TIMEOUT_MS
Global timeouts in milliseconds, shared by every server and client. Change them with setTimeout().
Definition protocol.h:37
void setTimeout(std::chrono::milliseconds &targetTimeout, std::chrono::milliseconds timeout)
Changes one of the global timeouts.
Definition protocol.h:80
Logging settings passed to acsf::Server and acsf::Client.
Definition logger.h:118
std::filesystem::path folder
Definition logger.h:123
bool enabled
Definition logger.h:120