2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-07-12 02:42:56 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-12-25 20:27:52 +01:00
|
|
|
#include <map>
|
2020-07-12 02:42:56 +02:00
|
|
|
#include <memory>
|
2023-02-03 06:08:45 +01:00
|
|
|
#include <span>
|
2020-07-12 02:42:56 +02:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2020-08-31 16:09:06 +02:00
|
|
|
#elif !YUZU_UNIX
|
2020-07-12 02:42:56 +02:00
|
|
|
#error "Platform not implemented"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2021-12-25 20:27:52 +01:00
|
|
|
#include "core/internal_network/network.h"
|
2022-07-30 05:58:23 +02:00
|
|
|
#include "network/network.h"
|
2020-07-12 02:42:56 +02:00
|
|
|
|
|
|
|
// TODO: C++20 Replace std::vector usages with std::span
|
|
|
|
|
|
|
|
namespace Network {
|
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
class SocketBase {
|
2020-07-12 02:42:56 +02:00
|
|
|
public:
|
2022-07-30 05:58:23 +02:00
|
|
|
#ifdef YUZU_UNIX
|
|
|
|
using SOCKET = int;
|
|
|
|
static constexpr SOCKET INVALID_SOCKET = -1;
|
|
|
|
static constexpr SOCKET SOCKET_ERROR = -1;
|
|
|
|
#endif
|
|
|
|
|
2020-07-12 02:42:56 +02:00
|
|
|
struct AcceptResult {
|
2022-07-30 05:58:23 +02:00
|
|
|
std::unique_ptr<SocketBase> socket;
|
2020-07-12 02:42:56 +02:00
|
|
|
SockAddrIn sockaddr_in;
|
|
|
|
};
|
2022-09-22 14:10:43 +02:00
|
|
|
|
|
|
|
SocketBase() = default;
|
|
|
|
explicit SocketBase(SOCKET fd_) : fd{fd_} {}
|
2022-08-01 22:47:39 +02:00
|
|
|
virtual ~SocketBase() = default;
|
2022-07-30 05:58:23 +02:00
|
|
|
|
2023-04-03 01:02:04 +02:00
|
|
|
YUZU_NON_COPYABLE(SocketBase);
|
|
|
|
YUZU_NON_MOVEABLE(SocketBase);
|
2022-07-30 05:58:23 +02:00
|
|
|
|
|
|
|
virtual Errno Initialize(Domain domain, Type type, Protocol protocol) = 0;
|
|
|
|
|
|
|
|
virtual Errno Close() = 0;
|
|
|
|
|
|
|
|
virtual std::pair<AcceptResult, Errno> Accept() = 0;
|
|
|
|
|
|
|
|
virtual Errno Connect(SockAddrIn addr_in) = 0;
|
|
|
|
|
|
|
|
virtual std::pair<SockAddrIn, Errno> GetPeerName() = 0;
|
|
|
|
|
|
|
|
virtual std::pair<SockAddrIn, Errno> GetSockName() = 0;
|
|
|
|
|
|
|
|
virtual Errno Bind(SockAddrIn addr) = 0;
|
|
|
|
|
|
|
|
virtual Errno Listen(s32 backlog) = 0;
|
|
|
|
|
|
|
|
virtual Errno Shutdown(ShutdownHow how) = 0;
|
|
|
|
|
|
|
|
virtual std::pair<s32, Errno> Recv(int flags, std::vector<u8>& message) = 0;
|
|
|
|
|
|
|
|
virtual std::pair<s32, Errno> RecvFrom(int flags, std::vector<u8>& message,
|
|
|
|
SockAddrIn* addr) = 0;
|
|
|
|
|
2023-02-03 06:08:45 +01:00
|
|
|
virtual std::pair<s32, Errno> Send(std::span<const u8> message, int flags) = 0;
|
2022-07-30 05:58:23 +02:00
|
|
|
|
2023-02-03 06:08:45 +01:00
|
|
|
virtual std::pair<s32, Errno> SendTo(u32 flags, std::span<const u8> message,
|
2022-07-30 05:58:23 +02:00
|
|
|
const SockAddrIn* addr) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetLinger(bool enable, u32 linger) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetReuseAddr(bool enable) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetKeepAlive(bool enable) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetBroadcast(bool enable) = 0;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
virtual Errno SetSndBuf(u32 value) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetRcvBuf(u32 value) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetSndTimeo(u32 value) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetRcvTimeo(u32 value) = 0;
|
|
|
|
|
|
|
|
virtual Errno SetNonBlock(bool enable) = 0;
|
|
|
|
|
|
|
|
virtual bool IsOpened() const = 0;
|
|
|
|
|
|
|
|
virtual void HandleProxyPacket(const ProxyPacket& packet) = 0;
|
|
|
|
|
2022-09-22 14:10:43 +02:00
|
|
|
[[nodiscard]] SOCKET GetFD() const {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-07-30 05:58:23 +02:00
|
|
|
SOCKET fd = INVALID_SOCKET;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Socket : public SocketBase {
|
|
|
|
public:
|
|
|
|
Socket() = default;
|
2022-09-22 14:10:43 +02:00
|
|
|
explicit Socket(SOCKET fd_) : SocketBase{fd_} {}
|
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
~Socket() override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
|
|
|
Socket(Socket&& rhs) noexcept;
|
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Initialize(Domain domain, Type type, Protocol protocol) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Close() override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
std::pair<AcceptResult, Errno> Accept() override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Connect(SockAddrIn addr_in) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
std::pair<SockAddrIn, Errno> GetPeerName() override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
std::pair<SockAddrIn, Errno> GetSockName() override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Bind(SockAddrIn addr) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Listen(s32 backlog) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno Shutdown(ShutdownHow how) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
std::pair<s32, Errno> Recv(int flags, std::vector<u8>& message) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
std::pair<s32, Errno> RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2023-02-03 06:08:45 +01:00
|
|
|
std::pair<s32, Errno> Send(std::span<const u8> message, int flags) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2023-02-03 06:08:45 +01:00
|
|
|
std::pair<s32, Errno> SendTo(u32 flags, std::span<const u8> message,
|
2022-07-30 05:58:23 +02:00
|
|
|
const SockAddrIn* addr) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetLinger(bool enable, u32 linger) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetReuseAddr(bool enable) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetKeepAlive(bool enable) override;
|
2022-04-07 23:05:50 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetBroadcast(bool enable) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetSndBuf(u32 value) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetRcvBuf(u32 value) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetSndTimeo(u32 value) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetRcvTimeo(u32 value) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
Errno SetNonBlock(bool enable) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
template <typename T>
|
|
|
|
Errno SetSockOpt(SOCKET fd, int option, T value);
|
2020-07-12 02:42:56 +02:00
|
|
|
|
2022-07-30 05:58:23 +02:00
|
|
|
bool IsOpened() const override;
|
|
|
|
|
|
|
|
void HandleProxyPacket(const ProxyPacket& packet) override;
|
2020-07-12 02:42:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<s32, Errno> Poll(std::vector<PollFD>& poll_fds, s32 timeout);
|
|
|
|
|
|
|
|
} // namespace Network
|