Allow operations using only TCP
This commit is contained in:
+69
-43
@@ -39,39 +39,6 @@
|
|||||||
|
|
||||||
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))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("Invalid handshake received");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("No handshake received");
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -86,27 +53,86 @@ void init_stream(T & stream)
|
|||||||
{
|
{
|
||||||
spdlog::warn("Failed to set IP ToS to Expedited Forwarding: {}", e.what());
|
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
|
} // 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) :
|
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];
|
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();
|
handshake(address);
|
||||||
stream.connect(address, port);
|
|
||||||
init_stream(stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(-1), address(address)
|
||||||
{
|
{
|
||||||
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();
|
handshake(address);
|
||||||
stream.connect(address, port);
|
|
||||||
init_stream(stream);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ class wivrn_session
|
|||||||
typed_socket<TCP, to_headset::packets, from_headset::packets> control;
|
typed_socket<TCP, to_headset::packets, from_headset::packets> control;
|
||||||
typed_socket<UDP, to_headset::packets, from_headset::packets> stream;
|
typed_socket<UDP, to_headset::packets, from_headset::packets> stream;
|
||||||
|
|
||||||
void handshake();
|
template <typename T>
|
||||||
|
void handshake(T address);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::variant<in_addr, in6_addr> address;
|
std::variant<in_addr, in6_addr> address;
|
||||||
@@ -48,7 +49,10 @@ public:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
void send_stream(T && packet)
|
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>
|
template <typename T>
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ struct headset_info_packet
|
|||||||
|
|
||||||
struct handshake
|
struct handshake
|
||||||
{
|
{
|
||||||
|
// Sending this on TCP means connection will be TCP only
|
||||||
};
|
};
|
||||||
|
|
||||||
struct tracking
|
struct tracking
|
||||||
@@ -223,6 +224,8 @@ namespace to_headset
|
|||||||
|
|
||||||
struct handshake
|
struct handshake
|
||||||
{
|
{
|
||||||
|
// -1 if stream socket should not be used
|
||||||
|
int stream_port;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct audio_stream_description
|
struct audio_stream_description
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ xrt::drivers::wivrn::UDP::UDP()
|
|||||||
throw std::system_error{errno, std::generic_category()};
|
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)
|
void xrt::drivers::wivrn::UDP::bind(int port)
|
||||||
{
|
{
|
||||||
sockaddr_in6 bind_addr{};
|
sockaddr_in6 bind_addr{};
|
||||||
|
|||||||
@@ -102,6 +102,7 @@ class UDP : public fd_base
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UDP();
|
UDP();
|
||||||
|
explicit UDP(int fd);
|
||||||
|
|
||||||
deserialization_packet receive_raw();
|
deserialization_packet receive_raw();
|
||||||
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
||||||
|
|||||||
@@ -42,19 +42,19 @@ wivrn_connection::wivrn_connection(TCP && tcp) :
|
|||||||
throw std::system_error(errno, std::system_category(), "Cannot get client address");
|
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);
|
auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(10);
|
||||||
|
|
||||||
stream.bind(port);
|
stream.bind(port);
|
||||||
|
|
||||||
control.send(to_headset::handshake{});
|
control.send(to_headset::handshake{.stream_port = port});
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
pollfd fds[2] = {};
|
pollfd fds[2] = {};
|
||||||
fds[0].events = POLLIN;
|
fds[0].events = POLLIN;
|
||||||
fds[0].fd = stream.get_fd();
|
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();
|
fds[1].fd = control.get_fd();
|
||||||
|
|
||||||
int r = ::poll(fds, std::size(fds), 1000);
|
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)
|
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
|
try
|
||||||
{
|
{
|
||||||
// Set Expedited forwarding https://datatracker.ietf.org/doc/html/rfc3246
|
// 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)
|
catch (std::exception & e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -44,7 +44,10 @@ public:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
void send_stream(T && packet)
|
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);
|
std::optional<from_headset::packets> poll_control(int timeout);
|
||||||
|
|||||||
Reference in New Issue
Block a user