Remove code that no longer works after ASIO update

This commit is contained in:
Sleepy Flower
2025-01-31 17:24:53 -05:00
parent 9b441e3488
commit 93aa4fdcf8
15 changed files with 43 additions and 568 deletions

View File

@@ -9,8 +9,6 @@ if (NOT ONLY_SLEEPY_DISCORD)
option(AUTO_DOWNLOAD_LIBRARY "Automatically download sleepy discord standard config dependencies" ON )
option(SLEEPY_VCPKG "VCPKG with Sleepy Discord" OFF)
option(USE_CPR "Use CPR http library" ON )
#option(USE_WEBSOCKETPP "Use websocketpp websockets library" OFF)
#option(USE_UWEBSOCKETS "Use uWebsockets websockets library" ON )
option(USE_ASIO "Use ASIO network and I/O library (Used for UDP and TCP)" ON)
option(USE_BOOST_ASIO "Same as USE_ASIO but for boost library" OFF)
option(USE_LIBOPUS "Use Opus audio codec library" OFF)
@@ -34,14 +32,6 @@ if(ENABLE_VOICE)
set(USE_LIBSODIUM ON)
endif()
if(USE_WEBSOCKETPP OR USE_UWEBSOCKETS)
if(Boost_FOUND OR USE_BOOST_ASIO) #checks if already defined
set(USE_BOOST_ASIO ON)
elseif(USE_WEBSOCKETPP)
set(USE_ASIO ON)
endif()
endif()
if(USE_ZLIB_NG AND USE_ZLIB)
message(FATAL_ERROR "can't use zlib and zlib-ng at the same time")
endif()
@@ -118,8 +108,6 @@ if(USE_ASIO)
find_package(asio CONFIG REQUIRED)
else()
if(AUTO_DOWNLOAD_LIBRARY)
# Note websocketpp doesn't work with the latest version of asio
# Remember to go back to the master version after they are fixed
download_project(
PROJ asio
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
@@ -135,54 +123,6 @@ if(USE_ASIO)
endif()
endif()
if(USE_WEBSOCKETPP)
if (SLEEPY_VCPKG)
find_package(websocketpp CONFIG REQUIRED)
else()
if(AUTO_DOWNLOAD_LIBRARY)
download_project(
PROJ websocketpp
GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
GIT_TAG master
SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/websocketpp
UPDATE_DISCONNECTED 1
)
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_subdirectory(deps/websocketpp)
if(NOT DEFINED websocketpp_SOURCE_DIR)
set(websocketpp_SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/websocketpp)
endif()
endif()
endif()
if(USE_UWEBSOCKETS)
if(SLEEPY_VCPKG)
find_path(UWEBSOCKETS_INCLUDE_DIRS "uwebsockets/App.h")
set(uwebsockets_SOURCE_DIR "${UWEBSOCKETS_INCLUDE_DIRS}/..")
else()
if(AUTO_DOWNLOAD_LIBRARY)
#to do install zlib
download_project(
PROJ uwebsockets
GIT_REPOSITORY https://github.com/uNetworking/uWebSockets.git
GIT_TAG master
SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/uwebsockets
UPDATE_DISCONNECTED 1
BUILD_COMMAND ${MAKE}
)
set(UWEBSOCKETS_INCLUDE_DIRS "${uwebsockets_SOURCE_DIR}/include")
endif()
if(NOT DEFINED uwebsockets_SOURCE_DIR)
set(uwebsockets_SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/uwebsockets)
endif()
endif()
endif()
if(USE_LIBOPUS)
if(SLEEPY_VCPKG)
find_package(Opus CONFIG REQUIRED)
@@ -268,6 +208,27 @@ if (USE_BEAST)
add_subdirectory(deps/beast)
endif()
if (USE_IXWEBSOCKET)
if (SLEEPY_VCPKG)
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket/IXWebSocket.h)
find_library(IXWEBSOCKET_LIBRARY ixwebsocket)
else()
if (AUTO_DOWNLOAD_LIBRARY)
download_project(
PROJ IXWebSocket
GIT_REPOSITORY https://github.com/machinezone/IXWebSocket.git
GIT_TAG master
SOURCE_DIR ${PROJECT_SOURCE_DIR}/deps/ixwebsocket
UPDATE_DISCONNECTED 1
)
endif()
add_subdirectory(deps/ixwebsocket)
set(IXWEBSOCKET_LIBRARY ixwebsocket)
set(IXWEBSOCKET_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/deps/ixwebsocket/ixwebsocket)
endif()
endif ()
# Get Version Info
# Not needed for vcpkg
if(NOT SLEEPY_VCPKG)

