Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
PlatformSocket.hpp
Go to the documentation of this file.
1#pragma once
3
4#include <string>
5
6#ifdef _WIN32
7 #ifndef WIN32_LEAN_AND_MEAN
8 #define WIN32_LEAN_AND_MEAN
9 #endif
10 #include <windows.h>
11 #include <winsock2.h>
12 #include <ws2tcpip.h>
13 #undef min
14 #undef max
15#else
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netinet/tcp.h>
19 #include <arpa/inet.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <cerrno>
23 #include <netdb.h>
24#endif
25
26namespace orbit::network {
27
28#ifdef _WIN32
29 using socket_t = SOCKET;
30 constexpr socket_t INVALID_SOCKET_FD = INVALID_SOCKET;
31 constexpr int SOCKET_ERROR_VAL = SOCKET_ERROR;
32
33 using socklen_t = int;
34 using ssize_t = intptr_t;
35
36 inline int get_last_socket_error() { return WSAGetLastError(); }
37 inline bool is_would_block_error() { return WSAGetLastError() == WSAEWOULDBLOCK; }
38#else
39 using socket_t = int;
41 constexpr int SOCKET_ERROR_VAL = -1;
42
45
46 inline int get_last_socket_error() { return errno; }
47 inline bool is_would_block_error() { return errno == EWOULDBLOCK || errno == EAGAIN; }
48#endif
49
57
61 std::string format_ip(const sockaddr* addr);
62
64 std::string peer_ip(socket_t fd);
65}
Definition Http2Session.hpp:21
void initialize_platform_networking()
constexpr socket_t INVALID_SOCKET_FD
Definition PlatformSocket.hpp:40
std::string format_ip(const sockaddr *addr)
std::string peer_ip(socket_t fd)
The connected peer's IP address (see format_ip), or "" if unknown.
void shutdown_socket(socket_t fd)
::ssize_t ssize_t
Definition PlatformSocket.hpp:44
void cleanup_platform_networking()
bool is_would_block_error()
Definition PlatformSocket.hpp:47
int socket_t
Definition PlatformSocket.hpp:39
::socklen_t socklen_t
Definition PlatformSocket.hpp:43
constexpr int SOCKET_ERROR_VAL
Definition PlatformSocket.hpp:41
int get_last_socket_error()
Definition PlatformSocket.hpp:46
void set_non_blocking(socket_t fd)
void close_socket(socket_t fd)