Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Finn Williams | 9937f93 | 2020-04-29 12:00:24 +0100 | [diff] [blame] | 6 | #include "common/include/NetworkSockets.hpp" |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 7 | |
| 8 | #if defined(__unix__) |
| 9 | #include <unistd.h> |
| 10 | #include <fcntl.h> |
| 11 | #endif |
| 12 | |
| 13 | namespace armnnUtils |
| 14 | { |
| 15 | namespace Sockets |
| 16 | { |
| 17 | |
| 18 | bool Initialize() |
| 19 | { |
| 20 | #if defined(__unix__) |
| 21 | return true; |
| 22 | #elif defined(_MSC_VER) |
| 23 | WSADATA wsaData; |
| 24 | return WSAStartup(MAKEWORD(2, 2), &wsaData) == 0; |
| 25 | #endif |
| 26 | } |
| 27 | |
| 28 | int Close(Socket s) |
| 29 | { |
| 30 | #if defined(__unix__) |
| 31 | return close(s); |
| 32 | #elif defined(_MSC_VER) |
| 33 | return closesocket(s); |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | |
| 38 | bool SetNonBlocking(Socket s) |
| 39 | { |
| 40 | #if defined(__unix__) |
| 41 | const int currentFlags = fcntl(s, F_GETFL); |
| 42 | return fcntl(s, F_SETFL, currentFlags | O_NONBLOCK) == 0; |
| 43 | #elif defined(_MSC_VER) |
| 44 | u_long mode = 1; |
| 45 | return ioctlsocket(s, FIONBIO, &mode) == 0; |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | |
| 50 | long Write(Socket s, const void* buf, size_t len) |
| 51 | { |
| 52 | #if defined(__unix__) |
| 53 | return write(s, buf, len); |
| 54 | #elif defined(_MSC_VER) |
Rob Hughes | b98032f | 2020-04-24 11:41:34 +0100 | [diff] [blame] | 55 | return send(s, static_cast<const char*>(buf), static_cast<int>(len), 0); |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 56 | #endif |
| 57 | } |
| 58 | |
| 59 | |
| 60 | long Read(Socket s, void* buf, size_t len) |
| 61 | { |
| 62 | #if defined(__unix__) |
| 63 | return read(s, buf, len); |
| 64 | #elif defined(_MSC_VER) |
Rob Hughes | b98032f | 2020-04-24 11:41:34 +0100 | [diff] [blame] | 65 | return recv(s, static_cast<char*>(buf), static_cast<int>(len), 0); |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 66 | #endif |
| 67 | } |
| 68 | |
David Monahan | 5277ad5 | 2020-01-22 15:40:34 +0000 | [diff] [blame] | 69 | int Ioctl(Socket s, unsigned long int cmd, void* arg) |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 70 | { |
David Monahan | 5277ad5 | 2020-01-22 15:40:34 +0000 | [diff] [blame] | 71 | #if defined(__ANDROID__) |
| 72 | return ioctl(s, static_cast<int>(cmd), arg); |
| 73 | #elif defined(__unix__) |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 74 | return ioctl(s, cmd, arg); |
| 75 | #elif defined(_MSC_VER) |
| 76 | return ioctlsocket(s, cmd, static_cast<u_long*>(arg)); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | |
David Monahan | 5277ad5 | 2020-01-22 15:40:34 +0000 | [diff] [blame] | 81 | int Poll(PollFd* fds, nfds_t numFds, int timeout) |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 82 | { |
| 83 | #if defined(__unix__) |
| 84 | return poll(fds, numFds, timeout); |
| 85 | #elif defined(_MSC_VER) |
| 86 | return WSAPoll(fds, numFds, timeout); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | |
David Monahan | a0d1896 | 2020-01-24 09:13:33 +0000 | [diff] [blame] | 91 | armnnUtils::Sockets::Socket Accept(Socket s, sockaddr* addr, socklen_t* addrlen, int flags) |
Rob Hughes | 25b7436 | 2020-01-13 11:14:59 +0000 | [diff] [blame] | 92 | { |
| 93 | #if defined(__unix__) |
| 94 | return accept4(s, addr, addrlen, flags); |
| 95 | #elif defined(_MSC_VER) |
| 96 | return accept(s, addr, reinterpret_cast<int*>(addrlen)); |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | } |