MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
server.cpp File Reference
#include "mxnetwork/socket.hpp"
#include <atomic>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <signal.h>
#include <sys/socket.h>

Go to the source code of this file.

Functions

void exit_signal (int)
int main (int argc, char **argv)

Variables

std::atomic< bool > active_loop {false}

Function Documentation

◆ exit_signal()

void exit_signal ( int )

Definition at line 27 of file server.cpp.

27 {
28 active_loop.store(false);
29}
std::atomic< bool > active_loop
Definition server.cpp:16

◆ main()

int main ( int argc,
char ** argv )

Definition at line 32 of file server.cpp.

32 {
33 if (argc != 2) {
34 std::cerr << "server: Invalid arguments\n"
35 << argv[0] << " <port>\n";
36 return EXIT_FAILURE;
37 }
39#ifdef _WIN32
40 if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
41 std::cerr << "Error setting console handler\n";
42 return EXIT_FAILURE;
43 }
44#else
45 struct sigaction sa = {};
46 sa.sa_handler = exit_signal;
47 sa.sa_flags = 0;
48 if (sigaction(SIGINT, &sa, nullptr) == -1) {
49 perror("sigaction");
50 return EXIT_FAILURE;
51 }
52#endif
53
54 try {
57 if (sock.listen(argv[1], 5)) {
58 active_loop.store(true);
59 while (active_loop.load()) {
60 std::optional<mxnetwork::Socket> s = sock.accept();
61 if (s) {
62 std::thread t([sockfd = sock.sockfd()](mxnetwork::Socket sfd) {
63 char buffer[256];
64 ssize_t bytes = 0;
65 if ((bytes = sfd.read_all(buffer, 255)) > 0) {
66 buffer[bytes] = '\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);
72 return;
73 }
74 std::cout << value << "\n";
75 } else {
76 std::cerr << "Error reading stream.\n";
77 }
78 }, std::move(*s));
79
80 t.detach();
81 } else {
83 continue;
84 break;
85 }
86 }
87 } else {
88 perror("listen");
89 }
90 } catch (const mxnetwork::Exception &s) {
91 std::cerr << "Exception: " << s.text() << "\n";
92 return EXIT_FAILURE;
93 }
94 return EXIT_SUCCESS;
95}
Lightweight exception wrapper for MXNetwork failures.
Definition exception.hpp:11
std::string text() const
Return the stored error text.
Definition exception.cpp:5
C++ wrapper around the MXNetwork socket API.
Definition socket.hpp:57
#define SOCK_ERRNO
Definition mxsocket.h:51
void mx_socket_ignore_pipe_signal()
Ignore SIGPIPE on platforms that require it.
Definition mxsocket.c:577
#define SOCK_EINTR
Definition mxsocket.h:52
@ TYPE_INET
IPv4 stream socket.
Definition socket.hpp:41
void exit_signal(int)
Definition server.cpp:27
RAII helper that initializes and shuts down the platform socket subsystem.
Definition socket.hpp:21

Variable Documentation

◆ active_loop

std::atomic<bool> active_loop {false}

Definition at line 16 of file server.cpp.

16{false};