Skip to content

Commit 9454571

Browse files
authored
fix: get rid of poll macro (#1468)
1 parent e8a9d74 commit 9454571

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

include/dpp/compat.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************************************************************************
2+
*
3+
* D++, A Lightweight C++ library for Discord
4+
*
5+
* Copyright 2021 Craig Edwards and D++ contributors
6+
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
************************************************************************************/
21+
#pragma once
22+
23+
#ifdef _WIN32
24+
#include <WinSock2.h>
25+
#include <WS2tcpip.h>
26+
#include <io.h>
27+
namespace dpp::compat {
28+
using pollfd = WSAPOLLFD;
29+
inline int poll(pollfd* fds, ULONG nfds, int timeout) {
30+
return WSAPoll(fds, nfds, timeout);
31+
}
32+
} // namespace dpp::compat
33+
#pragma comment(lib, "ws2_32")
34+
#else
35+
#include <poll.h>
36+
#include <netinet/in.h>
37+
#include <sys/socket.h>
38+
#include <arpa/inet.h>
39+
namespace dpp::compat {
40+
using ::pollfd;
41+
using ::poll;
42+
} // namespace dpp::compat
43+
#endif

include/dpp/socket.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,8 @@
2222
#pragma once
2323

2424
#include <dpp/export.h>
25-
#ifdef _WIN32
26-
#include <WinSock2.h>
27-
#include <WS2tcpip.h>
28-
#include <io.h>
29-
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
30-
#define pollfd WSAPOLLFD
31-
#else
32-
#include <netinet/in.h>
33-
#include <sys/socket.h>
34-
#include <arpa/inet.h>
35-
#endif
25+
#include <dpp/compat.h>
26+
3627
#include <string_view>
3728
#include <cstdint>
3829

src/dpp/socketengines/poll.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,11 @@
2020
************************************************************************************/
2121

2222
#include <dpp/cluster.h>
23+
#include <dpp/compat.h>
2324
#include <dpp/socketengine.h>
2425
#include <dpp/exception.h>
2526
#include <vector>
2627
#include <shared_mutex>
27-
#ifdef _WIN32
28-
/* Windows-specific sockets includes */
29-
#include <WinSock2.h>
30-
#include <WS2tcpip.h>
31-
#include <io.h>
32-
/* Windows doesn't have standard poll(), it has WSAPoll.
33-
* It's the same thing with different symbol names.
34-
* Microsoft gotta be different.
35-
*/
36-
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
37-
#define pollfd WSAPOLLFD
38-
/* Windows sockets library */
39-
#pragma comment(lib, "ws2_32")
40-
#else
41-
/* Anything other than Windows (e.g. sane OSes) */
42-
#include <poll.h>
43-
#include <sys/socket.h>
44-
#include <unistd.h>
45-
#endif
4628
#include <memory>
4729

4830
namespace dpp {
@@ -81,7 +63,7 @@ struct DPP_EXPORT socket_engine_poll : public socket_engine_base {
8163
}
8264
}
8365

84-
int i = poll(out_set, static_cast<unsigned int>(poll_set.size()), poll_delay);
66+
int i = dpp::compat::poll(out_set, static_cast<unsigned int>(poll_set.size()), poll_delay);
8567
int processed = 0;
8668

8769
for (size_t index = 0; index < poll_set.size() && processed < i; index++) {

src/dpp/voice/enabled/discover_ip.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,9 @@
2222

2323
#include <dpp/exception.h>
2424
#include <dpp/discordvoiceclient.h>
25+
#include <dpp/compat.h>
2526
#include "enabled.h"
2627

27-
#ifdef _WIN32
28-
#include <WinSock2.h>
29-
#include <WS2tcpip.h>
30-
#include <io.h>
31-
#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
32-
#define pollfd WSAPOLLFD
33-
#else
34-
#include <poll.h>
35-
#include <netinet/in.h>
36-
#include <sys/socket.h>
37-
#endif
38-
3928
namespace dpp {
4029

4130
/**
@@ -164,7 +153,7 @@ std::string discord_voice_client::discover_ip() {
164153
pollfd pfd{};
165154
pfd.fd = socket.fd;
166155
pfd.events = POLLIN;
167-
int ret = ::poll(&pfd, 1, discovery_timeout);
156+
int ret = dpp::compat::poll(&pfd, 1, discovery_timeout);
168157
switch (ret) {
169158
case -1:
170159
log(ll_warning, "poll() error on IP discovery");

0 commit comments

Comments
 (0)