• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 // This file (along with its corresponding .cpp) defines a very thin platform abstraction layer for the use of
7 // networking sockets. Thankfully the underlying APIs on Windows and Linux are very similar so not much conversion
8 // is needed (typically just forwarding the parameters to a differently named function).
9 // Some of the APIs are in fact completely identical and so no forwarding function is needed.
10 
11 #pragma once
12 
13 #if defined(__unix__) || defined(__APPLE__)
14 #include <poll.h>
15 #include <sys/ioctl.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #elif defined(_MSC_VER)
19 #include <WindowsWrapper.hpp>
20 #include <winsock2.h>
21 #include <afunix.h>
22 #elif defined(__MINGW32__)
23 #include <WindowsWrapper.hpp>
24 #include <winsock2.h>
25 #endif
26 
27 namespace arm
28 {
29 namespace pipe
30 {
31 
32 #if defined(__unix__)
33 
34 using Socket = int;
35 using PollFd = pollfd;
36 
37 #elif defined(__APPLE__)
38 
39 using Socket = int;
40 using PollFd = pollfd;
41 #define SOCK_CLOEXEC 0
42 
43 #elif defined(_MSC_VER)
44 
45 using Socket = SOCKET;
46 using PollFd = WSAPOLLFD;
47 using nfds_t = int;
48 using socklen_t = int;
49 #define SOCK_CLOEXEC 0
50 
51 #elif defined(__MINGW32__)
52 
53 using Socket = SOCKET;
54 using PollFd = WSAPOLLFD;
55 using nfds_t = int;
56 using socklen_t = int;
57 #define SOCK_CLOEXEC 0
58 
59 #endif
60 
61 /// Performs any required one-time setup.
62 bool Initialize();
63 
64 int Close(Socket s);
65 
66 bool SetNonBlocking(Socket s);
67 
68 long Write(Socket s, const void* buf, size_t len);
69 
70 long Read(Socket s, void* buf, size_t len);
71 
72 int Ioctl(Socket s, unsigned long int cmd, void* arg);
73 
74 int Poll(PollFd* fds, nfds_t numFds, int timeout);
75 
76 Socket Accept(Socket s, sockaddr* addr, socklen_t* addrlen, int flags);
77 
78 } // namespace arm
79 } // namespace pipe
80