2 #ifndef ENIGMA_SINGLE_INSTANCE_H
3 #define ENIGMA_SINGLE_INSTANCE_H
5 #if defined(ENIGMA_PLATFORM_WINDOWS)
6 #ifndef WIN32_LEAN_AND_MEAN
7 #define WIN32_LEAN_AND_MEAN
9 #if defined(_WIN32_WINNT)
11 #define _WIN32_WINNT 0x06000100
14 #include <SDKDDKVer.h>
21 #include <netinet/in.h>
44 #if defined(ENIGMA_PLATFORM_WINDOWS)
47 const auto err = WSAStartup(MAKEWORD(2, 2), &data);
50 auto err_reason = [err]() ->
const char * {
51 #define CASE_RET(c, v) \
55 CASE_RET(WSASYSNOTREADY,
"The underlying network subsystem is not ready for network communication.");
56 CASE_RET(WSAVERNOTSUPPORTED,
"The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation.");
57 CASE_RET(WSAEINPROGRESS,
"A blocking Windows Sockets 1.1 operation is in progress.");
58 CASE_RET(WSAEPROCLIM,
"A limit on the number of tasks supported by the Windows Sockets implementation has been reached.");
59 CASE_RET(WSAEFAULT,
"The lpWSAData parameter is not a valid pointer.");
61 return "<unknown winsock err>";
66 ENIGMA_ERROR(
"Failed to start WinSock: #{}: {}", err, err_reason());
72 m_socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
75 struct sockaddr_in name {};
76 name.sin_family = AF_INET;
77 name.sin_port = htons(port);
78 name.sin_addr.s_addr = htonl(INADDR_ANY);
79 m_rc = bind(m_socket_fd, (
struct sockaddr *) &name,
sizeof(name));
81 ENIGMA_ERROR(
"Failed to bind socket: {}", std::strerror(errno));
85 ENIGMA_ERROR(
"Failed to create socket: {}", std::strerror(errno));
89 ENIGMA_INFO(
"Created Single Process Instance Socket: {} Port: {}", m_socket_fd, m_port);
102 #if defined(ENIGMA_PLATFORM_WINDOWS)
103 closesocket(m_socket_fd);
108 ENIGMA_INFO(
"Closed Single Process Instance Socket: {} Port: {}", m_socket_fd, m_port);
114 std::uint16_t m_port{};
#define ENIGMA_ERROR(...)
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
constexpr socket_t M_INVALID_SOCKET
Single process instance class.
virtual ~SingleProcessInstance() noexcept
bool IsUnique() const noexcept
SingleProcessInstance(const std::uint16_t port) noexcept