Skip to content

Commit 9b441e3

Browse files
committed
update ASIO to 1.32.0
1 parent fe589b2 commit 9b441e3

File tree

15 files changed

+1033
-159
lines changed

15 files changed

+1033
-159
lines changed

CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ if (NOT ONLY_SLEEPY_DISCORD)
99
option(AUTO_DOWNLOAD_LIBRARY "Automatically download sleepy discord standard config dependencies" ON )
1010
option(SLEEPY_VCPKG "VCPKG with Sleepy Discord" OFF)
1111
option(USE_CPR "Use CPR http library" ON )
12-
option(USE_WEBSOCKETPP "Use websocketpp websockets library" ON )
13-
option(USE_UWEBSOCKETS "Use uWebsockets websockets library" OFF)
14-
option(USE_ASIO "Use ASIO network and I/O library (Used for UDP)" OFF)
12+
#option(USE_WEBSOCKETPP "Use websocketpp websockets library" OFF)
13+
#option(USE_UWEBSOCKETS "Use uWebsockets websockets library" ON )
14+
option(USE_ASIO "Use ASIO network and I/O library (Used for UDP and TCP)" ON)
1515
option(USE_BOOST_ASIO "Same as USE_ASIO but for boost library" OFF)
1616
option(USE_LIBOPUS "Use Opus audio codec library" OFF)
1717
option(USE_LIBSODIUM "Use libsodium cryptography library" OFF)
1818
option(USE_ZLIB_NG "Use zlib-ng for data compression" OFF)
1919
option(USE_ZLIB "Use zlib for data compression" OFF)
2020
#option(USE_Boost_BEAST "Use Boost Beast for http library" OFF)
21+
#option(USE_IXWEBSOCKET "Use IXWebSocket for websockets" OFF)
2122
endif()
2223

2324
#Define a variable to use to check if this file has been executed
@@ -36,7 +37,7 @@ endif()
3637
if(USE_WEBSOCKETPP OR USE_UWEBSOCKETS)
3738
if(Boost_FOUND OR USE_BOOST_ASIO) #checks if already defined
3839
set(USE_BOOST_ASIO ON)
39-
else()
40+
elseif(USE_WEBSOCKETPP)
4041
set(USE_ASIO ON)
4142
endif()
4243
endif()
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
#pragma once
22

3-
#ifdef _WIN32
4-
#include <winsock2.h>
5-
#elif defined(unix) || defined(__unix__) || defined(__unix)
6-
#include <netinet/in.h>
7-
#endif
3+
#if !defined(NONEXISTENT_ASIO)
4+
#ifdef _WIN32
5+
#include <winsock2.h>
6+
#elif defined(unix) || defined(__unix__) || defined(__unix)
7+
#include <netinet/in.h>
8+
#endif
89

9-
#if !defined(SLEEPY_USE_BOOST) && !defined(EXISTENT_BOOST_ASIO)
10-
#define ASIO_STANDALONE
11-
#include "websocketpp_common.h"
12-
#ifdef NONEXISTENT_WEBSOCKETPP
13-
#include <asio.hpp>
14-
#ifdef NONEXISTENT_ASIO
15-
#undef ASIO_STANDALONE
16-
#define SLEEPY_USE_BOOST
10+
#if !defined(SLEEPY_USE_BOOST) && !defined(EXISTENT_BOOST_ASIO)
11+
#define ASIO_STANDALONE
12+
#include "websocketpp_common.h"
13+
#ifdef NONEXISTENT_WEBSOCKETPP
14+
#include <asio.hpp>
15+
#ifdef NONEXISTENT_ASIO
16+
#undef ASIO_STANDALONE
17+
#define SLEEPY_USE_BOOST
18+
#endif
19+
#else
20+
namespace asio {
21+
using namespace websocketpp::lib::asio;
22+
}
1723
#endif
18-
#else
19-
namespace asio {
20-
using namespace websocketpp::lib::asio;
21-
}
2224
#endif
23-
#endif
2425

