openMSX
Socket.hh
Go to the documentation of this file.
1#ifndef SOCKET_HH
2#define SOCKET_HH
3
4#include <cassert>
5#include <cstddef>
6#include <string>
7
8#ifndef _WIN32
9#include <sys/types.h>
10#include <sys/socket.h>
11#include <sys/un.h>
12#include <netinet/in.h>
13#include <fcntl.h>
14#include <unistd.h>
15#else
16#include <WinSock2.h>
17#include <WS2tcpip.h>
18#endif
19
20namespace openmsx {
21
22#ifndef _WIN32
23inline constexpr int OPENMSX_INVALID_SOCKET = -1;
24inline constexpr int SOCKET_ERROR = -1;
25using SOCKET = int;
26using socklen_t = int;
27#else
28// INVALID_SOCKET is #defined as (SOCKET)(~0)
29// but that gives a old-style-cast warning
30static const SOCKET OPENMSX_INVALID_SOCKET = static_cast<SOCKET>(~0);
31using in_addr_t = UINT32;
32#endif
33
34[[nodiscard]] std::string sock_error();
35void sock_close(SOCKET sd);
36[[nodiscard]] ptrdiff_t sock_recv(SOCKET sd, char* buf, size_t count);
37[[nodiscard]] ptrdiff_t sock_send(SOCKET sd, const char* buf, size_t count);
38
40
41// Activate the socket subsystem (required on Windows)
42void sock_startup(); // should only be called via SockActivator
43void sock_cleanup();
44
46{
49
51 {
52 if (counter == 0) {
54 }
55 ++counter;
56 }
57
59 {
60 assert(counter > 0);
61 --counter;
62 if (counter == 0) {
64 }
65 }
66
67private:
68 static inline int counter = 0;
69};
70
71} // namespace openmsx
72
73#endif
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr int OPENMSX_INVALID_SOCKET
Definition Socket.hh:23
int socklen_t
Definition Socket.hh:26
ptrdiff_t sock_send(SOCKET sd, const char *buf, size_t count)
Definition Socket.cc:88
constexpr int SOCKET_ERROR
Definition Socket.hh:24
void sock_close(SOCKET sd)
Definition Socket.cc:55
void sock_startup()
Definition Socket.cc:30
std::string sock_error()
Definition Socket.cc:12
int SOCKET
Definition Socket.hh:25
void sock_cleanup()
Definition Socket.cc:45
ptrdiff_t sock_recv(SOCKET sd, char *buf, size_t count)
Definition Socket.cc:65
SocketActivator(const SocketActivator &)=delete
SocketActivator & operator=(const SocketActivator &)=delete