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:
Patrick Nicolas
2023-11-08 19:15:59 +01:00
parent c3383521a4
commit 38fcf7dc00
12 changed files with 141 additions and 67 deletions
+1 -1
View File
@@ -246,7 +246,7 @@ std::unique_ptr<wivrn_session> connect_to_session(const std::vector<wivrn_discov
} }
catch(std::exception& e) catch(std::exception& e)
{ {
spdlog::warn("Cannot connect to {}", service.hostname); spdlog::warn("Cannot connect to {}: {}", service.hostname, e.what());
} }
} }
} }
+1
View File
@@ -100,6 +100,7 @@ public:
virtual void render() override; virtual void render() override;
void operator()(to_headset::handshake&&) {};
void operator()(to_headset::video_stream_data_shard &&); void operator()(to_headset::video_stream_data_shard &&);
void operator()(to_headset::video_stream_parity_shard &&); void operator()(to_headset::video_stream_parity_shard &&);
void operator()(to_headset::haptics &&); void operator()(to_headset::haptics &&);
+39 -6
View File
@@ -36,28 +36,61 @@
using namespace std::chrono_literals; using namespace std::chrono_literals;
void wivrn_session::handshake()
{
//Wait for handshake on control socket, then send ours on stream socket
pollfd fds{};
fds.events = POLLIN;
fds.fd = control.get_fd();
auto timeout = std::chrono::steady_clock::now() + 5s;
// Loop because TCP socket may return partial data
while (std::chrono::steady_clock::now() < timeout)
{
int r = ::poll(&fds, 1, 5000);
if (r < 0)
throw std::system_error(errno, std::system_category());
if (r > 0 && (fds.revents & POLLIN))
{
auto packet = control.receive();
if (not packet)
continue;
if (std::holds_alternative<to_headset::handshake>(*packet))
{
// If this packet is lost, a tracking packet will do the job to establish the connection
send_stream(from_headset::stream_packets(from_headset::handshake{}));
return;
}
throw std::runtime_error("invalid handshale received");
}
}
throw std::runtime_error("no handshake received");
}
wivrn_session::wivrn_session(in6_addr address, int port) : wivrn_session::wivrn_session(in6_addr address, int port) :
control(address, port), stream(), address(address) control(address, port), stream(), address(address)
{ {
// TODO: negociate UDP port stream.connect(address, port);
stream.bind(stream_client_port);
stream.connect(address, stream_server_port);
stream.set_receive_buffer_size(1024 * 1024 * 5); stream.set_receive_buffer_size(1024 * 1024 * 5);
char buffer[100]; char buffer[100];
spdlog::info("Connection to {}:{}", inet_ntop(AF_INET6, &address, buffer, sizeof(buffer)), port); spdlog::info("Connection to {}:{}", inet_ntop(AF_INET6, &address, buffer, sizeof(buffer)), port);
handshake();
} }
wivrn_session::wivrn_session(in_addr address, int port) : wivrn_session::wivrn_session(in_addr address, int port) :
control(address, port), stream(), address(address) control(address, port), stream(), address(address)
{ {
// TODO: negociate UDP port stream.connect(address, port);
stream.bind(stream_client_port);
stream.connect(address, stream_server_port);
stream.set_receive_buffer_size(1024 * 1024 * 5); stream.set_receive_buffer_size(1024 * 1024 * 5);
char buffer[100]; char buffer[100];
spdlog::info("Connection to {}:{}", inet_ntop(AF_INET, &address, buffer, sizeof(buffer)), port); spdlog::info("Connection to {}:{}", inet_ntop(AF_INET, &address, buffer, sizeof(buffer)), port);
handshake();
} }
// std::unique_ptr<wivrn_session> wivrn_client::poll() // std::unique_ptr<wivrn_session> wivrn_client::poll()
+2
View File
@@ -31,6 +31,8 @@ class wivrn_session
typed_socket<TCP, to_headset::control_packets, from_headset::control_packets> control; typed_socket<TCP, to_headset::control_packets, from_headset::control_packets> control;
typed_socket<UDP, to_headset::stream_packets, from_headset::stream_packets> stream; typed_socket<UDP, to_headset::stream_packets, from_headset::stream_packets> stream;
void handshake();
public: public:
std::variant<in_addr, in6_addr> address; std::variant<in_addr, in6_addr> address;
+12 -7
View File
@@ -26,8 +26,6 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <optional> #include <optional>
#include <span> #include <span>
#include <string>
#include <tuple>
#include <variant> #include <variant>
#include <vector> #include <vector>
#include <openxr/openxr.h> #include <openxr/openxr.h>
@@ -38,9 +36,8 @@ namespace xrt::drivers::wivrn
template <typename T, typename Enable> template <typename T, typename Enable>
struct serialization_traits; struct serialization_traits;
static const int control_port = 9757; // Default port for server to listen, both TCP and UDP
static const int stream_client_port = 9757; static const int default_port = 9757;
static const int stream_server_port = 9758;
enum class device_id : uint8_t enum class device_id : uint8_t
{ {
@@ -110,6 +107,10 @@ struct headset_info_packet
std::optional<audio_description> microphone; std::optional<audio_description> microphone;
}; };
struct handshake
{
};
struct tracking struct tracking
{ {
enum flags : uint8_t enum flags : uint8_t
@@ -186,12 +187,16 @@ struct feedback
}; };
using control_packets = std::variant<headset_info_packet, feedback, audio_data>; using control_packets = std::variant<headset_info_packet, feedback, audio_data>;
using stream_packets = std::variant<tracking, inputs, timesync_response>; using stream_packets = std::variant<handshake, tracking, inputs, timesync_response>;
} // namespace from_headset } // namespace from_headset
namespace to_headset namespace to_headset
{ {
struct handshake
{
};
struct audio_stream_description struct audio_stream_description
{ {
struct device struct device
@@ -303,8 +308,8 @@ struct timesync_query
std::chrono::nanoseconds query; std::chrono::nanoseconds query;
}; };
using control_packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data>;
using stream_packets = std::variant<video_stream_data_shard, video_stream_parity_shard, haptics, timesync_query>; using stream_packets = std::variant<video_stream_data_shard, video_stream_parity_shard, haptics, timesync_query>;
using control_packets = std::variant<audio_stream_description, video_stream_description, audio_data>;
} // namespace to_headset } // namespace to_headset
+15 -15
View File
@@ -242,21 +242,21 @@ xrt::drivers::wivrn::TCPListener::TCPListener(int port)
} }
} }
// std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> xrt::drivers::wivrn::UDP::receive_from_raw() std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> xrt::drivers::wivrn::UDP::receive_from_raw()
// { {
// std::vector<uint8_t> buffer(2000); std::vector<uint8_t> buffer(2000);
//
// sockaddr_in6 addr; sockaddr_in6 addr;
// socklen_t addrlen = sizeof(addr); socklen_t addrlen = sizeof(addr);
//
// ssize_t received = recvfrom(fd, buffer.data(), buffer.size(), 0, (sockaddr *)&addr, &addrlen); ssize_t received = recvfrom(fd, buffer.data(), buffer.size(), 0, (sockaddr *)&addr, &addrlen);
// if (received < 0) if (received < 0)
// throw std::system_error{errno, std::generic_category()}; throw std::system_error{errno, std::generic_category()};
//
// buffer.resize(received); buffer.resize(received);
//
// return {deserialization_packet{buffer}, addr}; return {deserialization_packet{buffer}, addr};
// } }
xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_raw() xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_raw()
{ {
+2 -1
View File
@@ -25,7 +25,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <stdexcept> #include <exception>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -88,6 +88,7 @@ public:
UDP(); UDP();
deserialization_packet receive_raw(); deserialization_packet receive_raw();
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
void send_raw(const std::vector<uint8_t> & data); void send_raw(const std::vector<uint8_t> & data);
void connect(in6_addr address, int port); void connect(in6_addr address, int port);
+61 -21
View File
@@ -19,15 +19,73 @@
#include "wivrn_connection.h" #include "wivrn_connection.h"
#include "util/u_logging.h" #include "util/u_logging.h"
#include <arpa/inet.h>
#include <poll.h> #include <poll.h>
using namespace std::chrono_literals; using namespace std::chrono_literals;
wivrn_connection::wivrn_connection(TCP && tcp, in6_addr address) : wivrn_connection::wivrn_connection(TCP && tcp) :
control(std::move(tcp)) control(std::move(tcp))
{ {
stream.bind(stream_server_port); sockaddr_in6 server_address;
stream.connect(address, stream_client_port); 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 try
{ {
// Set Expedited forwarding https://datatracker.ietf.org/doc/html/rfc3246 // 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); 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) std::optional<from_headset::control_packets> wivrn_connection::poll_control(int timeout)
{ {
pollfd fds{}; pollfd fds{};
+1 -2
View File
@@ -32,14 +32,13 @@ class wivrn_connection
typed_socket<UDP, from_headset::stream_packets, to_headset::stream_packets> stream; typed_socket<UDP, from_headset::stream_packets, to_headset::stream_packets> stream;
public: public:
wivrn_connection(TCP && tcp, in6_addr address); wivrn_connection(TCP && tcp);
wivrn_connection(const wivrn_connection &) = delete; wivrn_connection(const wivrn_connection &) = delete;
wivrn_connection & operator=(const wivrn_connection &) = delete; wivrn_connection & operator=(const wivrn_connection &) = delete;
void send_control(const to_headset::control_packets & packet); void send_control(const to_headset::control_packets & packet);
void send_stream(const to_headset::stream_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); std::optional<from_headset::control_packets> poll_control(int timeout);
template <typename T> template <typename T>
+3 -11
View File
@@ -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) : xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp) :
connection(std::move(tcp), address) connection(std::move(tcp))
{ {
} }
wivrn_system_devices * xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wivrn::TCP && 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::shared_ptr<wivrn_session> self;
std::optional<xrt::drivers::wivrn::from_headset::control_packets> control; std::optional<xrt::drivers::wivrn::from_headset::control_packets> control;
try 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))) while (not(control = self->connection.poll_control(-1)))
{} {}
} }
+2 -1
View File
@@ -64,7 +64,7 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
std::shared_ptr<audio_device> audio_handle; std::shared_ptr<audio_device> audio_handle;
wivrn_session(TCP && tcp, in6_addr & address); wivrn_session(TCP && tcp);
public: public:
static wivrn_system_devices * static wivrn_system_devices *
@@ -73,6 +73,7 @@ public:
clock_offset clock_offset
get_offset(); get_offset();
void operator()(from_headset::handshake&&) {}
void operator()(from_headset::headset_info_packet &&); void operator()(from_headset::headset_info_packet &&);
void operator()(from_headset::tracking &&); void operator()(from_headset::tracking &&);
void operator()(from_headset::inputs &&); void operator()(from_headset::inputs &&);
+2 -2
View File
@@ -195,9 +195,9 @@ int inner_main(int argc, char * argv[])
{ {
try try
{ {
avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", control_port); avahi_publisher publisher(hostname().c_str(), "_wivrn._tcp", default_port);
TCPListener listener(control_port); TCPListener listener(default_port);
bool client_connected = false; bool client_connected = false;
bool sigint_received = false; bool sigint_received = false;