Skip to content

Commit

Permalink
清理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Nov 26, 2024
1 parent 0782074 commit ba11929
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions bee/net/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
# endif
#endif

#include <bee/error.h>
#include <bee/net/endpoint.h>
#include <bee/net/socket.h>
#include <bee/nonstd/unreachable.h>

#include <cassert>

#define net_success(x) ((x) == 0)

#if defined(__MINGW32__)
# define WSA_FLAG_NO_HANDLE_INHERIT 0x80
#endif

namespace bee::net::socket {
static bool net_success(int x) noexcept {
return x == 0;
}

#if defined(_WIN32)
static_assert(sizeof(SOCKET) == sizeof(fd_t));

Expand Down Expand Up @@ -229,15 +228,8 @@ namespace bee::net::socket {
const int ok = ::ioctlsocket(s, FIONBIO, &nonblock);
return net_success(ok);
}
static bool set_cloexec(fd_t s, bool set) noexcept {
if (set) {
DWORD flags = 0;
(void)flags;
assert(::GetHandleInformation((HANDLE)s, &flags) && (flags & HANDLE_FLAG_INHERIT) == 0);
return true;
} else {
return !!SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
}
static bool set_cloexec(fd_t s) noexcept {
return true;
}
#elif defined(__APPLE__)
static bool set_nonblock(int fd, bool set) noexcept {
Expand All @@ -255,10 +247,10 @@ namespace bee::net::socket {
while (!net_success(r) && errno == EINTR);
return net_success(r);
}
static bool set_cloexec(fd_t fd, bool set) noexcept {
static bool set_cloexec(fd_t fd) noexcept {
int ok;
do
ok = ::ioctl(fd, set ? FIOCLEX : FIONCLEX);
ok = ::ioctl(fd, FIOCLEX);
while (!net_success(ok) && errno == EINTR);
return net_success(ok);
}
Expand Down Expand Up @@ -313,7 +305,7 @@ namespace bee::net::socket {
if (fd == retired_fd) {
return retired_fd;
}
if (!set_cloexec(fd, true)) {
if (!set_cloexec(fd)) {
internal_close(fd);
return retired_fd;
}
Expand Down Expand Up @@ -454,7 +446,7 @@ namespace bee::net::socket {
#if defined(_WIN32) || defined(__APPLE__)
const fd_t fd = ::accept(s, addr, addrlen);
if (fd != retired_fd) {
if (!set_cloexec(fd, true)) {
if (!set_cloexec(fd)) {
internal_close(fd);
return retired_fd;
}
Expand Down Expand Up @@ -672,10 +664,10 @@ namespace bee::net::socket {
if (!net_success(ok)) {
return false;
}
if (!set_cloexec(temp[0], true)) {
if (!set_cloexec(temp[0])) {
goto fail;
}
if (!set_cloexec(temp[1], true)) {
if (!set_cloexec(temp[1])) {
goto fail;
}
if (!set_nonblock(temp[0], fd_flags == fd_flags::nonblock)) {
Expand Down Expand Up @@ -724,10 +716,10 @@ namespace bee::net::socket {
if (!net_success(ok)) {
return false;
}
if (!set_cloexec(temp[0], true)) {
if (!set_cloexec(temp[0])) {
goto fail;
}
if (!set_cloexec(temp[1], true)) {
if (!set_cloexec(temp[1])) {
goto fail;
}
if (!set_nonblock(temp[0], fd_flags == fd_flags::nonblock)) {
Expand Down

0 comments on commit ba11929

Please sign in to comment.