diff options
Diffstat (limited to 'src/input_common/udp/client.cpp')
| -rw-r--r-- | src/input_common/udp/client.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index c4afa4174..df73f9ff7 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #include <chrono> | 5 | #include <chrono> |
| 6 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include <functional> | 7 | #include <functional> |
| 8 | #include <random> | ||
| 8 | #include <thread> | 9 | #include <thread> |
| 9 | #include <boost/asio.hpp> | 10 | #include <boost/asio.hpp> |
| 10 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| @@ -26,10 +27,10 @@ class Socket { | |||
| 26 | public: | 27 | public: |
| 27 | using clock = std::chrono::system_clock; | 28 | using clock = std::chrono::system_clock; |
| 28 | 29 | ||
| 29 | explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_, | 30 | explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, |
| 30 | SocketCallback callback_) | 31 | SocketCallback callback_) |
| 31 | : callback(std::move(callback_)), timer(io_service), | 32 | : callback(std::move(callback_)), timer(io_service), |
| 32 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_), | 33 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()), |
| 33 | pad_index(pad_index_) { | 34 | pad_index(pad_index_) { |
| 34 | boost::system::error_code ec{}; | 35 | boost::system::error_code ec{}; |
| 35 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); | 36 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); |
| @@ -63,6 +64,11 @@ public: | |||
| 63 | } | 64 | } |
| 64 | 65 | ||
| 65 | private: | 66 | private: |
| 67 | u32 GenerateRandomClientId() const { | ||
| 68 | std::random_device device; | ||
| 69 | return device(); | ||
| 70 | } | ||
| 71 | |||
| 66 | void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) { | 72 | void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) { |
| 67 | if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { | 73 | if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { |
| 68 | switch (*type) { | 74 | switch (*type) { |
| @@ -115,7 +121,7 @@ private: | |||
| 115 | boost::asio::basic_waitable_timer<clock> timer; | 121 | boost::asio::basic_waitable_timer<clock> timer; |
| 116 | udp::socket socket; | 122 | udp::socket socket; |
| 117 | 123 | ||
| 118 | u32 client_id{}; | 124 | const u32 client_id; |
| 119 | std::size_t pad_index{}; | 125 | std::size_t pad_index{}; |
| 120 | 126 | ||
| 121 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); | 127 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); |
| @@ -203,7 +209,7 @@ void Client::ReloadSockets() { | |||
| 203 | LOG_ERROR(Input, "Duplicated UDP servers found"); | 209 | LOG_ERROR(Input, "Duplicated UDP servers found"); |
| 204 | continue; | 210 | continue; |
| 205 | } | 211 | } |
| 206 | StartCommunication(client++, udp_input_address, udp_input_port, pad, 24872); | 212 | StartCommunication(client++, udp_input_address, udp_input_port, pad); |
| 207 | } | 213 | } |
| 208 | } | 214 | } |
| 209 | } | 215 | } |
| @@ -277,7 +283,7 @@ void Client::OnPadData(Response::PadData data, std::size_t client) { | |||
| 277 | } | 283 | } |
| 278 | 284 | ||
| 279 | void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, | 285 | void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, |
| 280 | std::size_t pad_index, u32 client_id) { | 286 | std::size_t pad_index) { |
| 281 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | 287 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, |
| 282 | [this](Response::PortInfo info) { OnPortInfo(info); }, | 288 | [this](Response::PortInfo info) { OnPortInfo(info); }, |
| 283 | [this, client](Response::PadData data) { OnPadData(data, client); }}; | 289 | [this, client](Response::PadData data) { OnPadData(data, client); }}; |
| @@ -287,7 +293,7 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16 | |||
| 287 | clients[client].port = port; | 293 | clients[client].port = port; |
| 288 | clients[client].pad_index = pad_index; | 294 | clients[client].pad_index = pad_index; |
| 289 | clients[client].active = 0; | 295 | clients[client].active = 0; |
| 290 | clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); | 296 | clients[client].socket = std::make_unique<Socket>(host, port, pad_index, callback); |
| 291 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; | 297 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; |
| 292 | // Set motion parameters | 298 | // Set motion parameters |
| 293 | // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode | 299 | // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode |
| @@ -416,7 +422,7 @@ const Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() const { | |||
| 416 | return pad_queue; | 422 | return pad_queue; |
| 417 | } | 423 | } |
| 418 | 424 | ||
| 419 | void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, | 425 | void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, |
| 420 | const std::function<void()>& success_callback, | 426 | const std::function<void()>& success_callback, |
| 421 | const std::function<void()>& failure_callback) { | 427 | const std::function<void()>& failure_callback) { |
| 422 | std::thread([=] { | 428 | std::thread([=] { |
| @@ -426,7 +432,7 @@ void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, | |||
| 426 | .port_info = [](Response::PortInfo) {}, | 432 | .port_info = [](Response::PortInfo) {}, |
| 427 | .pad_data = [&](Response::PadData) { success_event.Set(); }, | 433 | .pad_data = [&](Response::PadData) { success_event.Set(); }, |
| 428 | }; | 434 | }; |
| 429 | Socket socket{host, port, pad_index, client_id, std::move(callback)}; | 435 | Socket socket{host, port, pad_index, std::move(callback)}; |
| 430 | std::thread worker_thread{SocketLoop, &socket}; | 436 | std::thread worker_thread{SocketLoop, &socket}; |
| 431 | const bool result = success_event.WaitFor(std::chrono::seconds(5)); | 437 | const bool result = success_event.WaitFor(std::chrono::seconds(5)); |
| 432 | socket.Stop(); | 438 | socket.Stop(); |
| @@ -440,7 +446,7 @@ void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, | |||
| 440 | } | 446 | } |
| 441 | 447 | ||
| 442 | CalibrationConfigurationJob::CalibrationConfigurationJob( | 448 | CalibrationConfigurationJob::CalibrationConfigurationJob( |
| 443 | const std::string& host, u16 port, std::size_t pad_index, u32 client_id, | 449 | const std::string& host, u16 port, std::size_t pad_index, |
| 444 | std::function<void(Status)> status_callback, | 450 | std::function<void(Status)> status_callback, |
| 445 | std::function<void(u16, u16, u16, u16)> data_callback) { | 451 | std::function<void(u16, u16, u16, u16)> data_callback) { |
| 446 | 452 | ||
| @@ -485,7 +491,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( | |||
| 485 | complete_event.Set(); | 491 | complete_event.Set(); |
| 486 | } | 492 | } |
| 487 | }}; | 493 | }}; |
| 488 | Socket socket{host, port, pad_index, client_id, std::move(callback)}; | 494 | Socket socket{host, port, pad_index, std::move(callback)}; |
| 489 | std::thread worker_thread{SocketLoop, &socket}; | 495 | std::thread worker_thread{SocketLoop, &socket}; |
| 490 | complete_event.Wait(); | 496 | complete_event.Wait(); |
| 491 | socket.Stop(); | 497 | socket.Stop(); |