31#ifndef OPENSSL_NO_DEPRECATED
32 #define OPENSSL_NO_DEPRECATED
35#include <openssl/err.h>
36#include <openssl/ssl.h>
39 #pragma comment(lib, "Ws2_32.lib")
44 #define errno WSAGetLastError()
48 #define EWOULDBLOCK WSAEWOULDBLOCK
50 #define poll(fd_set, fd_count, timeout) WSAPoll(fd_set, fd_count, timeout)
51 #define pollfd WSAPOLLFD
52 #define close closesocket
53 #define SHUT_RDWR SD_BOTH
61 #include <netinet/tcp.h>
62 #include <netinet/in.h>
63 #include <sys/socket.h>
64 #include <sys/types.h>
70namespace DiscordCoreInternal {
73 #define SOCKET_ERROR (-1)
77 #define INVALID_SOCKET (~0)
81 using SOCKET = int32_t;
84 struct DiscordCoreAPI_Dll PollFDWrapper {
85 std::vector<size_t> indices{};
86 std::vector<pollfd> polls{};
90 struct DiscordCoreAPI_Dll WSADataWrapper {
91 struct DiscordCoreAPI_Dll WSADataDeleter {
92 void operator()(WSADATA* other);
98 std::unique_ptr<WSADATA, WSADataDeleter> ptr{ std::make_unique<WSADATA>().release(), WSADataDeleter{} };
102 struct DiscordCoreAPI_Dll SSL_CTXWrapper {
103 struct DiscordCoreAPI_Dll SSL_CTXDeleter {
104 void operator()(SSL_CTX* other);
107 SSL_CTXWrapper& operator=(SSL_CTX* other);
112 std::unique_ptr<SSL_CTX, SSL_CTXDeleter> ptr{
nullptr, SSL_CTXDeleter{} };
115 struct DiscordCoreAPI_Dll SSLWrapper {
116 struct DiscordCoreAPI_Dll SSLDeleter {
117 void operator()(SSL* other);
120 SSLWrapper& operator=(nullptr_t other);
122 SSLWrapper& operator=(SSL* other);
127 std::unique_ptr<SSL, SSLDeleter> ptr{
nullptr, SSLDeleter{} };
130 struct DiscordCoreAPI_Dll SOCKETWrapper {
131 struct DiscordCoreAPI_Dll SOCKETDeleter {
132 void operator()(SOCKET* other);
135 SOCKETWrapper& operator=(SOCKET other)
noexcept;
137 SOCKETWrapper(SOCKET other)
noexcept;
139 operator SOCKET*()
noexcept;
141 operator SOCKET() noexcept;
143 SOCKETWrapper() noexcept = default;
146 std::unique_ptr<SOCKET, SOCKETDeleter> ptr{ std::make_unique<SOCKET>(INVALID_SOCKET).release(), SOCKETDeleter{} };
149 struct DiscordCoreAPI_Dll addrinfoWrapper {
150 addrinfo* operator->();
152 operator addrinfo**();
154 operator addrinfo*();
158 addrinfo* ptr{ &this->value };
161 class DiscordCoreAPI_Dll SSLConnectionInterface {
163 virtual bool connect(
const std::string& baseUrl,
const uint16_t portNew,
bool doWePrintErrorMessages,
164 bool areWeAStandaloneSocket)
noexcept = 0;
166 virtual bool areWeStillConnected() noexcept = 0;
168 virtual
void disconnect() noexcept = 0;
170 static
bool initialize() noexcept;
172 virtual ~SSLConnectionInterface() noexcept = default;
175 static SSL_CTXWrapper context;
176 static std::mutex mutex;
178 SOCKETWrapper socket{};
182 enum class ProcessIOResult : uint8_t { No_Error = 0, Error = 1 };
184 class DiscordCoreAPI_Dll SSLDataInterface {
186 friend class HttpsClient;
188 virtual ProcessIOResult writeData(std::string_view dataToWrite,
bool priority)
noexcept = 0;
190 virtual std::string_view getInputBuffer() noexcept = 0;
192 virtual ~SSLDataInterface() noexcept = default;
195 const uint64_t maxBufferSize{ (1024 * 16) - 1 };
196 RingBuffer<char, 16> outputBuffer{};
197 RingBuffer<char, 64> inputBuffer{};
201 class DiscordCoreAPI_Dll TCPSSLClient :
public SSLDataInterface,
public SSLConnectionInterface {
203 virtual void handleBuffer() noexcept = 0;
205 bool connect(const std::
string& baseUrl, const uint16_t portNew,
bool doWePrintErrorMessages,
bool areWeAStandaloneSocket) noexcept;
207 static std::vector<TCPSSLClient*> processIO(std::unordered_map<uint32_t, std::unique_ptr<WebSocketClient>>& shardMap) noexcept;
209 ProcessIOResult writeData(std::string_view dataToWrite,
bool priority) noexcept;
211 ProcessIOResult processIO(int32_t msToWait) noexcept;
213 std::string_view getInputBuffer() noexcept;
215 bool areWeStillConnected() noexcept;
217 bool processWriteData() noexcept;
219 bool processReadData() noexcept;
221 int64_t getBytesRead() noexcept;
223 virtual ~TCPSSLClient() noexcept = default;
226 bool doWePrintErrorMessages{};
227 bool areWeAStandaloneSocket{};
230 enum class ProcessIOType { Both = 0, Read_Only = 1, Write_Only = 2 };
232 class DiscordCoreAPI_Dll UDPConnection {
238 bool connect(
const std::string& baseUrlNew, uint16_t portNew, std::stop_token token = std::stop_token{})
noexcept;
240 void writeData(std::basic_string_view<std::byte> dataToWrite)
noexcept;
242 std::basic_string_view<std::byte> getInputBuffer() noexcept;
244 ProcessIOResult processIO(ProcessIOType type) noexcept;
246 virtual
void handleAudioBuffer() noexcept = 0;
248 bool areWeStillConnected() noexcept;
250 bool processWriteData() noexcept;
252 bool processReadData() noexcept;
254 void disconnect() noexcept;
256 ~UDPConnection() noexcept;
259 const uint64_t maxBufferSize{ (1024 * 16) };
260 RingBuffer<std::byte, 16> outputBuffer{};
261 RingBuffer<std::byte, 16> inputBuffer{};
263 addrinfoWrapper address{};
264 bool doWePrintErrors{};
265 SOCKETWrapper socket{};
266 std::string baseUrl{};
StreamType
For selecting the type of streamer that the given bot is, one must be one server and one of client pe...
VoiceConnection class - represents the connection to a given voice Channel.