Add server source, originally in monado
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
// Copyright 2022, Guillaume Meunier
|
||||
// Copyright 2022, Patrick Nicolas
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "wivrn_serialization.h"
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <netinet/ip.h>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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 <typename T, typename... Ts> struct index_of_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts> struct index_of_type<T, T, Ts...> : std::integral_constant<size_t, 0>
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T, typename T2, typename... Ts>
|
||||
struct index_of_type<T, T2, Ts...> : std::integral_constant<size_t, 1 + index_of_type<T, Ts...>::value>
|
||||
{
|
||||
};
|
||||
|
||||
static_assert(index_of_type<int, int, float>::value == 0);
|
||||
static_assert(index_of_type<float, int, float>::value == 1);
|
||||
|
||||
class socket_base
|
||||
{
|
||||
protected:
|
||||
int fd = -1;
|
||||
|
||||
socket_base() = default;
|
||||
socket_base(const socket_base &) = delete;
|
||||
socket_base(socket_base &&);
|
||||
~socket_base();
|
||||
|
||||
public:
|
||||
int
|
||||
get_fd() const
|
||||
{
|
||||
return fd;
|
||||
}
|
||||
};
|
||||
|
||||
class UDP : public socket_base
|
||||
{
|
||||
public:
|
||||
UDP();
|
||||
UDP(const UDP &) = delete;
|
||||
UDP(UDP &&) = default;
|
||||
|
||||
deserialization_packet
|
||||
receive_raw();
|
||||
void
|
||||
send_raw(const std::vector<uint8_t> &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);
|
||||
};
|
||||
|
||||
class TCP : public socket_base
|
||||
{
|
||||
std::vector<uint8_t> buffer;
|
||||
std::unique_ptr<std::mutex> mutex;
|
||||
|
||||
void
|
||||
init();
|
||||
|
||||
public:
|
||||
TCP(in6_addr address, int port);
|
||||
TCP(in_addr address, int port);
|
||||
explicit TCP(int fd);
|
||||
TCP(const TCP &) = delete;
|
||||
TCP(TCP &&) = default;
|
||||
|
||||
deserialization_packet
|
||||
receive_raw();
|
||||
void
|
||||
send_raw(const std::vector<uint8_t> &data);
|
||||
};
|
||||
|
||||
class TCPListener : public socket_base
|
||||
{
|
||||
public:
|
||||
TCPListener(int port);
|
||||
TCPListener(const TCPListener &) = delete;
|
||||
TCPListener(TCPListener &&) = default;
|
||||
|
||||
std::pair<TCP, sockaddr_in6>
|
||||
accept();
|
||||
};
|
||||
|
||||
template <typename Socket, typename ReceivedType, typename SentType> class typed_socket : public Socket
|
||||
{
|
||||
public:
|
||||
template <typename... Args> typed_socket(Args &&...args) : Socket(std::forward<Args>(args)...) {}
|
||||
|
||||
std::optional<ReceivedType>
|
||||
receive()
|
||||
{
|
||||
deserialization_packet packet = this->receive_raw();
|
||||
if (packet.empty())
|
||||
return {};
|
||||
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
|
||||
template <typename T = SentType, typename = std::enable_if_t<std::is_same_v<T, SentType>>>
|
||||
void
|
||||
send(const T &data)
|
||||
{
|
||||
serialization_packet p;
|
||||
p.serialize(data);
|
||||
this->send_raw(std::move(p));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
Reference in New Issue
Block a user