9#define SHUT_RDWR SD_BOTH
13#include <sys/socket.h>
19BOOL WINAPI console_handler(DWORD signal) {
20 if (signal == CTRL_C_EVENT || signal == CTRL_CLOSE_EVENT) {
32int main(
int argc,
char **argv) {
34 std::cerr <<
"server: Invalid arguments\n"
35 << argv[0] <<
" <port>\n";
40 if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
41 std::cerr <<
"Error setting console handler\n";
45 struct sigaction sa = {};
48 if (sigaction(SIGINT, &sa,
nullptr) == -1) {
57 if (sock.
listen(argv[1], 5)) {
60 std::optional<mxnetwork::Socket> s = sock.
accept();
65 if ((bytes = sfd.read_all(buffer, 255)) > 0) {
67 std::string value{buffer};
68 if (value.find(
"exit") != std::string::npos) {
69 active_loop.store(false);
70 std::cerr <<
"server: Exiting..\n";
71 shutdown(sockfd, SHUT_RDWR);
74 std::cout << value <<
"\n";
76 std::cerr <<
"Error reading stream.\n";
91 std::cerr <<
"Exception: " << s.
text() <<
"\n";
Lightweight exception wrapper for MXNetwork failures.
std::string text() const
Return the stored error text.
C++ wrapper around the MXNetwork socket API.
bool listen(std::string_view port, int backlog)
Start listening on an Internet port.
std::optional< Socket > accept()
Accept an incoming connection.
mx_socket_fd sockfd() const
Return the underlying socket handle.
void mx_socket_ignore_pipe_signal()
Ignore SIGPIPE on platforms that require it.
@ TYPE_INET
IPv4 stream socket.
std::atomic< bool > active_loop
RAII helper that initializes and shuts down the platform socket subsystem.