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