Files
WiVRn/client/wivrn_client.h

131 lines
3.6 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "crypto.h"
#include "wivrn_packets.h"
#include "wivrn_sockets.h"
#include <chrono>
#include <poll.h>
using namespace wivrn;
class handshake_error : public std::exception
{
std::string message;
public:
const char * what() const noexcept override;
handshake_error(std::string_view message);
};
class wivrn_session
{
public:
using control_socket_t = typed_socket<TCP, to_headset::packets, from_headset::packets>;
using stream_socket_t = typed_socket<UDP, to_headset::packets, from_headset::packets>;
private:
control_socket_t control;
stream_socket_t stream;
std::atomic<uint64_t> bytes_sent_ = 0;
std::atomic<uint64_t> bytes_received_ = 0;
template <typename T>
void handshake(T address, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
public:
std::variant<in_addr, in6_addr> address;
wivrn_session(in6_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(in_addr address, int port, bool tcp_only, crypto::key & headset_keypair, std::function<std::string(int fd)> pin_enter);
wivrn_session(const wivrn_session &) = delete;
wivrn_session & operator=(const wivrn_session &) = delete;
template <typename T>
void send_control(T && packet)
{
bytes_sent_ += control.send(std::forward<T>(packet));
}
template <typename T>
void send_stream(T && packet)
{
if (stream)
bytes_sent_ += stream.send(std::forward<T>(packet));
else
bytes_sent_ += control.send(std::forward<T>(packet));
}
template <typename T>
int poll(T && visitor, std::chrono::milliseconds timeout)
{
pollfd fds[2] = {};
fds[0].events = POLLIN;
fds[0].fd = stream.get_fd();
fds[1].events = POLLIN;
fds[1].fd = control.get_fd();
while (auto packet = stream.receive_pending(&bytes_received_))
std::visit(std::forward<T>(visitor), std::move(*packet));
while (auto packet = control.receive_pending(&bytes_received_))
std::visit(std::forward<T>(visitor), std::move(*packet));
int r = ::poll(fds, std::size(fds), timeout.count());
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 = stream.receive(&bytes_received_);
if (packet)
{
std::visit(std::forward<T>(visitor), std::move(*packet));
}
}
if (fds[1].revents & POLLIN)
{
auto packet = control.receive(&bytes_received_);
if (packet)
std::visit(std::forward<T>(visitor), std::move(*packet));
}
return r;
}
uint64_t bytes_received() const
{
return bytes_received_;
}
uint64_t bytes_sent() const
{
return bytes_sent_;
}
};