Some updates

This commit is contained in:
WolverinDEV 2020-03-09 18:29:28 +01:00
parent 7926a26091
commit 799df15ace
4 changed files with 146 additions and 146 deletions

View File

@ -1,5 +1,5 @@
// //
// Created by WolverinDEV on 07/03/2020. // Created by WolverinDEV on 07/03/2020.
// //
#include "UDPServer.h" #include "UDPServer.h"

View File

@ -1,107 +1,107 @@
#pragma once #pragma once
#include <thread> #include <thread>
#include <event.h> #include <event.h>
#include <vector> #include <vector>
#include <misc/spin_lock.h> #include <misc/spin_lock.h>
#include <Definitions.h> #include <Definitions.h>
#include <mutex> #include <mutex>
namespace ts::server { namespace ts::server {
class VoiceClient; class VoiceClient;
} }
namespace ts::server::server::udp { namespace ts::server::server::udp {
struct datagram_packet { struct datagram_packet {
union pktinfo_storage { union pktinfo_storage {
in_pktinfo v4; in_pktinfo v4;
in6_pktinfo v6; in6_pktinfo v6;
}; };
datagram_packet* next_packet; datagram_packet* next_packet;
sockaddr_storage address; sockaddr_storage address;
pktinfo_storage address_info; pktinfo_storage address_info;
size_t data_length; size_t data_length;
uint8_t data[0]; uint8_t data[0];
}; };
static_assert(std::is_trivially_destructible<datagram_packet>::value); static_assert(std::is_trivially_destructible<datagram_packet>::value);
static_assert(std::is_trivially_constructible<datagram_packet>::value); static_assert(std::is_trivially_constructible<datagram_packet>::value);
template <typename T, size_t N> template <typename T, size_t N>
struct write_ring_queue { struct write_ring_queue {
std::array<T, N> memory{}; std::array<T, N> memory{};
size_t current_index{0}; size_t current_index{0};
size_t filled_index{0}; size_t filled_index{0};
[[nodiscard]] constexpr inline auto max_size() const { return N; } [[nodiscard]] constexpr inline auto max_size() const { return N; }
[[nodiscard]] inline size_t current_size() const { return this->filled_index - this->current_index; } [[nodiscard]] inline size_t current_size() const { return this->filled_index - this->current_index; }
[[nodiscard]] inline bool pop_entry(T& result) { [[nodiscard]] inline bool pop_entry(T& result) {
if(this->current_index >= this->filled_index) return false; if(this->current_index >= this->filled_index) return false;
return this->memory[this->current_index++ % N]; return this->memory[this->current_index++ % N];
} }
[[nodiscard]] inline bool push_entry(T&& entry) { [[nodiscard]] inline bool push_entry(T&& entry) {
if(this->filled_index - this->current_index >= N) return false; if(this->filled_index - this->current_index >= N) return false;
this->memory[this->filled_index++ % N] = std::forward(entry); this->memory[this->filled_index++ % N] = std::forward(entry);
return true; return true;
} }
}; };
struct io_loop; struct io_loop;
struct io_loop_entry { struct io_loop_entry {
io_loop* io_loop{nullptr}; io_loop* io_loop{nullptr};
int file_descriptor{0}; int file_descriptor{0};
event* event_read{}; event* event_read{};
event* event_write{}; event* event_write{};
spin_lock write_queue_lock{}; spin_lock write_queue_lock{};
datagram_packet* dg_write_queue_head{nullptr}; datagram_packet* dg_write_queue_head{nullptr};
datagram_packet* dg_write_queue_tail{nullptr}; datagram_packet* dg_write_queue_tail{nullptr};
write_ring_queue<std::weak_ptr<VoiceClient>, 1024 * 8> voice_write_queue{}; write_ring_queue<std::weak_ptr<VoiceClient>, 1024 * 8> voice_write_queue{};
}; };
struct io_loop { struct io_loop {
std::thread base_dispatcher{}; std::thread base_dispatcher{};
struct event_base* event_base{nullptr}; struct event_base* event_base{nullptr};
std::mutex entries_mutex{}; std::mutex entries_mutex{};
std::vector<io_loop_entry*> registered_entries{}; std::vector<io_loop_entry*> registered_entries{};
}; };
struct io_binding { struct io_binding {
VirtualServerId server_id{0}; VirtualServerId server_id{0};
sockaddr_storage address{}; sockaddr_storage address{};
size_t loop_entry_index{0}; size_t loop_entry_index{0};
std::vector<io_loop_entry*> loop_entries{}; std::vector<io_loop_entry*> loop_entries{};
struct server_client { struct server_client {
std::shared_ptr<VoiceClient> client{}; std::shared_ptr<VoiceClient> client{};
ClientId client_id{0}; ClientId client_id{0};
}; };
std::mutex client_lock{}; std::mutex client_lock{};
std::deque<server_client> known_clients{}; std::deque<server_client> known_clients{};
}; };
class Server { class Server {
public: public:
void schedule_client_write(const std::shared_ptr<VoiceClient>& /* client */); void schedule_client_write(const std::shared_ptr<VoiceClient>& /* client */);
void unregister_client(const std::shared_ptr<VoiceClient>& /* client */); void unregister_client(const std::shared_ptr<VoiceClient>& /* client */);
private: private:
std::mutex io_lock{}; std::mutex io_lock{};
std::vector<io_loop*> io_loops{}; std::vector<io_loop*> io_loops{};
std::mutex bindings_lock{}; std::mutex bindings_lock{};
std::vector<io_binding*> io_bindings{}; std::vector<io_binding*> io_bindings{};
}; };
} }

View File

@ -1,5 +1,5 @@
// //
// Created by WolverinDEV on 07/03/2020. // Created by WolverinDEV on 07/03/2020.
// //
#include "VirtualServerBroadcastService.h" #include "VirtualServerBroadcastService.h"

View File

@ -1,31 +1,31 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <deque> #include <deque>
#include <Properties.h> #include <Properties.h>
namespace ts::server { namespace ts::server {
class ConnectedClient; class ConnectedClient;
} }
namespace ts::server::vserver { namespace ts::server::vserver {
class VirtualServerBase; class VirtualServerBase;
class BroadcastService { class BroadcastService {
public: public:
explicit BroadcastService(VirtualServerBase*); explicit BroadcastService(VirtualServerBase*);
bool client_updated(const std::shared_ptr<ConnectedClient>& /* client */, bool client_updated(const std::shared_ptr<ConnectedClient>& /* client */,
const std::deque<std::shared_ptr<property::PropertyDescription>>& /* keys */, bool /* notify_client */ = true); const std::deque<std::shared_ptr<property::PropertyDescription>>& /* keys */, bool /* notify_client */ = true);
inline bool client_updated(const std::shared_ptr<ConnectedClient>& client, const std::deque<property::ClientProperties>& keys, bool notify_client = true) { inline bool client_updated(const std::shared_ptr<ConnectedClient>& client, const std::deque<property::ClientProperties>& keys, bool notify_client = true) {
if(keys.empty()) return false; if(keys.empty()) return false;
std::deque<std::shared_ptr<property::PropertyDescription>> _keys{}; std::deque<std::shared_ptr<property::PropertyDescription>> _keys{};
for(const auto& key : keys) _keys.push_back(property::impl::info<property::ClientProperties>(key)); for(const auto& key : keys) _keys.push_back(property::impl::info<property::ClientProperties>(key));
return this->client_updated(client, _keys, notify_client); return this->client_updated(client, _keys, notify_client);
}; };
private: private:
VirtualServerBase* virtual_server_; VirtualServerBase* virtual_server_;
}; };
} }