diff options
Diffstat (limited to 'src/core/network')
| -rw-r--r-- | src/core/network/network.cpp | 84 | ||||
| -rw-r--r-- | src/core/network/network.h | 24 | ||||
| -rw-r--r-- | src/core/network/sockets.h | 4 |
3 files changed, 59 insertions, 53 deletions
diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 56d173b5e..681e93468 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp | |||
| @@ -11,7 +11,7 @@ | |||
| 11 | #ifdef _WIN32 | 11 | #ifdef _WIN32 |
| 12 | #define _WINSOCK_DEPRECATED_NO_WARNINGS // gethostname | 12 | #define _WINSOCK_DEPRECATED_NO_WARNINGS // gethostname |
| 13 | #include <winsock2.h> | 13 | #include <winsock2.h> |
| 14 | #elif __unix__ | 14 | #elif YUZU_UNIX |
| 15 | #include <errno.h> | 15 | #include <errno.h> |
| 16 | #include <fcntl.h> | 16 | #include <fcntl.h> |
| 17 | #include <netdb.h> | 17 | #include <netdb.h> |
| @@ -54,7 +54,7 @@ constexpr IPv4Address TranslateIPv4(in_addr addr) { | |||
| 54 | sockaddr TranslateFromSockAddrIn(SockAddrIn input) { | 54 | sockaddr TranslateFromSockAddrIn(SockAddrIn input) { |
| 55 | sockaddr_in result; | 55 | sockaddr_in result; |
| 56 | 56 | ||
| 57 | #ifdef __unix__ | 57 | #if YUZU_UNIX |
| 58 | result.sin_len = sizeof(result); | 58 | result.sin_len = sizeof(result); |
| 59 | #endif | 59 | #endif |
| 60 | 60 | ||
| @@ -63,7 +63,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { | |||
| 63 | result.sin_family = AF_INET; | 63 | result.sin_family = AF_INET; |
| 64 | break; | 64 | break; |
| 65 | default: | 65 | default: |
| 66 | UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", static_cast<int>(input.family)); | 66 | UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", input.family); |
| 67 | result.sin_family = AF_INET; | 67 | result.sin_family = AF_INET; |
| 68 | break; | 68 | break; |
| 69 | } | 69 | } |
| @@ -99,7 +99,7 @@ bool EnableNonBlock(SOCKET fd, bool enable) { | |||
| 99 | return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR; | 99 | return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR; |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | #elif __unix__ // ^ _WIN32 v __unix__ | 102 | #elif YUZU_UNIX // ^ _WIN32 v YUZU_UNIX |
| 103 | 103 | ||
| 104 | using SOCKET = int; | 104 | using SOCKET = int; |
| 105 | using WSAPOLLFD = pollfd; | 105 | using WSAPOLLFD = pollfd; |
| @@ -133,7 +133,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { | |||
| 133 | result.sin_family = AF_INET; | 133 | result.sin_family = AF_INET; |
| 134 | break; | 134 | break; |
| 135 | default: | 135 | default: |
| 136 | UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", static_cast<int>(input.family)); | 136 | UNIMPLEMENTED_MSG("Unhandled sockaddr family={}", input.family); |
| 137 | result.sin_family = AF_INET; | 137 | result.sin_family = AF_INET; |
| 138 | break; | 138 | break; |
| 139 | } | 139 | } |
| @@ -148,7 +148,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { | |||
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) { | 150 | int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) { |
| 151 | return poll(fds, nfds, timeout); | 151 | return poll(fds, static_cast<nfds_t>(nfds), timeout); |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | int closesocket(SOCKET fd) { | 154 | int closesocket(SOCKET fd) { |
| @@ -186,7 +186,7 @@ int TranslateDomain(Domain domain) { | |||
| 186 | case Domain::INET: | 186 | case Domain::INET: |
| 187 | return AF_INET; | 187 | return AF_INET; |
| 188 | default: | 188 | default: |
| 189 | UNIMPLEMENTED_MSG("Unimplemented domain={}", static_cast<int>(domain)); | 189 | UNIMPLEMENTED_MSG("Unimplemented domain={}", domain); |
| 190 | return 0; | 190 | return 0; |
| 191 | } | 191 | } |
| 192 | } | 192 | } |
| @@ -198,7 +198,7 @@ int TranslateType(Type type) { | |||
| 198 | case Type::DGRAM: | 198 | case Type::DGRAM: |
| 199 | return SOCK_DGRAM; | 199 | return SOCK_DGRAM; |
| 200 | default: | 200 | default: |
| 201 | UNIMPLEMENTED_MSG("Unimplemented type={}", static_cast<int>(type)); | 201 | UNIMPLEMENTED_MSG("Unimplemented type={}", type); |
| 202 | return 0; | 202 | return 0; |
| 203 | } | 203 | } |
| 204 | } | 204 | } |
| @@ -210,7 +210,7 @@ int TranslateProtocol(Protocol protocol) { | |||
| 210 | case Protocol::UDP: | 210 | case Protocol::UDP: |
| 211 | return IPPROTO_UDP; | 211 | return IPPROTO_UDP; |
| 212 | default: | 212 | default: |
| 213 | UNIMPLEMENTED_MSG("Unimplemented protocol={}", static_cast<int>(protocol)); | 213 | UNIMPLEMENTED_MSG("Unimplemented protocol={}", protocol); |
| 214 | return 0; | 214 | return 0; |
| 215 | } | 215 | } |
| 216 | } | 216 | } |
| @@ -238,45 +238,45 @@ SockAddrIn TranslateToSockAddrIn(sockaddr input_) { | |||
| 238 | return result; | 238 | return result; |
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | u16 TranslatePollEvents(u16 events) { | 241 | short TranslatePollEvents(PollEvents events) { |
| 242 | u16 result = 0; | 242 | short result = 0; |
| 243 | 243 | ||
| 244 | if (events & POLL_IN) { | 244 | if (True(events & PollEvents::In)) { |
| 245 | events &= ~POLL_IN; | 245 | events &= ~PollEvents::In; |
| 246 | result |= POLLIN; | 246 | result |= POLLIN; |
| 247 | } | 247 | } |
| 248 | if (events & POLL_PRI) { | 248 | if (True(events & PollEvents::Pri)) { |
| 249 | events &= ~POLL_PRI; | 249 | events &= ~PollEvents::Pri; |
| 250 | #ifdef _WIN32 | 250 | #ifdef _WIN32 |
| 251 | LOG_WARNING(Service, "Winsock doesn't support POLLPRI"); | 251 | LOG_WARNING(Service, "Winsock doesn't support POLLPRI"); |
| 252 | #else | 252 | #else |
| 253 | result |= POLL_PRI; | 253 | result |= POLLPRI; |
| 254 | #endif | 254 | #endif |
| 255 | } | 255 | } |
| 256 | if (events & POLL_OUT) { | 256 | if (True(events & PollEvents::Out)) { |
| 257 | events &= ~POLL_OUT; | 257 | events &= ~PollEvents::Out; |
| 258 | result |= POLLOUT; | 258 | result |= POLLOUT; |
| 259 | } | 259 | } |
| 260 | 260 | ||
| 261 | UNIMPLEMENTED_IF_MSG(events != 0, "Unhandled guest events=0x{:x}", events); | 261 | UNIMPLEMENTED_IF_MSG((u16)events != 0, "Unhandled guest events=0x{:x}", (u16)events); |
| 262 | 262 | ||
| 263 | return result; | 263 | return result; |
| 264 | } | 264 | } |
| 265 | 265 | ||
| 266 | u16 TranslatePollRevents(u16 revents) { | 266 | PollEvents TranslatePollRevents(short revents) { |
| 267 | u16 result = 0; | 267 | PollEvents result{}; |
| 268 | const auto translate = [&result, &revents](int host, unsigned guest) { | 268 | const auto translate = [&result, &revents](short host, PollEvents guest) { |
| 269 | if (revents & host) { | 269 | if ((revents & host) != 0) { |
| 270 | revents &= ~host; | 270 | revents &= static_cast<short>(~host); |
| 271 | result |= guest; | 271 | result |= guest; |
| 272 | } | 272 | } |
| 273 | }; | 273 | }; |
| 274 | 274 | ||
| 275 | translate(POLLIN, POLL_IN); | 275 | translate(POLLIN, PollEvents::In); |
| 276 | translate(POLLPRI, POLL_PRI); | 276 | translate(POLLPRI, PollEvents::Pri); |
| 277 | translate(POLLOUT, POLL_OUT); | 277 | translate(POLLOUT, PollEvents::Out); |
| 278 | translate(POLLERR, POLL_ERR); | 278 | translate(POLLERR, PollEvents::Err); |
| 279 | translate(POLLHUP, POLL_HUP); | 279 | translate(POLLHUP, PollEvents::Hup); |
| 280 | 280 | ||
| 281 | UNIMPLEMENTED_IF_MSG(revents != 0, "Unhandled host revents=0x{:x}", revents); | 281 | UNIMPLEMENTED_IF_MSG(revents != 0, "Unhandled host revents=0x{:x}", revents); |
| 282 | 282 | ||
| @@ -408,7 +408,7 @@ std::pair<Socket::AcceptResult, Errno> Socket::Accept() { | |||
| 408 | 408 | ||
| 409 | Errno Socket::Connect(SockAddrIn addr_in) { | 409 | Errno Socket::Connect(SockAddrIn addr_in) { |
| 410 | const sockaddr host_addr_in = TranslateFromSockAddrIn(addr_in); | 410 | const sockaddr host_addr_in = TranslateFromSockAddrIn(addr_in); |
| 411 | if (connect(fd, &host_addr_in, sizeof(host_addr_in)) != INVALID_SOCKET) { | 411 | if (connect(fd, &host_addr_in, sizeof(host_addr_in)) != SOCKET_ERROR) { |
| 412 | return Errno::SUCCESS; | 412 | return Errno::SUCCESS; |
| 413 | } | 413 | } |
| 414 | 414 | ||
| @@ -482,7 +482,7 @@ Errno Socket::Shutdown(ShutdownHow how) { | |||
| 482 | host_how = SD_BOTH; | 482 | host_how = SD_BOTH; |
| 483 | break; | 483 | break; |
| 484 | default: | 484 | default: |
| 485 | UNIMPLEMENTED_MSG("Unimplemented flag how={}", static_cast<int>(how)); | 485 | UNIMPLEMENTED_MSG("Unimplemented flag how={}", how); |
| 486 | return Errno::SUCCESS; | 486 | return Errno::SUCCESS; |
| 487 | } | 487 | } |
| 488 | if (shutdown(fd, host_how) != SOCKET_ERROR) { | 488 | if (shutdown(fd, host_how) != SOCKET_ERROR) { |
| @@ -503,10 +503,10 @@ std::pair<s32, Errno> Socket::Recv(int flags, std::vector<u8>& message) { | |||
| 503 | ASSERT(flags == 0); | 503 | ASSERT(flags == 0); |
| 504 | ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max())); | 504 | ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max())); |
| 505 | 505 | ||
| 506 | const int result = | 506 | const auto result = |
| 507 | recv(fd, reinterpret_cast<char*>(message.data()), static_cast<int>(message.size()), 0); | 507 | recv(fd, reinterpret_cast<char*>(message.data()), static_cast<int>(message.size()), 0); |
| 508 | if (result != SOCKET_ERROR) { | 508 | if (result != SOCKET_ERROR) { |
| 509 | return {result, Errno::SUCCESS}; | 509 | return {static_cast<s32>(result), Errno::SUCCESS}; |
| 510 | } | 510 | } |
| 511 | 511 | ||
| 512 | switch (const int ec = LastError()) { | 512 | switch (const int ec = LastError()) { |
| @@ -531,14 +531,14 @@ std::pair<s32, Errno> Socket::RecvFrom(int flags, std::vector<u8>& message, Sock | |||
| 531 | socklen_t* const p_addrlen = addr ? &addrlen : nullptr; | 531 | socklen_t* const p_addrlen = addr ? &addrlen : nullptr; |
| 532 | sockaddr* const p_addr_in = addr ? &addr_in : nullptr; | 532 | sockaddr* const p_addr_in = addr ? &addr_in : nullptr; |
| 533 | 533 | ||
| 534 | const int result = recvfrom(fd, reinterpret_cast<char*>(message.data()), | 534 | const auto result = recvfrom(fd, reinterpret_cast<char*>(message.data()), |
| 535 | static_cast<int>(message.size()), 0, p_addr_in, p_addrlen); | 535 | static_cast<int>(message.size()), 0, p_addr_in, p_addrlen); |
| 536 | if (result != SOCKET_ERROR) { | 536 | if (result != SOCKET_ERROR) { |
| 537 | if (addr) { | 537 | if (addr) { |
| 538 | ASSERT(addrlen == sizeof(addr_in)); | 538 | ASSERT(addrlen == sizeof(addr_in)); |
| 539 | *addr = TranslateToSockAddrIn(addr_in); | 539 | *addr = TranslateToSockAddrIn(addr_in); |
| 540 | } | 540 | } |
| 541 | return {result, Errno::SUCCESS}; | 541 | return {static_cast<s32>(result), Errno::SUCCESS}; |
| 542 | } | 542 | } |
| 543 | 543 | ||
| 544 | switch (const int ec = LastError()) { | 544 | switch (const int ec = LastError()) { |
| @@ -558,10 +558,10 @@ std::pair<s32, Errno> Socket::Send(const std::vector<u8>& message, int flags) { | |||
| 558 | ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max())); | 558 | ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max())); |
| 559 | ASSERT(flags == 0); | 559 | ASSERT(flags == 0); |
| 560 | 560 | ||
| 561 | const int result = send(fd, reinterpret_cast<const char*>(message.data()), | 561 | const auto result = send(fd, reinterpret_cast<const char*>(message.data()), |
| 562 | static_cast<int>(message.size()), 0); | 562 | static_cast<int>(message.size()), 0); |
| 563 | if (result != SOCKET_ERROR) { | 563 | if (result != SOCKET_ERROR) { |
| 564 | return {result, Errno::SUCCESS}; | 564 | return {static_cast<s32>(result), Errno::SUCCESS}; |
| 565 | } | 565 | } |
| 566 | 566 | ||
| 567 | const int ec = LastError(); | 567 | const int ec = LastError(); |
| @@ -591,10 +591,10 @@ std::pair<s32, Errno> Socket::SendTo(u32 flags, const std::vector<u8>& message, | |||
| 591 | to = &host_addr_in; | 591 | to = &host_addr_in; |
| 592 | } | 592 | } |
| 593 | 593 | ||
| 594 | const int result = sendto(fd, reinterpret_cast<const char*>(message.data()), | 594 | const auto result = sendto(fd, reinterpret_cast<const char*>(message.data()), |
| 595 | static_cast<int>(message.size()), 0, to, tolen); | 595 | static_cast<int>(message.size()), 0, to, tolen); |
| 596 | if (result != SOCKET_ERROR) { | 596 | if (result != SOCKET_ERROR) { |
| 597 | return {result, Errno::SUCCESS}; | 597 | return {static_cast<s32>(result), Errno::SUCCESS}; |
| 598 | } | 598 | } |
| 599 | 599 | ||
| 600 | const int ec = LastError(); | 600 | const int ec = LastError(); |
diff --git a/src/core/network/network.h b/src/core/network/network.h index 0622e4593..76b2821f2 100644 --- a/src/core/network/network.h +++ b/src/core/network/network.h | |||
| @@ -61,19 +61,25 @@ struct SockAddrIn { | |||
| 61 | }; | 61 | }; |
| 62 | 62 | ||
| 63 | /// Cross-platform poll fd structure | 63 | /// Cross-platform poll fd structure |
| 64 | |||
| 65 | enum class PollEvents : u16 { | ||
| 66 | // Using Pascal case because IN is a macro on Windows. | ||
| 67 | In = 1 << 0, | ||
| 68 | Pri = 1 << 1, | ||
| 69 | Out = 1 << 2, | ||
| 70 | Err = 1 << 3, | ||
| 71 | Hup = 1 << 4, | ||
| 72 | Nval = 1 << 5, | ||
| 73 | }; | ||
| 74 | |||
| 75 | DECLARE_ENUM_FLAG_OPERATORS(PollEvents); | ||
| 76 | |||
| 64 | struct PollFD { | 77 | struct PollFD { |
| 65 | Socket* socket; | 78 | Socket* socket; |
| 66 | u16 events; | 79 | PollEvents events; |
| 67 | u16 revents; | 80 | PollEvents revents; |
| 68 | }; | 81 | }; |
| 69 | 82 | ||
| 70 | constexpr u16 POLL_IN = 1 << 0; | ||
| 71 | constexpr u16 POLL_PRI = 1 << 1; | ||
| 72 | constexpr u16 POLL_OUT = 1 << 2; | ||
| 73 | constexpr u16 POLL_ERR = 1 << 3; | ||
| 74 | constexpr u16 POLL_HUP = 1 << 4; | ||
| 75 | constexpr u16 POLL_NVAL = 1 << 5; | ||
| 76 | |||
| 77 | class NetworkInstance { | 83 | class NetworkInstance { |
| 78 | public: | 84 | public: |
| 79 | explicit NetworkInstance(); | 85 | explicit NetworkInstance(); |
diff --git a/src/core/network/sockets.h b/src/core/network/sockets.h index 7bdff0fe4..a44393325 100644 --- a/src/core/network/sockets.h +++ b/src/core/network/sockets.h | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | #if defined(_WIN32) | 10 | #if defined(_WIN32) |
| 11 | #include <winsock.h> | 11 | #include <winsock.h> |
| 12 | #elif !defined(__unix__) | 12 | #elif !YUZU_UNIX |
| 13 | #error "Platform not implemented" | 13 | #error "Platform not implemented" |
| 14 | #endif | 14 | #endif |
| 15 | 15 | ||
| @@ -84,7 +84,7 @@ public: | |||
| 84 | 84 | ||
| 85 | #if defined(_WIN32) | 85 | #if defined(_WIN32) |
| 86 | SOCKET fd = INVALID_SOCKET; | 86 | SOCKET fd = INVALID_SOCKET; |
| 87 | #elif defined(__unix__) | 87 | #elif YUZU_UNIX |
| 88 | int fd = -1; | 88 | int fd = -1; |
| 89 | #endif | 89 | #endif |
| 90 | }; | 90 | }; |