input_common/udp: allow changing pad index

This commit is contained in:
zhupengfei 2018-08-10 14:15:26 +08:00
parent d1c364bb85
commit e1ae14d508
No known key found for this signature in database
GPG key ID: 85B82A3E62174206
6 changed files with 21 additions and 12 deletions

View file

@ -30,10 +30,12 @@ class Socket {
public:
using clock = std::chrono::system_clock;
explicit Socket(const std::string& host, u16 port, u32 client_id, SocketCallback callback)
explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id,
SocketCallback callback)
: client_id(client_id), timer(io_service),
send_endpoint(udp::endpoint(address_v4::from_string(host), port)),
socket(io_service, udp::endpoint(udp::v4(), 0)), callback(std::move(callback)) {}
socket(io_service, udp::endpoint(udp::v4(), 0)), pad_index(pad_index),
callback(std::move(callback)) {}
void Stop() {
io_service.stop();
@ -85,15 +87,14 @@ private:
}
void HandleSend(const boost::system::error_code& error) {
// TODO: add something to the UI to let people choose what ports to listen on
// Send a request for getting port info for pad 1
Request::PortInfo port_info{1, {0, 0, 0, 0}};
// Send a request for getting port info for the pad
Request::PortInfo port_info{1, {pad_index, 0, 0, 0}};
auto port_message = Request::Create(port_info, client_id);
std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
size_t len = socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint);
// Send a request for getting pad data for pad 1
Request::PadData pad_data{Request::PadData::Flags::Id, 0, EMPTY_MAC_ADDRESS};
// Send a request for getting pad data for the pad
Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS};
auto pad_message = Request::Create(pad_data, client_id);
std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
size_t len2 = socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint);
@ -106,6 +107,7 @@ private:
udp::socket socket;
u32 client_id;
u8 pad_index;
static constexpr size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
static constexpr size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
@ -124,13 +126,13 @@ static void SocketLoop(Socket* socket) {
}
Client::Client(std::shared_ptr<DeviceStatus> status, const std::string& host, u16 port,
u32 client_id)
u8 pad_index, u32 client_id)
: status(status) {
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);
socket = std::make_unique<Socket>(host, port, client_id, callback);
socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback);
thread = std::thread{SocketLoop, this->socket.get()};
}