31 namespace portability {
35 int network_socket_to_fd(NetworkSocket sock) {
36 return socket_to_fd(sock.data);
39 NetworkSocket fd_to_network_socket(
int fd) {
40 return NetworkSocket(fd_to_socket(fd));
44 bool is_fh_socket(
int fh) {
45 SOCKET
h = fd_to_socket(fh);
46 constexpr
long kDummyEvents = 0xABCDEF12;
48 e.lNetworkEvents = kDummyEvents;
49 WSAEnumNetworkEvents(h,
nullptr, &e);
50 return e.lNetworkEvents != kDummyEvents;
53 SOCKET fd_to_socket(
int fd) {
57 int socket_to_fd(SOCKET
s) {
61 template <
class R,
class F,
class...
Args>
62 static R wrapSocketFunction(F
f,
int s,
Args... args) {
63 NetworkSocket
h = fd_to_network_socket(s);
67 int accept(
int s,
struct sockaddr*
addr, socklen_t* addrlen) {
68 return network_socket_to_fd(
69 wrapSocketFunction<NetworkSocket>(
netops::accept, s, addr, addrlen));
72 int bind(
int s,
const struct sockaddr*
name, socklen_t namelen) {
76 int connect(
int s,
const struct sockaddr*
name, socklen_t namelen) {
88 int getsockopt(
int s,
int level,
int optname,
char* optval, socklen_t* optlen) {
89 return getsockopt(s, level, optname, (
void*)optval, optlen);
92 int getsockopt(
int s,
int level,
int optname,
void* optval, socklen_t* optlen) {
93 return wrapSocketFunction<int>(
97 int inet_aton(
const char* cp,
struct in_addr* inp) {
101 const char* inet_ntop(
int af,
const void* src,
char* dst, socklen_t
size) {
102 return ::inet_ntop(af, (
char*)src, dst,
size_t(size));
105 int listen(
int s,
int backlog) {
109 int poll(
struct pollfd fds[], nfds_t nfds,
int timeout) {
113 for (nfds_t
i = 0;
i < nfds; ++
i) {
114 desc[
i].
fd = fd_to_network_socket((
int)desc[
i].fd.data);
119 ssize_t
recv(
int s,
void* buf,
size_t len,
int flags) {
123 ssize_t
recv(
int s,
char* buf,
int len,
int flags) {
124 return recv(s, (
void*)buf, (
size_t)len, flags);
127 ssize_t
recv(
int s,
void* buf,
int len,
int flags) {
128 return recv(s, (
void*)buf, (
size_t)len, flags);
136 struct sockaddr*
from,
137 socklen_t* fromlen) {
138 return wrapSocketFunction<ssize_t>(
147 struct sockaddr*
from,
148 socklen_t* fromlen) {
149 return recvfrom(s, (
void*)buf, (
size_t)len, flags, from, fromlen);
157 struct sockaddr* from,
158 socklen_t* fromlen) {
159 return recvfrom(s, (
void*)buf, (
size_t)len, flags, from, fromlen);
166 ssize_t
send(
int s,
const void* buf,
size_t len,
int flags) {
170 ssize_t
send(
int s,
const char* buf,
int len,
int flags) {
171 return send(s, (
const void*)buf, (
size_t)len, flags);
174 ssize_t
send(
int s,
const void* buf,
int len,
int flags) {
175 return send(s, (
const void*)buf, (
size_t)len, flags);
189 return wrapSocketFunction<ssize_t>(
200 return sendto(s, (
const void*)buf, (
size_t)len, flags, to, tolen);
210 return sendto(s, buf, (
size_t)len, flags, to, tolen);
219 return wrapSocketFunction<int>(
229 return setsockopt(s, level, optname, (
const void*)optval, optlen);
240 int socketpair(
int domain,
int type,
int protocol,
int sv[2]) {
241 NetworkSocket
pair[2];
247 _open_osfhandle(static_cast<intptr_t>(pair[0].
data), O_RDWR | O_BINARY);
249 _open_osfhandle(static_cast<intptr_t>(pair[1].
data), O_RDWR | O_BINARY);
ssize_t recvfrom(NetworkSocket s, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
int connect(NetworkSocket s, const sockaddr *name, socklen_t namelen)
ssize_t sendmsg(NetworkSocket socket, const msghdr *message, int flags)
int inet_aton(const char *cp, in_addr *inp)
int setsockopt(NetworkSocket s, int level, int optname, const void *optval, socklen_t optlen)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
int getsockname(NetworkSocket s, sockaddr *name, socklen_t *namelen)
constexpr auto size(C const &c) -> decltype(c.size())
int shutdown(NetworkSocket s, int how)
static int socketToFd(int sock) noexcept
ssize_t sendto(NetworkSocket s, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
constexpr auto data(C &c) -> decltype(c.data())
int getsockopt(NetworkSocket s, int level, int optname, void *optval, socklen_t *optlen)
ssize_t send(NetworkSocket s, const void *buf, size_t len, int flags)
NetworkSocket socket(int af, int type, int protocol)
int listen(NetworkSocket s, int backlog)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
ssize_t recvmsg(NetworkSocket s, msghdr *message, int flags)
int poll(PollDescriptor fds[], nfds_t nfds, int timeout)
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
int getpeername(NetworkSocket s, sockaddr *name, socklen_t *namelen)
static int fdToSocket(int fd) noexcept
ssize_t recv(NetworkSocket s, void *buf, size_t len, int flags)
ThreadPoolListHook * addr
int socketpair(int domain, int type, int protocol, NetworkSocket sv[2])
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)