diff options
Diffstat (limited to 'src/input_common/udp')
-rw-r--r-- | src/input_common/udp/client.cpp | 65 | ||||
-rw-r--r-- | src/input_common/udp/client.h | 14 | ||||
-rw-r--r-- | src/input_common/udp/udp.cpp | 28 |
3 files changed, 56 insertions, 51 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 9d0b9f31d..bb109562c 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -26,11 +26,11 @@ class Socket { public: using clock = std::chrono::system_clock; - explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, - SocketCallback callback) - : callback(std::move(callback)), timer(io_service), - socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id), - pad_index(pad_index) { + explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_, + SocketCallback callback_) + : callback(std::move(callback_)), timer(io_service), + socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_), + pad_index(pad_index_) { boost::system::error_code ec{}; auto ipv4 = boost::asio::ip::make_address_v4(host, ec); if (ec.value() != boost::system::errc::success) { @@ -93,13 +93,17 @@ private: void HandleSend(const boost::system::error_code& error) { boost::system::error_code _ignored{}; // Send a request for getting port info for the pad - Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; + const Request::PortInfo port_info{1, {static_cast<u8>(pad_index), 0, 0, 0}}; const auto port_message = Request::Create(port_info, client_id); std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); // Send a request for getting pad data for the pad - Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; + const Request::PadData pad_data{ + Request::PadData::Flags::Id, + static_cast<u8>(pad_index), + EMPTY_MAC_ADDRESS, + }; const auto pad_message = Request::Create(pad_data, client_id); std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); @@ -112,7 +116,7 @@ private: udp::socket socket; u32 client_id{}; - u8 pad_index{}; + std::size_t pad_index{}; static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); @@ -133,7 +137,7 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); for (std::size_t client = 0; client < clients.size(); client++) { - u8 pad = client % 4; + const auto pad = client % 4; StartCommunication(client, Settings::values.udp_input_address, Settings::values.udp_input_port, pad, 24872); // Set motion parameters @@ -166,9 +170,9 @@ std::vector<Common::ParamPackage> Client::GetInputDevices() const { bool Client::DeviceConnected(std::size_t pad) const { // Use last timestamp to detect if the socket has stopped sending data const auto now = std::chrono::system_clock::now(); - u64 time_difference = + const auto time_difference = static_cast<u64>( std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) - .count(); + .count()); return time_difference < 1000 && clients[pad].active == 1; } @@ -177,9 +181,9 @@ void Client::ReloadUDPClient() { ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); } } -void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { +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 - std::size_t client = 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); @@ -194,8 +198,8 @@ void Client::OnPortInfo(Response::PortInfo data) { } void Client::OnPadData(Response::PadData data) { - // client number must be determined from host / port and pad index - std::size_t client = data.info.id; + // Client number must be determined from host / port and pad index + const std::size_t client = data.info.id; LOG_TRACE(Input, "PadData packet received"); if (data.packet_counter == clients[client].packet_sequence) { LOG_WARNING( @@ -207,11 +211,12 @@ void Client::OnPadData(Response::PadData data) { clients[client].active = data.info.is_pad_active; clients[client].packet_sequence = data.packet_counter; const auto now = std::chrono::system_clock::now(); - u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>( - now - clients[client].last_motion_update) - .count(); + const auto time_difference = + static_cast<u64>(std::chrono::duration_cast<std::chrono::microseconds>( + now - clients[client].last_motion_update) + .count()); clients[client].last_motion_update = now; - Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; + const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); // Gyroscope values are not it the correct scale from better joy. // Dividing by 312 allows us to make one full turn = 1 turn @@ -237,9 +242,11 @@ void Client::OnPadData(Response::PadData data) { const u16 min_y = clients[client].status.touch_calibration->min_y; const u16 max_y = clients[client].status.touch_calibration->max_y; - x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / + x = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - + min_x) / static_cast<float>(max_x - min_x); - y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) / + y = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - + min_y) / static_cast<float>(max_y - min_y); } @@ -253,8 +260,8 @@ void Client::OnPadData(Response::PadData data) { } } -void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, - u32 client_id) { +void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, + 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); }}; @@ -264,9 +271,9 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16 } void Client::Reset() { - for (std::size_t client = 0; client < clients.size(); client++) { - clients[client].socket->Stop(); - clients[client].thread.join(); + for (auto& client : clients) { + client.socket->Stop(); + client.thread.join(); } } @@ -325,7 +332,7 @@ const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() cons return pad_queue; } -void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function<void()> success_callback, std::function<void()> failure_callback) { std::thread([=] { @@ -346,7 +353,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie } CalibrationConfigurationJob::CalibrationConfigurationJob( - const std::string& host, u16 port, u8 pad_index, u32 client_id, + const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function<void(Status)> status_callback, std::function<void(u16, u16, u16, u16)> data_callback) { @@ -366,7 +373,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( current_status = Status::Ready; status_callback(current_status); } - if (!data.touch_1.is_active) { + if (data.touch_1.is_active == 0) { return; } LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index 523dc6a7a..2491a03a2 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -84,8 +84,8 @@ public: bool DeviceConnected(std::size_t pad) const; void ReloadUDPClient(); - void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, - u32 client_id = 24872); + void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, + std::size_t pad_index = 0, u32 client_id = 24872); std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue(); const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const; @@ -99,7 +99,7 @@ private: DeviceStatus status; std::thread thread; u64 packet_sequence = 0; - u8 active; + u8 active = 0; // Realtime values // motion is initalized with PID values for drift correction on joycons @@ -113,8 +113,8 @@ private: void OnVersion(Response::Version); void OnPortInfo(Response::PortInfo); void OnPadData(Response::PadData); - void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, - u32 client_id); + void StartCommunication(std::size_t client, const std::string& host, u16 port, + std::size_t pad_index, u32 client_id); void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, const Common::Vec3<float>& gyro, bool touch); @@ -139,7 +139,7 @@ public: * @param status_callback Callback for job status updates * @param data_callback Called when calibration data is ready */ - explicit CalibrationConfigurationJob(const std::string& host, u16 port, u8 pad_index, + explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function<void(Status)> status_callback, std::function<void(u16, u16, u16, u16)> data_callback); ~CalibrationConfigurationJob(); @@ -149,7 +149,7 @@ private: Common::Event complete_event; }; -void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function<void()> success_callback, std::function<void()> failure_callback); diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index eba077a36..71a76a7aa 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp @@ -2,8 +2,6 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include <atomic> -#include <list> #include <mutex> #include <utility> #include "common/assert.h" @@ -15,8 +13,8 @@ namespace InputCommon { class UDPMotion final : public Input::MotionDevice { public: - UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) - : ip(ip_), port(port_), pad(pad_), client(client_) {} + explicit UDPMotion(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) + : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} Input::MotionStatus GetStatus() const override { return client->GetPadState(pad).motion_status; @@ -25,7 +23,7 @@ public: private: const std::string ip; const int port; - const int pad; + const u32 pad; CemuhookUDP::Client* client; mutable std::mutex mutex; }; @@ -40,11 +38,11 @@ UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_) * - "port": the nth jcpad on the adapter */ std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) { - const std::string ip = params.Get("ip", "127.0.0.1"); - const int port = params.Get("port", 26760); - const int pad = params.Get("pad_index", 0); + auto ip = params.Get("ip", "127.0.0.1"); + const auto port = params.Get("port", 26760); + const auto pad = static_cast<u32>(params.Get("pad_index", 0)); - return std::make_unique<UDPMotion>(ip, port, pad, client.get()); + return std::make_unique<UDPMotion>(std::move(ip), port, pad, client.get()); } void UDPMotionFactory::BeginConfiguration() { @@ -79,7 +77,7 @@ Common::ParamPackage UDPMotionFactory::GetNextInput() { class UDPTouch final : public Input::TouchDevice { public: - UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) + explicit UDPTouch(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} std::tuple<float, float, bool> GetStatus() const override { @@ -89,7 +87,7 @@ public: private: const std::string ip; const int port; - const int pad; + const u32 pad; CemuhookUDP::Client* client; mutable std::mutex mutex; }; @@ -104,11 +102,11 @@ UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_) * - "port": the nth jcpad on the adapter */ std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) { - const std::string ip = params.Get("ip", "127.0.0.1"); - const int port = params.Get("port", 26760); - const int pad = params.Get("pad_index", 0); + auto ip = params.Get("ip", "127.0.0.1"); + const auto port = params.Get("port", 26760); + const auto pad = static_cast<u32>(params.Get("pad_index", 0)); - return std::make_unique<UDPTouch>(ip, port, pad, client.get()); + return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get()); } void UDPTouchFactory::BeginConfiguration() { |