From 2c2b586d86d71bd6c134c32d27b155615230222e Mon Sep 17 00:00:00 2001 From: german Date: Tue, 17 Nov 2020 22:16:29 -0600 Subject: Add multiple udp server support --- src/input_common/udp/client.cpp | 143 ++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 49 deletions(-) (limited to 'src/input_common/udp/client.cpp') diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index c0bb90048..17a9225d7 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -136,15 +136,7 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); - for (std::size_t client = 0; client < clients.size(); client++) { - const auto pad = client % 4; - StartCommunication(client, Settings::values.udp_input_address, - Settings::values.udp_input_port, pad, 24872); - // Set motion parameters - // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode - // Real HW values are unknown, 0.0001 is an approximate to Standard - clients[client].motion.SetGyroThreshold(0.0001f); - } + ReloadSockets(); } Client::~Client() { @@ -167,26 +159,61 @@ std::vector Client::GetInputDevices() const { return devices; } -bool Client::DeviceConnected(std::size_t pad) const { +bool Client::DeviceConnected(std::size_t client) const { // Use last timestamp to detect if the socket has stopped sending data - const auto now = std::chrono::system_clock::now(); - const auto time_difference = static_cast( - std::chrono::duration_cast(now - clients[pad].last_motion_update) - .count()); - return time_difference < 1000 && clients[pad].active == 1; + const auto now = std::chrono::steady_clock::now(); + const auto time_difference = + static_cast(std::chrono::duration_cast( + now - clients[client].last_motion_update) + .count()); + return time_difference < 1000 && clients[client].active == 1; } -void Client::ReloadUDPClient() { - for (std::size_t client = 0; client < clients.size(); client++) { - ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); +void Client::ReloadSockets() { + Reset(); + + std::stringstream servers_ss(Settings::values.udp_input_servers); + std::string server_token; + std::size_t client = 0; + while (std::getline(servers_ss, server_token, ',')) { + if (client == max_udp_clients) { + break; + } + std::stringstream server_ss(server_token); + std::string token; + std::getline(server_ss, token, ':'); + std::string udp_input_address = token; + std::getline(server_ss, token, ':'); + char* temp; + const u16 udp_input_port = static_cast(std::strtol(token.c_str(), &temp, 0)); + if (*temp != '\0') { + LOG_ERROR(Input, "Port number is not valid {}", token); + continue; + } + + for (std::size_t pad = 0; pad < 4; ++pad) { + const std::size_t client_number = + GetClientNumber(udp_input_address, udp_input_port, pad); + if (client_number != max_udp_clients) { + LOG_ERROR(Input, "Duplicated UDP servers found"); + continue; + } + StartCommunication(client++, udp_input_address, udp_input_port, pad, 24872); + } } } -void Client::ReloadSocket(const std::string& host, u16 port, std::size_t pad_index, u32 client_id) { - // client number must be determined from host / port and pad index - const std::size_t client = pad_index; - clients[client].socket->Stop(); - clients[client].thread.join(); - StartCommunication(client, host, port, pad_index, client_id); + +std::size_t Client::GetClientNumber(std::string_view host, u16 port, std::size_t pad) const { + for (std::size_t client = 0; client < clients.size(); client++) { + if (clients[client].active == -1) { + continue; + } + if (clients[client].host == host && clients[client].port == port && + clients[client].pad_index == pad) { + return client; + } + } + return max_udp_clients; } void Client::OnVersion([[maybe_unused]] Response::Version data) { @@ -197,9 +224,7 @@ void Client::OnPortInfo([[maybe_unused]] Response::PortInfo data) { LOG_TRACE(Input, "PortInfo packet received: {}", data.model); } -void Client::OnPadData(Response::PadData data) { - // Client number must be determined from host / port and pad index - const std::size_t client = data.info.id; +void Client::OnPadData(Response::PadData data, std::size_t client) { LOG_TRACE(Input, "PadData packet received"); if (data.packet_counter == clients[client].packet_sequence) { LOG_WARNING( @@ -208,9 +233,9 @@ void Client::OnPadData(Response::PadData data) { clients[client].packet_sequence, data.packet_counter); return; } - clients[client].active = data.info.is_pad_active; + clients[client].active = static_cast(data.info.is_pad_active); clients[client].packet_sequence = data.packet_counter; - const auto now = std::chrono::system_clock::now(); + const auto now = std::chrono::steady_clock::now(); const auto time_difference = static_cast(std::chrono::duration_cast( now - clients[client].last_motion_update) @@ -264,16 +289,28 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16 std::size_t pad_index, u32 client_id) { SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, [this](Response::PortInfo info) { OnPortInfo(info); }, - [this](Response::PadData data) { OnPadData(data); }}; - LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); + [this, client](Response::PadData data) { OnPadData(data, client); }}; + LOG_INFO(Input, "Starting communication with UDP input server on {}:{}:{}", host, port, + pad_index); + clients[client].host = host; + clients[client].port = port; + clients[client].pad_index = pad_index; + clients[client].active = 0; clients[client].socket = std::make_unique(host, port, pad_index, client_id, callback); clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; + // Set motion parameters + // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode + // Real HW values are unknown, 0.0001 is an approximate to Standard + clients[client].motion.SetGyroThreshold(0.0001f); } void Client::Reset() { for (auto& client : clients) { - client.socket->Stop(); - client.thread.join(); + if (client.thread.joinable()) { + client.active = -1; + client.socket->Stop(); + client.thread.join(); + } } } @@ -283,52 +320,60 @@ void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3& a LOG_DEBUG(Input, "UDP Controller {}: gyro=({}, {}, {}), accel=({}, {}, {}), touch={}", client, gyro[0], gyro[1], gyro[2], acc[0], acc[1], acc[2], touch); } - UDPPadStatus pad; + UDPPadStatus pad{ + .host = clients[client].host, + .port = clients[client].port, + .pad_index = clients[client].pad_index, + }; if (touch) { pad.touch = PadTouch::Click; - pad_queue[client].Push(pad); + pad_queue.Push(pad); } for (size_t i = 0; i < 3; ++i) { if (gyro[i] > 5.0f || gyro[i] < -5.0f) { pad.motion = static_cast(i); pad.motion_value = gyro[i]; - pad_queue[client].Push(pad); + pad_queue.Push(pad); } if (acc[i] > 1.75f || acc[i] < -1.75f) { pad.motion = static_cast(i + 3); pad.motion_value = acc[i]; - pad_queue[client].Push(pad); + pad_queue.Push(pad); } } } void Client::BeginConfiguration() { - for (auto& pq : pad_queue) { - pq.Clear(); - } + pad_queue.Clear(); configuring = true; } void Client::EndConfiguration() { - for (auto& pq : pad_queue) { - pq.Clear(); - } + pad_queue.Clear(); configuring = false; } -DeviceStatus& Client::GetPadState(std::size_t pad) { - return clients[pad].status; +DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) { + const std::size_t client_number = GetClientNumber(host, port, pad); + if (client_number == max_udp_clients) { + return clients[0].status; + } + return clients[client_number].status; } -const DeviceStatus& Client::GetPadState(std::size_t pad) const { - return clients[pad].status; +const DeviceStatus& Client::GetPadState(const std::string& host, u16 port, std::size_t pad) const { + const std::size_t client_number = GetClientNumber(host, port, pad); + if (client_number == max_udp_clients) { + return clients[0].status; + } + return clients[client_number].status; } -std::array, 4>& Client::GetPadQueue() { +Common::SPSCQueue& Client::GetPadQueue() { return pad_queue; } -const std::array, 4>& Client::GetPadQueue() const { +const Common::SPSCQueue& Client::GetPadQueue() const { return pad_queue; } -- cgit v1.2.3