diff --git a/client/wivrn_client.cpp b/client/wivrn_client.cpp index 1296bf1a..36158f38 100644 --- a/client/wivrn_client.cpp +++ b/client/wivrn_client.cpp @@ -39,39 +39,6 @@ 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(*packet)) - { - return; - } - - throw std::runtime_error("Invalid handshake received"); - } - } - - throw std::runtime_error("No handshake received"); -} - namespace { template @@ -86,27 +53,86 @@ void init_stream(T & stream) { spdlog::warn("Failed to set IP ToS to Expedited Forwarding: {}", e.what()); } - // If this packet is lost, a tracking packet will do the job to establish the connection - stream.send(from_headset::handshake{}); } } // namespace +template +void wivrn_session::handshake(T address) +{ + // Wait for handshake on control socket, + // then send ours on stream or control socket, + // finally wait for second server handshake + 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 (true) + { + 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; + try + { + auto h = std::get(*packet); + if (h.stream_port > 0) + { + stream = decltype(stream)(); + stream.connect(address, h.stream_port); + init_stream(stream); + } + break; + } + catch (std::exception & e) + { + spdlog::error("Error when expecting handshake: {}", e.what()); + throw std::runtime_error("Invalid handshake received"); + } + } + + if (std::chrono::steady_clock::now() >= timeout) + throw std::runtime_error("No handshake received"); + } + + // may be on control socket if forced TCP + send_stream(from_headset::handshake{}); + + // Wait for second handshake + while (true) + { + if (poll( + [](const auto && packet) { return std::is_same_v, to_headset::handshake>; }, + std::chrono::milliseconds(100))) + return; + if (std::chrono::steady_clock::now() >= timeout) + throw std::runtime_error("Failed to establish connection"); + + // If using stream socket, the handshake might be lost + if (stream) + stream.send(from_headset::handshake{}); + } +} + wivrn_session::wivrn_session(in6_addr address, int port) : - control(address, port), stream(), address(address) + control(address, port), stream(-1), address(address) { char buffer[100]; spdlog::info("Connection to {}:{}", inet_ntop(AF_INET6, &address, buffer, sizeof(buffer)), port); - handshake(); - stream.connect(address, port); - init_stream(stream); + handshake(address); } wivrn_session::wivrn_session(in_addr address, int port) : - control(address, port), stream(), address(address) + control(address, port), stream(-1), address(address) { char buffer[100]; spdlog::info("Connection to {}:{}", inet_ntop(AF_INET, &address, buffer, sizeof(buffer)), port); - handshake(); - stream.connect(address, port); - init_stream(stream); + handshake(address); } diff --git a/client/wivrn_client.h b/client/wivrn_client.h index 9b9facd9..f12ad112 100644 --- a/client/wivrn_client.h +++ b/client/wivrn_client.h @@ -30,7 +30,8 @@ class wivrn_session typed_socket control; typed_socket stream; - void handshake(); + template + void handshake(T address); public: std::variant address; @@ -48,7 +49,10 @@ public: template void send_stream(T && packet) { - stream.send(std::forward(packet)); + if (stream) + stream.send(std::forward(packet)); + else + control.send(std::forward(packet)); } template diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index baa6a596..fc98a7b5 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -111,6 +111,7 @@ struct headset_info_packet struct handshake { + // Sending this on TCP means connection will be TCP only }; struct tracking @@ -223,6 +224,8 @@ namespace to_headset struct handshake { + // -1 if stream socket should not be used + int stream_port; }; struct audio_stream_description diff --git a/common/wivrn_sockets.cpp b/common/wivrn_sockets.cpp index 9f965b33..11c627cd 100644 --- a/common/wivrn_sockets.cpp +++ b/common/wivrn_sockets.cpp @@ -68,6 +68,11 @@ xrt::drivers::wivrn::UDP::UDP() throw std::system_error{errno, std::generic_category()}; } +xrt::drivers::wivrn::UDP::UDP(int fd) +{ + this->fd = fd; +} + void xrt::drivers::wivrn::UDP::bind(int port) { sockaddr_in6 bind_addr{}; diff --git a/common/wivrn_sockets.h b/common/wivrn_sockets.h index 8090f589..d10c22f3 100644 --- a/common/wivrn_sockets.h +++ b/common/wivrn_sockets.h @@ -102,6 +102,7 @@ class UDP : public fd_base { public: UDP(); + explicit UDP(int fd); deserialization_packet receive_raw(); std::pair receive_from_raw(); diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index cff0b99d..42abd92f 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -42,19 +42,19 @@ wivrn_connection::wivrn_connection(TCP && tcp) : throw std::system_error(errno, std::system_category(), "Cannot get client address"); } - // Wait for client to send handshake on UDP + // Wait for client to send handshake auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(10); stream.bind(port); - control.send(to_headset::handshake{}); + control.send(to_headset::handshake{.stream_port = port}); 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].events = POLLIN; fds[1].fd = control.get_fd(); int r = ::poll(fds, std::size(fds), 1000); @@ -80,16 +80,37 @@ wivrn_connection::wivrn_connection(TCP && tcp) : } } + if (fds[1].revents & POLLIN) + { + auto packet = control.receive(); + if (packet) + { + if (std::holds_alternative(*packet)) + { + stream = decltype(stream)(-1); + port = -1; + U_LOG_I("Using TCP only"); + break; + } + else + { + throw std::runtime_error("Invalid handshake received from client"); + } + } + } + if (std::chrono::steady_clock::now() > timeout) { - throw std::runtime_error("No handshake received on stream socket"); + throw std::runtime_error("No handshake received from client"); } } + control.send(to_headset::handshake{.stream_port = port}); try { // Set Expedited forwarding https://datatracker.ietf.org/doc/html/rfc3246 - stream.set_tos(IPTOS_DSCP_EF); + if (stream) + stream.set_tos(IPTOS_DSCP_EF); } catch (std::exception & e) { diff --git a/server/driver/wivrn_connection.h b/server/driver/wivrn_connection.h index 45fe4b69..0d1191b6 100644 --- a/server/driver/wivrn_connection.h +++ b/server/driver/wivrn_connection.h @@ -44,7 +44,10 @@ public: template void send_stream(T && packet) { - stream.send(std::forward(packet)); + if (stream) + stream.send(std::forward(packet)); + else + control.send(std::forward(packet)); } std::optional poll_control(int timeout);