blob: 29575cdcd6e0fa1ed749ad0dec038eefdef2de4b [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Jim Flynn6da6a452020-07-14 14:26:27 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Rob Hughes25b74362020-01-13 11:14:59 +00003// 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
Jim Flynn6da6a452020-07-14 14:26:27 +010013#if defined(__unix__) || defined(__APPLE__)
Rob Hughes25b74362020-01-13 11:14:59 +000014#include <poll.h>
15#include <sys/ioctl.h>
16#include <sys/socket.h>
17#include <sys/un.h>
18#elif defined(_MSC_VER)
Rob Hughesbc873d22020-05-20 13:11:37 +010019#include <WindowsWrapper.hpp>
Rob Hughes25b74362020-01-13 11:14:59 +000020#include <winsock2.h>
21#include <afunix.h>
Jim Flynnbbfe6032020-07-20 16:57:44 +010022#elif defined(__MINGW32__)
23#include <WindowsWrapper.hpp>
24#include <winsock2.h>
Rob Hughes25b74362020-01-13 11:14:59 +000025#endif
26
Jim Flynnbbfe6032020-07-20 16:57:44 +010027namespace arm
Rob Hughes25b74362020-01-13 11:14:59 +000028{
Jim Flynnbbfe6032020-07-20 16:57:44 +010029namespace pipe
Rob Hughes25b74362020-01-13 11:14:59 +000030{
31
32#if defined(__unix__)
33
34using Socket = int;
35using PollFd = pollfd;
36
Jim Flynn6da6a452020-07-14 14:26:27 +010037#elif defined(__APPLE__)
38
39using Socket = int;
40using PollFd = pollfd;
41#define SOCK_CLOEXEC 0
42
Rob Hughes25b74362020-01-13 11:14:59 +000043#elif defined(_MSC_VER)
44
45using Socket = SOCKET;
46using PollFd = WSAPOLLFD;
Rob Hughesb98032f2020-04-24 11:41:34 +010047using nfds_t = int;
48using socklen_t = int;
Rob Hughes25b74362020-01-13 11:14:59 +000049#define SOCK_CLOEXEC 0
50
Jim Flynnbbfe6032020-07-20 16:57:44 +010051#elif defined(__MINGW32__)
52
53using Socket = SOCKET;
54using PollFd = WSAPOLLFD;
55using nfds_t = int;
56using socklen_t = int;
57#define SOCK_CLOEXEC 0
58
Rob Hughes25b74362020-01-13 11:14:59 +000059#endif
60
61/// Performs any required one-time setup.
62bool Initialize();
63
64int Close(Socket s);
65
66bool SetNonBlocking(Socket s);
67
68long Write(Socket s, const void* buf, size_t len);
69
70long Read(Socket s, void* buf, size_t len);
71
David Monahan5277ad52020-01-22 15:40:34 +000072int Ioctl(Socket s, unsigned long int cmd, void* arg);
Rob Hughes25b74362020-01-13 11:14:59 +000073
David Monahan5277ad52020-01-22 15:40:34 +000074int Poll(PollFd* fds, nfds_t numFds, int timeout);
Rob Hughes25b74362020-01-13 11:14:59 +000075
David Monahana0d18962020-01-24 09:13:33 +000076Socket Accept(Socket s, sockaddr* addr, socklen_t* addrlen, int flags);
Rob Hughes25b74362020-01-13 11:14:59 +000077
Jim Flynnbbfe6032020-07-20 16:57:44 +010078} // namespace arm
79} // namespace pipe