View File

@@ -58,9 +58,7 @@ Sleepy Discord doesn't require you to use any libraries, so that you can use any
* [OpenSSL](https://www.openssl.org/)
* [cpr](https://github.com/whoshuu/cpr)
* [Websocket++](https://github.com/zaphoyd/websocketpp)
or
[uWebSockets](https://github.com/uWebSockets/uWebSockets)
* [ASIO](https://think-async.com/Asio/)
# Develop Branch
For the cutting edge of Sleepy Discord, check out the develop branch. Please use the develop branch with caution because it may not even be able to compile or it is 100% not tested at all. Other then that, the branch is used for code that may not work. Also check to see if it's not behind the master branch, unless you want to use an older version of Sleepy Discord.

View File

@@ -1,2 +0,0 @@
#pragma once
#define NONEXISTENT_UWEBSOCKETS

View File

@@ -1,2 +0,0 @@
#pragma once
#define NONEXISTENT_WEBSOCKETPP

View File

@@ -9,19 +9,12 @@
#if !defined(SLEEPY_USE_BOOST) && !defined(EXISTENT_BOOST_ASIO)
#define ASIO_STANDALONE
#include "websocketpp_common.h"
#ifdef NONEXISTENT_WEBSOCKETPP
#include <asio.hpp>
#ifdef NONEXISTENT_ASIO
#undef ASIO_STANDALONE
#define SLEEPY_USE_BOOST
#endif
#else
namespace asio {
using namespace websocketpp::lib::asio;
}
#endif
#endif
#if defined(SLEEPY_USE_BOOST) || defined(EXISTENT_BOOST_ASIO)
#include <boost/asio.hpp>

View File

@@ -9,27 +9,17 @@ typedef BaseDiscordClient DiscordClient;\
#include "client.h"
SLEEPY_DEFINE_CUSTOM_CLIENT
#elif defined(SLEEPY_DISCORD_CMAKE)
#if defined(EXISTENT_WEBSOCKETPP)
#include "websocketpp_websocket.h"
#elif defined(EXISTENT_UWEBSOCKETS)
#include "uwebsockets_websocket.h"
#elif defined(EXISTENT_ASIO)
#if defined(EXISTENT_ASIO)
#include "asio_websocket.h"
#else
#include "client.h"
SLEEPY_DEFINE_CUSTOM_CLIENT
#endif
#else
#include "websocketpp_websocket.h"
#ifdef NONEXISTENT_WEBSOCKETPP
#include "uwebsockets_websocket.h"
#ifdef NONEXISTENT_UWEBSOCKETS
#include "asio_websocket.h"
#ifdef NONEXISTENT_ASIO
#include "client.h"
SLEEPY_DEFINE_CUSTOM_CLIENT
#endif
#endif
#include "asio_websocket.h"
#ifdef NONEXISTENT_ASIO
#include "client.h"
SLEEPY_DEFINE_CUSTOM_CLIENT
#endif
#endif

View File

@@ -1,8 +0,0 @@
#pragma once
#include <uWS/uWS.h>
#ifndef NONEXISTENT_UWEBSOCKETS
namespace SleepyDiscord {
typedef uWS::WebSocket<uWS::CLIENT>* WebsocketConnection;
}
#endif

View File

@@ -1,37 +0,0 @@
#pragma once
#if !defined(NONEXISTENT_UWEBSOCKETS) | defined(EXISTENT_UWEBSOCKETS)
#include <uWS/uWS.h>
#endif
#ifndef NONEXISTENT_UWEBSOCKETS
#include <thread>
#define SLEEPY_UWEBSOCKETS 563146 //random number that's only used once in the code
#define SLEEPY_LOCK_EXISTENT_TO SLEEPY_UWEBSOCKETS //stops the compiler from defining things from other libraries
#include "client.h"
namespace SleepyDiscord {
class UWebSocketsDiscordClient : public BaseDiscordClient {
public:
UWebSocketsDiscordClient() : maxNumOfThreads(0) {}
UWebSocketsDiscordClient(const std::string token, const char numOfThreads = 3);
~UWebSocketsDiscordClient();
void run();
Timer schedule(std::function<void()> code, const time_t milliseconds);
protected:
#include "standard_config_header.h"
private:
uWS::Hub hub;
bool connect(const std::string & uri, GenericMessageReceiver* messageProcessor, WebsocketConnection* connection);
void disconnect(unsigned int code, const std::string reason, WebsocketConnection* connection);
void send(std::string message, WebsocketConnection* connection);
void runAsync();
std::thread thread;
const char maxNumOfThreads;
bool isConnectionBad = false;
SLEEPY_LOCK_CLIENT_FUNCTIONS
};
typedef UWebSocketsDiscordClient DiscordClient;
}
#undef SLEEPY_UWEBSOCKETS
#undef SLEEPY_LOCK_TO_UWEBSOCKETS
#endif

View File

@@ -14,11 +14,7 @@
#endif
#elif defined(SLEEPY_DISCORD_CMAKE)
#if defined(EXISTENT_WEBSOCKETPP)
#include "websocketpp_connection.h"
#elif defined(EXISTENT_UWEBSOCKETS)
#include "uwebsockets_connection.h"
#elif defined(EXISTENT_ASIO)
#if defined(EXISTENT_ASIO)
#include "asio_websocketconnection.h"
#else
#include "custom_connection.h"
@@ -27,21 +23,11 @@
//defaults
#else
#ifndef SLEEPY_LOCK_EXISTENT_TO
#include "websocketpp_connection.h"
#ifdef NONEXISTENT_WEBSOCKETPP
#include "uwebsockets_connection.h"
#ifdef NONEXISTENT_UWEBSOCKETS
#include "asio_websocketconnection.h"
#ifdef NONEXISTENT_ASIO
//last resort
#include "custom_connection.h"
#endif
#endif
#include "asio_websocketconnection.h"
#ifdef NONEXISTENT_ASIO
//last resort
#include "custom_connection.h"
#endif
#elif SLEEPY_LOCK_EXISTENT_TO == SLEEPY_WEBSOCKETPP
#include "websocketpp_connection.h"
#elif SLEEPY_LOCK_EXISTENT_TO == SLEEPY_UWEBSOCKETS
#include "uwebsockets_connection.h"
#elif SLEEPY_LOCK_EXISTENT_TO == SLEEPY_ASIO
#include "asio_websocketconnection.h"
#else

View File

@@ -1,13 +0,0 @@
#pragma once
#ifndef BOOST_VERSION
#ifndef EXISTENT_BOOST_ASIO
#define ASIO_STANDALONE
#endif
#define _WEBSOCKETPP_CPP11_STL_
#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_
#define _WEBSOCKETPP_CPP11_TYPE_TRAITS_
#endif // !BOOST_VERSION
#ifndef NONEXISTENT_WEBSOCKETPP
#include <websocketpp/config/asio_client.hpp>
#endif

View File

@@ -1,8 +0,0 @@
#pragma once
#include "websocketpp_common.h"
namespace SleepyDiscord {
#ifndef NONEXISTENT_WEBSOCKETPP
typedef websocketpp::connection_hdl WebsocketConnection;
#endif
}

View File

@@ -1,83 +0,0 @@
#pragma once
#include "websocketpp_common.h"
#ifndef NONEXISTENT_WEBSOCKETPP
#include <chrono>
//#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include "client.h"
#include "websocketpp_connection.h"
#include "asio_schedule.h"
#include "asio_udp.h"
typedef websocketpp::client<websocketpp::config::asio_tls_client> _client;
namespace SleepyDiscord {
//typedef GenericMessageReceiver MessageProcssor;
class WebsocketppScheduleHandler : public ASIOBasedScheduleHandler {
public:
WebsocketppScheduleHandler(_client& c) : client(c) {}
~WebsocketppScheduleHandler() = default;
Timer schedule(TimedTask code, const time_t milliseconds) override;
inline websocketpp::lib::asio::io_service& getIOService() override {
return client.get_io_service();
}
private:
_client& client;
};
class WebsocketppDiscordClient : public BaseDiscordClient {
public:
WebsocketppDiscordClient() = default;
WebsocketppDiscordClient(const std::string token, const char numOfThreads = SleepyDiscord::DEFAULT_THREADS);
~WebsocketppDiscordClient();
using TimerPointer = std::weak_ptr<websocketpp::lib::asio::steady_timer>;
void run() override;
Timer schedule(TimedTask code, const time_t milliseconds) override;
void postTask(PostableTask code) override {
asio::post(code);
}
//UDPClient createUDPClient() /* override*/;
protected:
#include "standard_config_header.h"
private:
void init();
bool connect(const std::string & uri,
GenericMessageReceiver* messageProcessor,
WebsocketConnection& connection
) override;
void disconnect(unsigned int code, const std::string reason, WebsocketConnection& connection) override;
void onClose(
websocketpp::connection_hdl handle,
GenericMessageReceiver* messageProcessor
);
void onFail(websocketpp::connection_hdl handle, GenericMessageReceiver* messageProcessor);
void send(std::string message, WebsocketConnection& connection) override;
void runAsync() override;
void onOpen(websocketpp::connection_hdl hdl, GenericMessageReceiver* messageProcessor);
void onMessage(
websocketpp::connection_hdl hdl,
websocketpp::config::asio_client::message_type::ptr msg,
GenericMessageReceiver* messageProcessor
);
void stopClient() override {
this_client.stop_perpetual();
this_client.stop();
}
_client this_client;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> _thread;
websocketpp::connection_hdl handle;
};
typedef WebsocketppDiscordClient DiscordClient;
}
#else
#ifndef BOOST_VERSION
#undef ASIO_STANDALONE
#undef _WEBSOCKETPP_CPP11_RANDOM_DEVICE_
#undef _WEBSOCKETPP_CPP11_TYPE_TRAITS_
#endif
#endif

View File

@@ -17,11 +17,10 @@ add_library(sleepy-discord STATIC
server.cpp
slash_commands.cpp
user.cpp
uwebsockets_websocket.cpp
voice.cpp
voice_connection.cpp
webhook.cpp
websocketpp_websocket.cpp
zlib_compression.cpp
stage_instance.cpp
"thread.cpp"
@@ -98,37 +97,6 @@ if (NOT ONLY_SLEEPY_DISCORD)
list(APPEND LIB_CONFIG "NONEXISTENT_ASIO")
endif()
if(USE_WEBSOCKETPP)
if(SLEEPY_VCPKG)
list(APPEND LIBRARIES_TO_LINK "websocketpp::websocketpp")
else()
list(APPEND LIBRARIES_INCLUDE_DIRS "${websocketpp_SOURCE_DIR}")
endif()
find_package(OpenSSL REQUIRED)
list(APPEND REQUIRED_PACKAGES "OpenSSL")
list(APPEND LIBRARIES_TO_LINK "OpenSSL::SSL" "OpenSSL::Crypto")
if (UNIX)
find_package(Threads REQUIRED)
list(APPEND REQUIRED_PACKAGES "Threads")
list(APPEND LIBRARIES_TO_LINK "Threads::Threads")
endif()
list(APPEND REQUIRED_PACKAGES "websocketpp")
list(APPEND LIB_CONFIG "EXISTENT_WEBSOCKETPP")
else()
list(APPEND LIB_CONFIG "NONEXISTENT_WEBSOCKETPP")
endif()
if(USE_UWEBSOCKETS)
find_library(LIB_UWS uWS
PATHS ${uwebsockets_SOURCE_DIR}/lib
)
list(APPEND LIBRARIES_INCLUDE_DIRS "${UWEBSOCKETS_INCLUDE_DIRS}")
list(APPEND LIBRARIES_TO_LINK "${LIB_UWS}")
list(APPEND LIB_CONFIG "EXISTENT_UWEBSOCKETS")
else()
list(APPEND LIB_CONFIG "NONEXISTENT_UWEBSOCKETS")
endif()
if(USE_LIBOPUS)
if(SLEEPY_VCPKG)
list(APPEND REQUIRED_PACKAGES "Opus")
@@ -203,6 +171,16 @@ if (NOT ONLY_SLEEPY_DISCORD)
list(APPEND LIB_CONFIG "NONEXISTENT_BEAST")
endif()
if (USE_IXWEBSOCKET)
list(APPEND LIBRARIES_TO_LINK "${IXWEBSOCKET_LIBRARY}")
if(SLEEPY_VCPKG)
list(APPEND LIBRARIES_INCLUDE_DIRS "${IXWEBSOCKET_INCLUDE_DIR}")
endif()
list(APPEND LIB_CONFIG "EXISTENT_IXWEBSOCKET")
else()
list(APPEND LIB_CONFIG "NONEXISTENT_IXWEBSOCKET")
endif()
target_link_libraries(sleepy-discord PUBLIC ${LIBRARIES_TO_LINK})
target_include_directories(sleepy-discord PUBLIC ${LIBRARIES_INCLUDE_DIRS})
else()
@@ -210,8 +188,6 @@ else()
"NONEXISTENT_CPR"
"NONEXISTENT_ASIO"
"NONEXISTENT_BOOST_ASIO"
"NONEXISTENT_WEBSOCKETPP"
"NONEXISTENT_UWEBSOCKETS"
"NONEXISTENT_OPUS"
"NONEXISTENT_SODIUM")
endif()

View File

@@ -1,80 +0,0 @@
#include "uwebsockets_websocket.h"
#ifndef NONEXISTENT_UWEBSOCKETS
#include <iostream>
namespace SleepyDiscord {
UWebSocketsDiscordClient::~UWebSocketsDiscordClient() {
thread.join();
}
UWebSocketsDiscordClient::UWebSocketsDiscordClient(const std::string token, const char numOfThreads) :
maxNumOfThreads(numOfThreads) {
hub.onConnection([=](uWS::WebSocket<uWS::CLIENT>* ws, uWS::HttpRequest req) {
auto connection = static_cast<uWS::WebSocket<uWS::CLIENT>**>(ws->getUserData());
*connection = ws;
});
hub.onMessage([=](uWS::WebSocket<uWS::CLIENT>* ws, char * message, size_t length, uWS::OpCode opCode) {
processMessage(message);
});
hub.onError([=](void *user) {
isConnectionBad = true;
});
start(token, numOfThreads);
}
bool UWebSocketsDiscordClient::connect(const std::string & uri, GenericMessageReceiver* messageProcessor, WebsocketConnection* connection) {
isConnectionBad = false;
hub.connect(uri, connection);
return !isConnectionBad;
}
void UWebSocketsDiscordClient::run() {
hub.run();
}
Timer UWebSocketsDiscordClient::schedule(std::function<void()> code, const time_t milliseconds) {
uS::Timer *timer = new uS::Timer(hub.getLoop());
timer->setData(&code);
timer->start([](uS::Timer *timer) {
(*static_cast<std::function<void()>*>(timer->getData()))();
timer->close();
}, milliseconds, 0);
return Timer(
[timer]() {
timer->stop();
timer->close();
}
);
}
void UWebSocketsDiscordClient::runAsync() {
thread = std::thread([this]() { hub.run(); });
}
void UWebSocketsDiscordClient::disconnect(unsigned int code, const std::string reason, WebsocketConnection* connection) {
/*the static cast should do a compile time check that WebsocketConnection
is the correct type*/
static_cast<uWS::WebSocket<uWS::CLIENT>*>(*connection)->close();
}
void UWebSocketsDiscordClient::send(std::string message, WebsocketConnection* connection) {
//if disconnect doesn't give an error then this should also be fine
(*connection)->send(message.c_str());
}
#include "standard_config.h"
/* list of needed librarys to compile this
uWS.lib
libuv.lib
iphlpapi.lib
psapi.lib
userenv.lib
zlibstat.lib
*/
}
#endif

View File

@@ -1,196 +0,0 @@
#include "websocketpp_websocket.h"
#ifndef NONEXISTENT_WEBSOCKETPP
#include <future>
namespace SleepyDiscord {
void handleTimer(const websocketpp::lib::error_code &ec, std::function<void()>& code) {
if (ec != websocketpp::transport::error::operation_aborted) {
code();
}
}
Timer WebsocketppScheduleHandler::schedule(TimedTask code, const time_t milliseconds) {
auto timer = client.set_timer(
static_cast<long>(milliseconds),
websocketpp::lib::bind(&handleTimer, websocketpp::lib::placeholders::_1, code)
);
return Timer([timer]() {
timer->cancel();
});
}
WebsocketppDiscordClient::WebsocketppDiscordClient(const std::string token, const char numOfThreads) :
_thread(nullptr)
{
init();
setScheduleHandler<WebsocketppScheduleHandler>(this_client);
start(token, numOfThreads);
}
WebsocketppDiscordClient::~WebsocketppDiscordClient() {
if (_thread != nullptr && _thread->joinable()) _thread->join();
else _thread.reset();
}
void WebsocketppDiscordClient::init() {
// set up access channels to only log interesting things
this_client.clear_access_channels(websocketpp::log::alevel::all);
this_client.set_access_channels(websocketpp::log::alevel::connect);
this_client.set_access_channels(websocketpp::log::alevel::disconnect);
this_client.set_access_channels(websocketpp::log::alevel::app);
this_client.set_tls_init_handler([](websocketpp::connection_hdl) {
return websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::tls);
});
// Initialize the Asio transport policy
this_client.init_asio();
this_client.start_perpetual();
}
bool WebsocketppDiscordClient::connect(const std::string & uri,
GenericMessageReceiver* messageProcessor,
WebsocketConnection& _connection
) {
// Create a new connection to the given URI
websocketpp::lib::error_code ec;
// Note: there's might be a memory leak caused by get_connection
_client::connection_ptr con = this_client.get_connection(uri, ec);
if (ec) {
onError(GENERAL_ERROR, "Connect initialization: " + ec.message());
return false;
}
con->set_open_handler(std::bind(&WebsocketppDiscordClient::onOpen, this,
websocketpp::lib::placeholders::_1, messageProcessor
));
con->set_close_handler(std::bind(&WebsocketppDiscordClient::onClose, this,
websocketpp::lib::placeholders::_1, messageProcessor
));
con->set_message_handler(std::bind(&WebsocketppDiscordClient::onMessage, this,
websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2,
messageProcessor
));
con->set_fail_handler(std::bind(&WebsocketppDiscordClient::onFail, this,
websocketpp::lib::placeholders::_1, messageProcessor
));
#ifdef SLEEPY_WEBSCOKETPP_PRINTALL
this_client.set_access_channels(websocketpp::log::alevel::all);
this_client.set_error_channels(websocketpp::log::alevel::all);
#endif
if (_connection.expired() == false)
_connection.reset();
_connection = con->get_handle();
// Queue the connection. No DNS queries or network connections will be
// made until the io_service event loop is run.
this_client.connect(con);
return true;
}
void WebsocketppDiscordClient::run() {
BaseDiscordClient::connect();
this_client.run();
}
void handleTimers(const websocketpp::lib::error_code &ec, std::function<void()>& code, _client::timer_ptr timer) {
if (ec != websocketpp::transport::error::operation_aborted) {
code();
}
}
template<class Client>
void rerunTimer(_client::timer_ptr timer, TimedTask code, Client& c) {
timer->async_wait(
std::bind(
&_client::type::handle_timer,
c.this_client,
timer,
std::bind(
&handleTimers, websocketpp::lib::placeholders::_1, code, timer),
websocketpp::lib::placeholders::_1
)
);
}
Timer WebsocketppDiscordClient::schedule(TimedTask code, const time_t milliseconds) {
_client::timer_ptr timer;
auto callback = std::bind(
&handleTimers, websocketpp::lib::placeholders::_1, code, timer);
timer = this_client.set_timer(
static_cast<long>(milliseconds),
callback
);
return Timer(
[timer]() {
timer->cancel();
}
);
}
void WebsocketppDiscordClient::runAsync() {
if (!_thread) _thread.reset(new websocketpp::lib::thread(&WebsocketppDiscordClient::run, this));
}
void WebsocketppDiscordClient::onFail(websocketpp::connection_hdl _handle, GenericMessageReceiver* messageProcessor) {
messageProcessor->handleFailToConnect();
}
void WebsocketppDiscordClient::send(std::string message, WebsocketConnection& _connection) {
websocketpp::lib::error_code error;
this_client.send(_connection, message, websocketpp::frame::opcode::text, error);
//temp solution: ingnore all errors
//Besides the library can detect bad connections by itself anyway
}
void WebsocketppDiscordClient::onOpen(websocketpp::connection_hdl hdl,
GenericMessageReceiver* messageProcessor) {
initialize(messageProcessor);
}
void WebsocketppDiscordClient::onMessage(
websocketpp::connection_hdl hdl,
websocketpp::config::asio_client::message_type::ptr msg,
GenericMessageReceiver* messageProcessor) {
messageProcessor->processMessage(WebSocketMessage{
static_cast<WebSocketMessage::OPCodeType>(msg->get_opcode()),
msg->get_payload(),
msg
});
}
//UDPClient WebsocketppDiscordClient::createUDPClient() {
// return UDPClient(this_client.get_io_service());
//}
void WebsocketppDiscordClient::disconnect(
unsigned int code,
const std::string reason,
WebsocketConnection& _connection
) {
if (!_connection.expired()) {
websocketpp::lib::error_code error;
this_client.close(_connection, static_cast<websocketpp::close::status::value>(code), reason, error);
//temp fix ignore errors
}
std::cout << "disconnect\n";
}
void WebsocketppDiscordClient::onClose(websocketpp::connection_hdl _handle,
GenericMessageReceiver* messageProcessor) {
_client::connection_ptr con = this_client.get_con_from_hdl(_handle);
const int16_t closeCode = con->get_remote_close_code();
std::cout << "Close " << closeCode << ' ' << con->get_remote_close_reason() << '\n';
messageProcessor->processCloseCode(closeCode);
}
#include "standard_config.h"
}
#endif