#pragma once #include "loader/module_loader.hpp" #include "game/demonware/stun_server.hpp" #include "game/demonware/service_server.hpp" #define TCP_BLOCKING true #define UDP_BLOCKING false namespace demonware { class dw final : public module { public: dw(); void post_load() override; void pre_destroy() override; template static std::shared_ptr register_server(Args ... args) { std::lock_guard _(server_mutex_); auto server = std::make_shared(args...); servers_[server->get_address()] = server; return server; } static std::shared_ptr register_stun_server(std::string name) { std::lock_guard _(server_mutex_); auto server = std::make_shared(name); stun_servers_[server->get_address()] = server; return server; } static int recv_datagam_packet(SOCKET s, char* buf, int len, sockaddr* from, int* fromlen); static void send_datagram_packet(SOCKET s, const std::string& data, const sockaddr* to, int tolen); static bool is_blocking_socket(SOCKET s, bool def); static void remove_blocking_socket(SOCKET s); static void set_blocking_socket(SOCKET s, bool blocking); static std::shared_ptr find_stun_server_by_name(const std::string& name); static std::shared_ptr find_stun_server_by_address(unsigned long address); static std::shared_ptr find_server_by_name(const std::string& name); static std::shared_ptr find_server_by_address(unsigned long address); static std::shared_ptr find_server_by_socket(SOCKET s); static bool link_socket(SOCKET sock, unsigned long address); static void unlink_socket(SOCKET sock); static void set_key(bool encrypt, uint8_t* key); static uint8_t* get_key(bool encrypt); private: static bool terminate_; static std::thread message_thread_; static std::recursive_mutex server_mutex_; static uint8_t encryption_key_[24]; static uint8_t decryption_key_[24]; static std::map blocking_sockets_; static std::map> socket_links_; static std::map> servers_; static std::map> stun_servers_; static std::map>> datagram_packets_; static void server_thread(); static void bd_logger_stub(int /*type*/, const char* const /*channelName*/, const char*, const char* const /*file*/, const char* const function, const unsigned int /*line*/, const char* const msg, ...); }; }