summaryrefslogtreecommitdiffstats
path: root/src/core/network/sockets.h
blob: 1682cbd4e457bca709adf7b2f05d1b9b767c312e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <utility>

#if defined(_WIN32)
#include <winsock.h>
#elif !defined(__unix__)
#error "Platform not implemented"
#endif

#include "common/common_types.h"
#include "core/network/network.h"

// TODO: C++20 Replace std::vector usages with std::span

namespace Network {

class Socket {
public:
    struct AcceptResult {
        std::unique_ptr<Socket> socket;
        SockAddrIn sockaddr_in;
    };

    explicit Socket() = default;
    ~Socket();

    Socket(const Socket&) = delete;
    Socket& operator=(const Socket&) = delete;

    Socket(Socket&& rhs) noexcept;

    // Avoid closing sockets implicitly
    Socket& operator=(Socket&&) noexcept = delete;

    Errno Initialize(Domain domain, Type type, Protocol protocol);

    Errno Close();

    std::pair<AcceptResult, Errno> Accept();

    Errno Connect(SockAddrIn addr_in);

    std::pair<SockAddrIn, Errno> GetPeerName();

    std::pair<SockAddrIn, Errno> GetSockName();

    Errno Bind(SockAddrIn addr);

    Errno Listen(s32 backlog);

    Errno Shutdown(ShutdownHow how);

    std::pair<s32, Errno> Recv(u32 flags, std::vector<u8>& message);

    std::pair<s32, Errno> RecvFrom(u32 flags, std::vector<u8>& message, SockAddrIn* addr);

    std::pair<s32, Errno> Send(const std::vector<u8>& message, u32 flags);

    std::pair<s32, Errno> SendTo(u32 flags, const std::vector<u8>& message, const SockAddrIn* addr);

    Errno SetLinger(bool enable, u32 linger);

    Errno SetReuseAddr(bool enable);

    Errno SetBroadcast(bool enable);

    Errno SetSndBuf(u32 value);

    Errno SetRcvBuf(u32 value);

    Errno SetSndTimeo(u32 value);

    Errno SetRcvTimeo(u32 value);

    Errno SetNonBlock(bool enable);

    bool IsOpened() const;

#if defined(_WIN32)
    SOCKET fd = INVALID_SOCKET;
#elif defined(__unix__)
    int fd = -1;
#endif
};

std::pair<s32, Errno> Poll(std::vector<PollFD>& poll_fds, s32 timeout);

} // namespace Network