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{
51
53 {
54 if (counter == 0) {
56 }
57 ++counter;
58 }
59
61 {
62 assert(counter > 0);
63 --counter;
64 if (counter == 0) {
66 }
67 }
68
69private:
70 static inline int counter = 0;
71};
72
73} // namespace openmsx
74
75#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(SocketActivator &&)=delete
SocketActivator(const SocketActivator &)=delete
SocketActivator & operator=(const SocketActivator &)=delete
SocketActivator & operator=(SocketActivator &&)=delete