25-
#if defined(SLEEPY_USE_BOOST) || defined(EXISTENT_BOOST_ASIO)
26-
#include <boost/asio.hpp>
27-
#ifndef NONEXISTENT_BOOST_ASIO
28-
#undef NONEXISTENT_ASIO
29-
namespace asio {
30-
using namespace boost::asio;
31-
using boost::system::error_code;
32-
}
33-
#ifdef ASIO_STANDALONE
34-
#undef ASIO_STANDALONE
26+
#if defined(SLEEPY_USE_BOOST) || defined(EXISTENT_BOOST_ASIO)
27+
#include <boost/asio.hpp>
28+
#ifndef NONEXISTENT_BOOST_ASIO
29+
#undef NONEXISTENT_ASIO
30+
namespace asio {
31+
using namespace boost::asio;
32+
using boost::system::error_code;
33+
}
34+
#ifdef ASIO_STANDALONE
35+
#undef ASIO_STANDALONE
36+
#endif
3537
#endif
3638
#endif
3739
#endif

include/sleepy_discord/asio_schedule.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ namespace SleepyDiscord {
1414
class ASIOBasedScheduleHandler : public GenericScheduleHandler {
1515
public:
1616
virtual ~ASIOBasedScheduleHandler() = default;
17-
virtual asio::io_service& getIOService() = 0;
17+
virtual asio::io_context& getIOContext() = 0;
1818
};
1919

2020
class ASIOScheduleHandler : public ASIOBasedScheduleHandler {
2121
public:
22-
ASIOScheduleHandler() {}
22+
ASIOScheduleHandler(std::shared_ptr<asio::io_context> _io):
23+
io(_io) {}
2324
virtual ~ASIOScheduleHandler() = default;
2425

25-
inline asio::io_service& getIOService() override {
26-
return io;
26+
inline asio::io_context& getIOContext() override {
27+
return *io;
2728
}
2829

2930
static void handleTimer(const asio::error_code &ec, std::function<void()>& code) {
@@ -32,20 +33,24 @@ namespace SleepyDiscord {
3233
}
3334
}
3435

35-
inline Timer schedule(TimedTask code, const time_t milliseconds) override {
36+
inline static Timer schedule(asio::io_context& io, TimedTask code, const time_t milliseconds) {
3637
auto timer = std::make_shared<asio::steady_timer>(io, asio::chrono::milliseconds(milliseconds));
3738
timer->async_wait(std::bind(&handleTimer, std::placeholders::_1, code));
3839
return Timer([timer]() {
3940
timer->cancel();
4041
});
4142
}
4243

44+
inline Timer schedule(TimedTask code, const time_t milliseconds) override {
45+
return ASIOScheduleHandler::schedule(*io, std::move(code), std::move(milliseconds));
46+
}
47+
4348
inline void run() {
44-
io.run();
49+
io->run();
4550
}
4651

4752
private:
48-
asio::io_service io;
53+
std::shared_ptr<asio::io_context> io;
4954
};
5055
#endif
5156
}

include/sleepy_discord/asio_udp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SleepyDiscord {
1111
public:
1212
//ASIOUDPClient();
1313
ASIOUDPClient(BaseDiscordClient& client);
14-
ASIOUDPClient(asio::io_service& service);
14+
ASIOUDPClient(asio::io_context& context);
1515
bool connect(const std::string& to , const uint16_t port) override;
1616
void send(
1717
const uint8_t* buffer,
@@ -20,7 +20,7 @@ namespace SleepyDiscord {
2020
) override;
2121
void receive(ReceiveHandler handler) override;
2222
private:
23-
asio::io_service* iOService;
23+
asio::io_context* iOContext;
2424
asio::ip::udp::socket uDPSocket;
2525
asio::ip::udp::resolver resolver;
2626
asio::ip::udp::endpoint endpoint;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#pragma once
2+
#include "client.h"
3+
#include "message_receiver.h"
4+
#include "asio_schedule.h"
5+
#include "asio_websocketconnection.h"
6+
7+
namespace SleepyDiscord {
8+
9+
class ASIOWebSocketDiscordClient : public BaseDiscordClient {
10+
public:
11+
ASIOWebSocketDiscordClient() = default;
12+
ASIOWebSocketDiscordClient(const std::string token, const char _n = 0)
13+
: ASIOWebSocketDiscordClient(std::make_shared<asio::io_context>(), token) {}
14+
ASIOWebSocketDiscordClient(std::shared_ptr<asio::io_context> _ioContext, const std::string token) :
15+
ioContext(_ioContext)
16+
{
17+
setup(_ioContext, token);
18+
}
19+
~ASIOWebSocketDiscordClient() {}
20+
21+
std::shared_ptr<asio::io_context> ioContext;
22+
void setup(std::shared_ptr<asio::io_context> _ioContext, const std::string token) {
23+
if (workGuard && workGuard->owns_work()) {
24+
onError(GENERAL_ERROR, "can't set io context while client has work");
25+
return;
26+
}
27+
28+
ioContext = _ioContext;
29+
setScheduleHandler<ASIOScheduleHandler>(ioContext);
30+
31+
start(token);
32+
}
33+
34+
void run() override {
35+
if (!ioContext) { // shouldn't happen but can if default constructor is used without setup
36+
onError(SleepyDiscord::GENERAL_ERROR, "Can't run Discord Client without async IO or Discord token.");
37+
return;
38+
}
39+
40+
BaseDiscordClient::connect();
41+
workGuard = std::make_shared<asio::executor_work_guard<asio::io_context::executor_type>>(asio::make_work_guard(*ioContext));
42+
ioContext->run();
43+
}
44+
45+
Timer schedule(TimedTask code, const time_t milliseconds) override {
46+
return ASIOScheduleHandler::schedule(*ioContext, std::move(code), milliseconds);
47+
}
48+
49+
void postTask(PostableTask code) override {
50+
asio::post(code);
51+
}
52+
protected:
53+
#include "standard_config_header.h"
54+
private:
55+
56+
std::shared_ptr<asio::executor_work_guard<asio::io_context::executor_type>> workGuard;
57+
58+
bool connect(const std::string& uri,
59+
GenericMessageReceiver& messageProcessor,
60+
WebsocketConnection& connection
61+
) override {
62+
// set up websocket events
63+
// The connection object will own the eventListener
64+
// we can capture messageProcessor because the messageProcessor outlives the connection object
65+
ASIOWebSocketConnection::EventReceiver eventListener;
66+
eventListener.setOnMessage([&messageProcessor](std::vector<uint8_t> data) { // copy stream to vector
67+
auto message = std::make_shared<std::string>(data.begin(), data.end()); // copy again to string, dumb but it's simple and memory safe. I'll fix it later, just a temp solution
68+
messageProcessor.processMessage(WebSocketMessage{ WebSocketMessage::text, *message, message });
69+
});
70+
eventListener.setOnClose([&messageProcessor](uint16_t code, std::string _) {
71+
messageProcessor.processCloseCode(static_cast<WebSocketMessage::OPCode>(code));
72+
});
73+
eventListener.setOnOpen([&messageProcessor]() {
74+
messageProcessor.initialize();
75+
});
76+
77+
auto sharedConnection = std::make_shared<ASIOWebSocketConnection>(std::move(eventListener));
78+
connection = sharedConnection;
79+
sharedConnection->connect(uri, ioContext);
80+
return true;
81+
}
82+
83+
void disconnect(unsigned int code, const std::string reason, WebsocketConnection& _connection) override {
84+
if (auto websocketConnection = _connection.lock()) {
85+
websocketConnection->disconnect(code, reason);
86+
}
87+
std::cout << "disconnected\n";
88+
}
89+
90+
void send(std::string message, WebsocketConnection& _connection) override {
91+
if (auto websocketConnection = _connection.lock()) {
92+
websocketConnection->send(message, WebSocketMessage::text, nullptr);
93+
}
94+
}
95+
96+
void stopClient() override {
97+
workGuard.reset();
98+
ioContext->stop();
99+
}
100+
};
101+
102+
typedef ASIOWebSocketDiscordClient DiscordClient;
103+
}

0 commit comments

Comments
 (0)