ArmNN
 20.02
NetworkSockets.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "NetworkSockets.hpp"
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)
55  return send(s, static_cast<const char*>(buf), len, 0);
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)
65  return recv(s, static_cast<char*>(buf), len, 0);
66 #endif
67 }
68 
69 int Ioctl(Socket s, unsigned long int cmd, void* arg)
70 {
71 #if defined(__ANDROID__)
72  return ioctl(s, static_cast<int>(cmd), arg);
73 #elif defined(__unix__)
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 
81 int Poll(PollFd* fds, nfds_t numFds, int timeout)
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 
91 armnnUtils::Sockets::Socket Accept(Socket s, sockaddr* addr, socklen_t* addrlen, int flags)
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 }
int Poll(PollFd *fds, nfds_t numFds, int timeout)
int Ioctl(Socket s, unsigned long int cmd, void *arg)
bool Initialize()
Performs any required one-time setup.
bool SetNonBlocking(Socket s)
armnnUtils::Sockets::Socket Accept(Socket s, sockaddr *addr, socklen_t *addrlen, int flags)
long Write(Socket s, const void *buf, size_t len)
long Read(Socket s, void *buf, size_t len)