From 38fcf7dc009b0e6b35c17fc24f0089ddc22ad46e Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Wed, 8 Nov 2023 17:31:38 +0100 Subject: [PATCH] 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. --- client/scenes/lobby.cpp | 2 +- client/scenes/stream.h | 1 + client/wivrn_client.cpp | 45 +++++++++++++--- client/wivrn_client.h | 2 + common/wivrn_packets.h | 19 ++++--- common/wivrn_sockets.cpp | 30 +++++------ common/wivrn_sockets.h | 3 +- server/driver/wivrn_connection.cpp | 82 ++++++++++++++++++++++-------- server/driver/wivrn_connection.h | 3 +- server/driver/wivrn_session.cpp | 14 ++--- server/driver/wivrn_session.h | 3 +- server/main.cpp | 4 +- 12 files changed, 141 insertions(+), 67 deletions(-) diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 52b977fb..68c03f30 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -246,7 +246,7 @@ std::unique_ptr connect_to_session(const std::vector 0 && (fds.revents & POLLIN)) + { + auto packet = control.receive(); + if (not packet) + continue; + if (std::holds_alternative(*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) : control(address, port), stream(), address(address) { - // TODO: negociate UDP port - stream.bind(stream_client_port); - stream.connect(address, stream_server_port); + stream.connect(address, port); stream.set_receive_buffer_size(1024 * 1024 * 5); char buffer[100]; spdlog::info("Connection to {}:{}", inet_ntop(AF_INET6, &address, buffer, sizeof(buffer)), port); + handshake(); } wivrn_session::wivrn_session(in_addr address, int port) : control(address, port), stream(), address(address) { - // TODO: negociate UDP port - stream.bind(stream_client_port); - stream.connect(address, stream_server_port); + stream.connect(address, port); stream.set_receive_buffer_size(1024 * 1024 * 5); char buffer[100]; spdlog::info("Connection to {}:{}", inet_ntop(AF_INET, &address, buffer, sizeof(buffer)), port); + handshake(); } // std::unique_ptr wivrn_client::poll() diff --git a/client/wivrn_client.h b/client/wivrn_client.h index a36bbe54..429ce1aa 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -31,6 +31,8 @@ class wivrn_session typed_socket control; typed_socket stream; + void handshake(); + public: std::variant address; diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 0e01f6f6..cdfd4f1f 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -26,8 +26,6 @@ #include #include #include -#include -#include #include #include #include @@ -38,9 +36,8 @@ namespace xrt::drivers::wivrn template struct serialization_traits; -static const int control_port = 9757; -static const int stream_client_port = 9757; -static const int stream_server_port = 9758; +// Default port for server to listen, both TCP and UDP +static const int default_port = 9757; enum class device_id : uint8_t { @@ -110,6 +107,10 @@ struct headset_info_packet std::optional microphone; }; +struct handshake +{ +}; + struct tracking { enum flags : uint8_t @@ -186,12 +187,16 @@ struct feedback }; using control_packets = std::variant; -using stream_packets = std::variant; +using stream_packets = std::variant; } // namespace from_headset namespace to_headset { +struct handshake +{ +}; + struct audio_stream_description { struct device @@ -303,8 +308,8 @@ struct timesync_query std::chrono::nanoseconds query; }; +using control_packets = std::variant; using stream_packets = std::variant; -using control_packets = std::variant; } // namespace to_headset diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp index 1948bfc2..55d9e8ca 100644 --- a/common/wivrn_sockets.cpp +++ b/common/wivrn_sockets.cpp @@ -242,21 +242,21 @@ xrt::drivers::wivrn::TCPListener::TCPListener(int port) } } -// std::pair xrt::drivers::wivrn::UDP::receive_from_raw() -// { -// std::vector buffer(2000); -// -// sockaddr_in6 addr; -// socklen_t addrlen = sizeof(addr); -// -// ssize_t received = recvfrom(fd, buffer.data(), buffer.size(), 0, (sockaddr *)&addr, &addrlen); -// if (received < 0) -// throw std::system_error{errno, std::generic_category()}; -// -// buffer.resize(received); -// -// return {deserialization_packet{buffer}, addr}; -// } +std::pair xrt::drivers::wivrn::UDP::receive_from_raw() +{ + std::vector buffer(2000); + + sockaddr_in6 addr; + socklen_t addrlen = sizeof(addr); + + ssize_t received = recvfrom(fd, buffer.data(), buffer.size(), 0, (sockaddr *)&addr, &addrlen); + if (received < 0) + throw std::system_error{errno, std::generic_category()}; + + buffer.resize(received); + + return {deserialization_packet{buffer}, addr}; +} xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::UDP::receive_raw() { diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index 285a4346..7ec86468 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include @@ -88,6 +88,7 @@ public: UDP(); deserialization_packet receive_raw(); + std::pair receive_from_raw(); void send_raw(const std::vector & data); void connect(in6_addr address, int port); diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index 90297f08..ec61118e 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -19,15 +19,73 @@ #include "wivrn_connection.h" #include "util/u_logging.h" +#include #include 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 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 wivrn_connection::poll_control(int timeout) { pollfd fds{}; diff --git a/server/driver/wivrn_connection.h b/server/driver/wivrn_connection.h index c0b0e9cc..245a5a2e 100644 --- a/server/driver/wivrn_connection.h +++ b/server/driver/wivrn_connection.h @@ -32,14 +32,13 @@ class wivrn_connection typed_socket 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 poll_stream(int timeout); std::optional poll_control(int timeout); template diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 9315a3ac..7ec5126b 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -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 self; std::optional control; try { - self = std::shared_ptr(new wivrn_session(std::move(tcp), address.sin6_addr)); + self = std::shared_ptr(new wivrn_session(std::move(tcp))); while (not(control = self->connection.poll_control(-1))) {} } diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 79f9db5b..6be15b33 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -64,7 +64,7 @@ class wivrn_session : public std::enable_shared_from_this std::shared_ptr 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 &&); diff --git a/server/main.cpp b/server/main.cpp index 3bcc6133..473bdd56 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -195,9 +195,9 @@ int inner_main(int argc, char * argv[]) { 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 sigint_received = false;