Rework stream socket
Stream socket on server now uses the same port as the control socket. Client does not assign a port to its stream socket, instead it sends a handshake packet at the beginning, the server reads the remote port from it.
This commit is contained in:
@@ -19,15 +19,73 @@
|
||||
|
||||
#include "wivrn_connection.h"
|
||||
#include "util/u_logging.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <poll.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
wivrn_connection::wivrn_connection(TCP && tcp, in6_addr address) :
|
||||
wivrn_connection::wivrn_connection(TCP && tcp) :
|
||||
control(std::move(tcp))
|
||||
{
|
||||
stream.bind(stream_server_port);
|
||||
stream.connect(address, stream_client_port);
|
||||
sockaddr_in6 server_address;
|
||||
socklen_t len = sizeof(server_address);
|
||||
if (getsockname(control.get_fd(), (sockaddr*)&server_address, &len) < 0)
|
||||
{
|
||||
throw std::system_error(errno, std::system_category(), "Cannot get socket port");
|
||||
}
|
||||
int port = ntohs(((struct sockaddr_in6*)&server_address)->sin6_port);
|
||||
|
||||
sockaddr_in6 client_address;
|
||||
len = sizeof(client_address);
|
||||
if (getpeername(control.get_fd(), (sockaddr *)&client_address, &len) < 0)
|
||||
{
|
||||
throw std::system_error(errno, std::system_category(), "Cannot get client address");
|
||||
}
|
||||
|
||||
// Wait for client to send handshake on UDP
|
||||
auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(10);
|
||||
|
||||
stream.bind(port);
|
||||
|
||||
control.send(to_headset::control_packets(to_headset::handshake{}));
|
||||
|
||||
while (true)
|
||||
{
|
||||
pollfd fds[2] = {};
|
||||
fds[0].events = POLLIN;
|
||||
fds[0].fd = stream.get_fd();
|
||||
fds[1].events = 0; // only check for errors on control socket
|
||||
fds[1].fd = control.get_fd();
|
||||
|
||||
int r = ::poll(fds, std::size(fds), 1000);
|
||||
if (r < 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
if (fds[0].revents & (POLLHUP | POLLERR))
|
||||
throw std::runtime_error("Error on stream socket");
|
||||
|
||||
if (fds[1].revents & (POLLHUP | POLLERR))
|
||||
throw std::runtime_error("Error on control socket");
|
||||
|
||||
if (fds[0].revents & POLLIN)
|
||||
{
|
||||
auto [packet, peer_addr] = stream.receive_from_raw();
|
||||
if (memcmp(&peer_addr.sin6_addr, &client_address.sin6_addr, sizeof(peer_addr.sin6_addr)) == 0)
|
||||
{
|
||||
int client_port = htons(peer_addr.sin6_port);
|
||||
stream.connect(peer_addr.sin6_addr, client_port);
|
||||
U_LOG_D("Stream socket connected, client port %d", client_port);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (std::chrono::steady_clock::now() > timeout)
|
||||
{
|
||||
throw std::runtime_error("No handshake received on stream socket");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Set Expedited forwarding https://datatracker.ietf.org/doc/html/rfc3246
|
||||
@@ -49,24 +107,6 @@ void wivrn_connection::send_stream(const to_headset::stream_packets & packet)
|
||||
stream.send(packet);
|
||||
}
|
||||
|
||||
std::optional<from_headset::stream_packets> wivrn_connection::poll_stream(int timeout)
|
||||
{
|
||||
pollfd fds{};
|
||||
fds.events = POLLIN;
|
||||
fds.fd = stream.get_fd();
|
||||
|
||||
int r = ::poll(&fds, 1, timeout);
|
||||
if (r < 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
if (r > 0 && (fds.revents & POLLIN))
|
||||
{
|
||||
return stream.receive();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<from_headset::control_packets> wivrn_connection::poll_control(int timeout)
|
||||
{
|
||||
pollfd fds{};
|
||||
|
||||
@@ -32,14 +32,13 @@ class wivrn_connection
|
||||
typed_socket<UDP, from_headset::stream_packets, to_headset::stream_packets> stream;
|
||||
|
||||
public:
|
||||
wivrn_connection(TCP && tcp, in6_addr address);
|
||||
wivrn_connection(TCP && tcp);
|
||||
wivrn_connection(const wivrn_connection &) = delete;
|
||||
wivrn_connection & operator=(const wivrn_connection &) = delete;
|
||||
|
||||
void send_control(const to_headset::control_packets & packet);
|
||||
void send_stream(const to_headset::stream_packets & packet);
|
||||
|
||||
std::optional<from_headset::stream_packets> poll_stream(int timeout);
|
||||
std::optional<from_headset::control_packets> poll_control(int timeout);
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -61,26 +61,18 @@ struct wivrn_comp_target_factory : public comp_target_factory
|
||||
}
|
||||
};
|
||||
|
||||
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp, in6_addr & address) :
|
||||
connection(std::move(tcp), address)
|
||||
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp) :
|
||||
connection(std::move(tcp))
|
||||
{
|
||||
}
|
||||
|
||||
wivrn_system_devices * xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wivrn::TCP && tcp)
|
||||
{
|
||||
sockaddr_in6 address;
|
||||
socklen_t address_len = sizeof(address);
|
||||
if (getpeername(tcp.get_fd(), (sockaddr *)&address, &address_len) < 0)
|
||||
{
|
||||
U_LOG_E("Cannot get peer address: %s", strerror(errno));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<wivrn_session> self;
|
||||
std::optional<xrt::drivers::wivrn::from_headset::control_packets> control;
|
||||
try
|
||||
{
|
||||
self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp), address.sin6_addr));
|
||||
self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp)));
|
||||
while (not(control = self->connection.poll_control(-1)))
|
||||
{}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
|
||||
|
||||
std::shared_ptr<audio_device> audio_handle;
|
||||
|
||||
wivrn_session(TCP && tcp, in6_addr & address);
|
||||
wivrn_session(TCP && tcp);
|
||||
|
||||
public:
|
||||
static wivrn_system_devices *
|
||||
@@ -73,6 +73,7 @@ public:
|
||||
clock_offset
|
||||
get_offset();
|
||||
|
||||
void operator()(from_headset::handshake&&) {}
|
||||
void operator()(from_headset::headset_info_packet &&);
|
||||
void operator()(from_headset::tracking &&);
|
||||
void operator()(from_headset::inputs &&);
|
||||
|
||||
Reference in New Issue
Block a user