MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
recv.cpp
Go to the documentation of this file.
2#include <atomic>
3#include <cstdlib>
4#include <iostream>
5#include <signal.h>
6#include <string>
7#include <vector>
8#ifdef _WIN32
9#include <windows.h>
10#ifndef SHUT_RDWR
11#define SHUT_RDWR SD_BOTH
12#endif
13#else
14#include <signal.h>
15#include <sys/socket.h>
16#endif
17std::atomic<bool> active_loop{false};
18#ifdef _WIN32
19BOOL WINAPI console_handler(DWORD signal) {
20 if (signal == CTRL_C_EVENT || signal == CTRL_CLOSE_EVENT) {
21 active_loop.store(false);
22 return TRUE;
23 }
24 return FALSE;
25}
26#else
27void exit_signal(int) {
28 active_loop.store(false);
29}
30#endif
31
32int main(int argc, char **argv) {
33
34 if (argc != 2) {
35 std::cerr << "Usage: " << argv[0] << " <path>\n";
36 return EXIT_FAILURE;
37 }
38#ifdef _WIN32
39 if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
40 std::cerr << "Error setting console handler\n";
41 return EXIT_FAILURE;
42 }
43#else
44 struct sigaction sa = {};
45 sa.sa_handler = exit_signal;
46 sa.sa_flags = 0;
47 if (sigaction(SIGINT, &sa, nullptr) == -1) {
48 perror("sigaction");
49 return EXIT_FAILURE;
50 }
51#endif
52 try {
54 if (sock.bind_unix(argv[1])) {
55 std::cout << "Listening for UDP datagram on port " << argv[1] << "...\n";
56 std::vector<uint8_t> buffer(65536);
57 active_loop.store(true);
58 while (active_loop.load()) {
59 ssize_t bytes = sock.recvfrom(buffer.data(), buffer.size());
60 if (bytes > 0) {
61 std::cout << "Received " << bytes << " bytes.\n";
62 std::string data(buffer.begin(), buffer.end());
63 std::cout << "Data: " << data;
64 } else if (bytes < 0) {
65 if (errno == EINTR)
66 continue;
67 std::cerr << "read error.\n";
68 break;
69 }
70 }
71 }
72 } catch (const mxnetwork::Exception &e) {
73 std::cerr << "Exception: " << e.text() << "\n";
74 return EXIT_FAILURE;
75 }
76 return EXIT_SUCCESS;
77}
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
bool bind_unix(std::string_view path)
Bind a UNIX-domain socket to a local path.
Definition socket.cpp:153
ssize_t recvfrom(void *buf, size_t bytes)
Receive a datagram using the configured socket type.
Definition socket.cpp:216
int main(void)
Definition main.cpp:7
@ TYPE_UNIX_DGRAM
UNIX-domain datagram socket.
Definition socket.hpp:51
std::atomic< bool > active_loop
Definition server.cpp:16
void exit_signal(int)
Definition server.cpp:27
void exit_signal(int)
Definition recv.cpp:27