21 #include <shared_mutex> 22 #include <unordered_map> 36 static std::unordered_map<SOCKET, int> socketMap;
37 static std::shared_mutex socketMapMutex;
39 static int closeOnlyFileDescriptor(
int fd) {
40 HANDLE
h = (HANDLE)_get_osfhandle(fd);
51 constexpr DWORD protectFlag = HANDLE_FLAG_PROTECT_FROM_CLOSE;
52 DWORD handleFlags = 0;
53 if (!GetHandleInformation(h, &handleFlags)) {
56 if (!SetHandleInformation(h, protectFlag, protectFlag)) {
66 GetExceptionCode() == STATUS_HANDLE_NOT_CLOSABLE
67 ? EXCEPTION_CONTINUE_EXECUTION
68 : EXCEPTION_CONTINUE_SEARCH) {
74 if (!SetHandleInformation(h, protectFlag, handleFlags)) {
86 std::unique_lock<std::shared_mutex>
lock{socketMapMutex};
87 if (socketMap.find(hand) != socketMap.end()) {
88 socketMap.erase(hand);
91 auto r = closeOnlyFileDescriptor(fd);
95 return closesocket((SOCKET)hand);
102 std::unique_lock<std::shared_mutex>
lock{socketMapMutex};
103 auto lookup = socketMap.find(sock);
104 found =
lookup != socketMap.end();
114 return closesocket(sock);
119 return INVALID_SOCKET;
122 return (SOCKET)_get_osfhandle(fd);
126 if (sock == INVALID_SOCKET) {
131 std::shared_lock<std::shared_mutex>
lock{socketMapMutex};
132 auto const found = socketMap.find(sock);
133 if (found != socketMap.end()) {
134 return found->second;
138 std::unique_lock<std::shared_mutex>
lock{socketMapMutex};
139 auto const found = socketMap.find(sock);
140 if (found != socketMap.end()) {
141 return found->second;
144 int fd = _open_osfhandle((intptr_t)sock, O_RDWR | O_BINARY);
145 socketMap.emplace(sock, fd);
static int close(int fd) noexcept
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
static int socketToFd(int sock) noexcept
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
static const std::string lookup
static int fdToSocket(int fd) noexcept