/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * 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 . */ #pragma once #include "wivrn_serialization.h" #include #include #include #include #include #include #include #include #include namespace xrt::drivers::wivrn { class socket_shutdown : public std::exception { public: const char * what() const noexcept override; }; class invalid_packet : public std::exception { public: const char * what() const noexcept override; }; template struct index_of_type {}; template struct index_of_type : std::integral_constant {}; template struct index_of_type : std::integral_constant::value> {}; static_assert(index_of_type::value == 0); static_assert(index_of_type::value == 1); class fd_base { protected: int fd = -1; fd_base(const fd_base &) = delete; std::atomic bytes_sent_ = 0; std::atomic bytes_received_ = 0; public: fd_base() = default; fd_base(int fd) : fd{fd} {} fd_base(fd_base &&); fd_base & operator=(fd_base &&); ~fd_base(); int get_fd() const { return fd; } operator bool() const { return fd != -1; } uint64_t bytes_sent() const { return bytes_sent_; } uint64_t bytes_received() const { return bytes_received_; } }; class UDP : public fd_base { std::shared_ptr buffer; std::vector> messages; public: UDP(); explicit UDP(int fd); deserialization_packet receive_raw(); deserialization_packet receive_pending(); std::pair receive_from_raw(); void send_raw(const std::vector & data); void send_raw(const std::vector> & data); void send_many_raw(std::span> *> data); void connect(in6_addr address, int port); void connect(in_addr address, int port); void bind(int port); void subscribe_multicast(in6_addr address); void unsubscribe_multicast(in6_addr address); void set_receive_buffer_size(int size); void set_send_buffer_size(int size); void set_tos(int type_of_service); }; class TCP : public fd_base { std::shared_ptr buffer; ssize_t capacity_left = 0; std::span data; std::unique_ptr mutex; void init(); public: TCP(in6_addr address, int port); TCP(in_addr address, int port); explicit TCP(int fd); deserialization_packet receive_raw(); deserialization_packet receive_pending(); void send_raw(const std::vector> & data); void send_many_raw(std::span> *> data); }; using UnixDatagram = UDP; class TCPListener : public fd_base { public: TCPListener(); TCPListener(int port); template std::pair accept() { assert(fd != -1); sockaddr_in6 addr{}; socklen_t addrlen = sizeof(addr); int fd2 = ::accept(fd, (sockaddr *)&addr, &addrlen); if (fd2 < 0) throw std::system_error{errno, std::generic_category()}; return {T{fd2}, addr}; } }; template class typed_socket; namespace details { template struct Index; template struct Index> { static const std::size_t value = 0; }; template struct Index> { static const std::size_t value = 1 + Index>::value; }; } // namespace details template class typed_socket> : public Socket { public: template typed_socket(Args &&... args) : Socket(std::forward(args)...) {} std::optional receive_pending() { deserialization_packet packet = ((Socket *)this)->receive_pending(); if (packet.empty()) return {}; return packet.deserialize(); } std::optional receive() { deserialization_packet packet = this->receive_raw(); if (packet.empty()) return {}; return packet.deserialize(); } // WARNING: serialization packet keeps references to data template static void serialize(serialization_packet & p, const T & data) { p.clear(); uint8_t index = details::Index, std::tuple>::value; p.serialize(index); p.serialize(data); } template void send(const T & data) { thread_local serialization_packet p; serialize(p, data); this->send_raw(p); } void send(const std::span & packets) { thread_local std::vector> *> data; data.clear(); for (auto & packet: packets) data.emplace_back(packet); this->send_many_raw(data); } }; } // namespace xrt::drivers::wivrn