Merge pull request #4937 from german77/multiUDP
InputCommon: Add multiple udp server support
This commit is contained in:
commit
25f650e075
10 changed files with 420 additions and 267 deletions
|
@ -286,7 +286,7 @@ void InputSubsystem::ReloadInputDevices() {
|
|||
if (!impl->udp) {
|
||||
return;
|
||||
}
|
||||
impl->udp->ReloadUDPClient();
|
||||
impl->udp->ReloadSockets();
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Polling::DevicePoller>> InputSubsystem::GetPollers(
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,7 @@
|
|||
|
||||
namespace InputCommon::CemuhookUDP {
|
||||
|
||||
constexpr u16 DEFAULT_PORT = 26760;
|
||||
constexpr char DEFAULT_ADDR[] = "127.0.0.1";
|
||||
constexpr char DEFAULT_SRV[] = "127.0.0.1:26760";
|
||||
|
||||
class Socket;
|
||||
|
||||
|
@ -48,6 +47,9 @@ enum class PadTouch {
|
|||
};
|
||||
|
||||
struct UDPPadStatus {
|
||||
std::string host{"127.0.0.1"};
|
||||
u16 port{26760};
|
||||
std::size_t pad_index{};
|
||||
PadTouch touch{PadTouch::Undefined};
|
||||
PadMotion motion{PadMotion::Undefined};
|
||||
f32 motion_value{0.0f};
|
||||
|
@ -82,37 +84,41 @@ public:
|
|||
|
||||
std::vector<Common::ParamPackage> GetInputDevices() const;
|
||||
|
||||
bool DeviceConnected(std::size_t pad) const;
|
||||
void ReloadUDPClient();
|
||||
void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760,
|
||||
std::size_t pad_index = 0, u32 client_id = 24872);
|
||||
bool DeviceConnected(std::size_t client) const;
|
||||
void ReloadSockets();
|
||||
|
||||
std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue();
|
||||
const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& GetPadQueue() const;
|
||||
Common::SPSCQueue<UDPPadStatus>& GetPadQueue();
|
||||
const Common::SPSCQueue<UDPPadStatus>& GetPadQueue() const;
|
||||
|
||||
DeviceStatus& GetPadState(std::size_t pad);
|
||||
const DeviceStatus& GetPadState(std::size_t pad) const;
|
||||
DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad);
|
||||
const DeviceStatus& GetPadState(const std::string& host, u16 port, std::size_t pad) const;
|
||||
|
||||
private:
|
||||
struct ClientData {
|
||||
std::string host{"127.0.0.1"};
|
||||
u16 port{26760};
|
||||
std::size_t pad_index{};
|
||||
std::unique_ptr<Socket> socket;
|
||||
DeviceStatus status;
|
||||
std::thread thread;
|
||||
u64 packet_sequence = 0;
|
||||
u8 active = 0;
|
||||
u64 packet_sequence{};
|
||||
s8 active{-1};
|
||||
|
||||
// Realtime values
|
||||
// motion is initalized with PID values for drift correction on joycons
|
||||
InputCommon::MotionInput motion{0.3f, 0.005f, 0.0f};
|
||||
std::chrono::time_point<std::chrono::system_clock> last_motion_update;
|
||||
std::chrono::time_point<std::chrono::steady_clock> last_motion_update;
|
||||
};
|
||||
|
||||
// For shutting down, clear all data, join all threads, release usb
|
||||
void Reset();
|
||||
|
||||
// Translates configuration to client number
|
||||
std::size_t GetClientNumber(std::string_view host, u16 port, std::size_t pad) const;
|
||||
|
||||
void OnVersion(Response::Version);
|
||||
void OnPortInfo(Response::PortInfo);
|
||||
void OnPadData(Response::PadData);
|
||||
void OnPadData(Response::PadData, std::size_t client);
|
||||
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,
|
||||
|
@ -120,8 +126,10 @@ private:
|
|||
|
||||
bool configuring = false;
|
||||
|
||||
std::array<ClientData, 4> clients;
|
||||
std::array<Common::SPSCQueue<UDPPadStatus>, 4> pad_queue;
|
||||
// Allocate clients for 8 udp servers
|
||||
const std::size_t max_udp_clients = 32;
|
||||
std::array<ClientData, 4 * 8> clients;
|
||||
Common::SPSCQueue<UDPPadStatus> pad_queue;
|
||||
};
|
||||
|
||||
/// An async job allowing configuration of the touchpad calibration.
|
||||
|
|
|
@ -13,17 +13,17 @@ namespace InputCommon {
|
|||
|
||||
class UDPMotion final : public Input::MotionDevice {
|
||||
public:
|
||||
explicit UDPMotion(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_)
|
||||
explicit UDPMotion(std::string ip_, u16 port_, u16 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;
|
||||
return client->GetPadState(ip, port, pad).motion_status;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string ip;
|
||||
const int port;
|
||||
const u32 pad;
|
||||
const u16 port;
|
||||
const u16 pad;
|
||||
CemuhookUDP::Client* client;
|
||||
mutable std::mutex mutex;
|
||||
};
|
||||
|
@ -39,8 +39,8 @@ UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_)
|
|||
*/
|
||||
std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) {
|
||||
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));
|
||||
const auto port = static_cast<u16>(params.Get("port", 26760));
|
||||
const auto pad = static_cast<u16>(params.Get("pad_index", 0));
|
||||
|
||||
return std::make_unique<UDPMotion>(std::move(ip), port, pad, client.get());
|
||||
}
|
||||
|
@ -59,35 +59,33 @@ Common::ParamPackage UDPMotionFactory::GetNextInput() {
|
|||
Common::ParamPackage params;
|
||||
CemuhookUDP::UDPPadStatus pad;
|
||||
auto& queue = client->GetPadQueue();
|
||||
for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
|
||||
while (queue[pad_number].Pop(pad)) {
|
||||
if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) {
|
||||
continue;
|
||||
}
|
||||
params.Set("engine", "cemuhookudp");
|
||||
params.Set("ip", "127.0.0.1");
|
||||
params.Set("port", 26760);
|
||||
params.Set("pad_index", static_cast<int>(pad_number));
|
||||
params.Set("motion", static_cast<u16>(pad.motion));
|
||||
return params;
|
||||
while (queue.Pop(pad)) {
|
||||
if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) {
|
||||
continue;
|
||||
}
|
||||
params.Set("engine", "cemuhookudp");
|
||||
params.Set("ip", pad.host);
|
||||
params.Set("port", static_cast<u16>(pad.port));
|
||||
params.Set("pad_index", static_cast<u16>(pad.pad_index));
|
||||
params.Set("motion", static_cast<u16>(pad.motion));
|
||||
return params;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
class UDPTouch final : public Input::TouchDevice {
|
||||
public:
|
||||
explicit UDPTouch(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_)
|
||||
explicit UDPTouch(std::string ip_, u16 port_, u16 pad_, CemuhookUDP::Client* client_)
|
||||
: ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
|
||||
|
||||
std::tuple<float, float, bool> GetStatus() const override {
|
||||
return client->GetPadState(pad).touch_status;
|
||||
return client->GetPadState(ip, port, pad).touch_status;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string ip;
|
||||
const int port;
|
||||
const u32 pad;
|
||||
const u16 port;
|
||||
const u16 pad;
|
||||
CemuhookUDP::Client* client;
|
||||
mutable std::mutex mutex;
|
||||
};
|
||||
|
@ -103,8 +101,8 @@ UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_)
|
|||
*/
|
||||
std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) {
|
||||
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));
|
||||
const auto port = static_cast<u16>(params.Get("port", 26760));
|
||||
const auto pad = static_cast<u16>(params.Get("pad_index", 0));
|
||||
|
||||
return std::make_unique<UDPTouch>(std::move(ip), port, pad, client.get());
|
||||
}
|
||||
|
@ -123,18 +121,16 @@ Common::ParamPackage UDPTouchFactory::GetNextInput() {
|
|||
Common::ParamPackage params;
|
||||
CemuhookUDP::UDPPadStatus pad;
|
||||
auto& queue = client->GetPadQueue();
|
||||
for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
|
||||
while (queue[pad_number].Pop(pad)) {
|
||||
if (pad.touch == CemuhookUDP::PadTouch::Undefined) {
|
||||
continue;
|
||||
}
|
||||
params.Set("engine", "cemuhookudp");
|
||||
params.Set("ip", "127.0.0.1");
|
||||
params.Set("port", 26760);
|
||||
params.Set("pad_index", static_cast<int>(pad_number));
|
||||
params.Set("touch", static_cast<u16>(pad.touch));
|
||||
return params;
|
||||
while (queue.Pop(pad)) {
|
||||
if (pad.touch == CemuhookUDP::PadTouch::Undefined) {
|
||||
continue;
|
||||
}
|
||||
params.Set("engine", "cemuhookudp");
|
||||
params.Set("ip", pad.host);
|
||||
params.Set("port", static_cast<u16>(pad.port));
|
||||
params.Set("pad_index", static_cast<u16>(pad.pad_index));
|
||||
params.Set("touch", static_cast<u16>(pad.touch));
|
||||
return params;
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue