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)
{
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;
void operator()(to_headset::handshake&&) {};
void operator()(to_headset::video_stream_data_shard &&);
void operator()(to_headset::video_stream_parity_shard &&);
void operator()(to_headset::haptics &&);
+39 -6
View File
@@ -36,28 +36,61 @@
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) :
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_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<UDP, to_headset::stream_packets, from_headset::stream_packets> stream;
void handshake();
public:
std::variant<in_addr, in6_addr> address;