Allow operations using only TCP

This commit is contained in:
Patrick Nicolas
2024-05-16 15:19:56 +02:00
parent f092a3fac0
commit 111afd4737
7 changed files with 114 additions and 51 deletions
+69 -43
View File
@@ -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<to_headset::handshake>(*packet))
{
return;
}
throw std::runtime_error("Invalid handshake received");
}
}
throw std::runtime_error("No handshake received");
}
namespace
{
template <typename T>
@@ -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 <typename T>
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<to_headset::handshake>(*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<std::remove_cvref_t<decltype(packet)>, 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);
}
+6 -2
View File
@@ -30,7 +30,8 @@ class wivrn_session
typed_socket<TCP, to_headset::packets, from_headset::packets> control;
typed_socket<UDP, to_headset::packets, from_headset::packets> stream;
void handshake();
template <typename T>
void handshake(T address);
public:
std::variant<in_addr, in6_addr> address;
@@ -48,7 +49,10 @@ public:
template <typename T>
void send_stream(T && packet)
{
stream.send(std::forward<T>(packet));
if (stream)
stream.send(std::forward<T>(packet));
else
control.send(std::forward<T>(packet));
}
template <typename T>
+3
View File
@@ -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
+5
View File
@@ -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{};
+1
View File
@@ -102,6 +102,7 @@ class UDP : public fd_base
{
public:
UDP();
explicit UDP(int fd);
deserialization_packet receive_raw();
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
+26 -5
View File
@@ -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<from_headset::handshake>(*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)
{
+4 -1
View File
@@ -44,7 +44,10 @@ public:
template <typename T>
void send_stream(T && packet)
{
stream.send(std::forward<T>(packet));
if (stream)
stream.send(std::forward<T>(packet));
else
control.send(std::forward<T>(packet));
}
std::optional<from_headset::packets> poll_control(int timeout);