diff options
author | german <german@thesoftwareartisans.com> | 2020-11-18 05:16:29 +0100 |
---|---|---|
committer | german <german@thesoftwareartisans.com> | 2020-11-26 06:44:41 +0100 |
commit | 2c2b586d86d71bd6c134c32d27b155615230222e (patch) | |
tree | 8344f77a6a09879bc419dc62cfc3a10ad5632d82 /src/input_common/udp/client.cpp | |
parent | Merge pull request #4976 from comex/poll-events (diff) | |
download | yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar.gz yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar.bz2 yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar.lz yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar.xz yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.tar.zst yuzu-2c2b586d86d71bd6c134c32d27b155615230222e.zip |
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r-- | src/input_common/udp/client.cpp | 143 |
1 files changed, 94 insertions, 49 deletions
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<Common::ParamPackage> 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<u64>( - std::chrono::duration_cast<std::chrono::milliseconds>(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<u64>(std::chrono::duration_cast<std::chrono::milliseconds>( + 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<u16>(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<s8>(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<u64>(std::chrono::duration_cast<std::chrono::microseconds>( 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<Socket>(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<float>& 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<PadMotion>(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<PadMotion>(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<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() { +Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() { return pad_queue; } -const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const { +const Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() const { return pad_queue; } |