Repository navigation
Expand file tree
/
Copy pathSocket.hpp
More file actions
102 lines (80 loc) · 3.62 KB
/
Copy pathSocket.hpp
File metadata and controls
102 lines (80 loc) · 3.62 KB
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
95
96
97
98
99
100
101
102
/*
** Project LibCppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
** Last update Sun Oct 25 11:38:13 2026 Francois Michaut
**
** Socket.hpp : Portable C++ socket class
*/
#pragma once
#include "CppSockets/OSDetection.hpp"
#include "CppSockets/internal/SocketInit.hpp"
// TODO: move the RawSocketType in CppSockets namespace
#ifdef OS_WINDOWS
#define NOMINMAX
#include <winsock2.h>
using RawSocketType=SOCKET;
using socklen_t=int;
using SockOptType=char;
#else
#include <sys/socket.h>
using RawSocketType=int;
using SockOptType=void;
#endif
#include <memory>
#include "CppSockets/Address.hpp"
namespace CppSockets {
class Socket : SocketInit {
public:
Socket();
Socket(int domain, int type, int protocol);
Socket(RawSocketType fd, bool connected);
virtual ~Socket();
// TODO Maybe enable copy with dup(2) ?
Socket(const Socket &other) = delete;
Socket(Socket &&other) noexcept;
auto operator=(const Socket &other) -> Socket & = delete;
auto operator=(Socket &&other) noexcept -> Socket &;
virtual void close();
virtual auto read(std::size_t len = -1) -> std::string;
virtual auto read(char *buff, std::size_t size) -> std::size_t;
virtual auto write(std::string_view buff) -> std::size_t;
virtual auto write(const char *buff, std::size_t len) -> std::size_t;
auto set_reuseaddr(bool value) -> int;
auto getsockopt(int level, int optname, SockOptType *optval, socklen_t *optlen) -> int;
auto setsockopt(int level, int optname, const SockOptType *optval, socklen_t optlen) -> int;
virtual auto connect(const IEndpoint &endpoint) -> int;
virtual auto connect(const std::string &addr, uint16_t port) -> int;
auto bind(const IEndpoint &endpoint) -> int;
auto bind(const std::string &addr, uint16_t port) -> int;
auto listen(int backlog) -> int;
auto accept(void *addr_out = nullptr) -> std::unique_ptr<Socket>;
void set_blocking(bool val);
[[nodiscard]] auto get_fd() const -> RawSocketType { return m_sockfd; }
[[nodiscard]] auto get_type() const -> int { return m_type; }
[[nodiscard]] auto get_domain() const -> int { return m_domain; }
[[nodiscard]] auto get_protocol() const -> int { return m_protocol; }
// TODO: Allow to get Endpoint
[[nodiscard]] auto connected() const -> bool { return m_is_connected; }
static auto get_errno() -> int;
static auto strerror(int err) -> std::string;
static auto strerror() -> std::string;
protected:
static void throw_errno_error(std::string message);
static void throw_errno_error(std::string message, int err);
static auto getsockopt(int fd, int level, int optname, SockOptType *optval, socklen_t *optlen) -> int;
static auto getsockopt(int fd, int level, int optname, SockOptType *optval, socklen_t optlen) -> int { return Socket::getsockopt(fd, level, optname, optval, &optlen); }
auto bind(std::uint32_t addr, uint16_t port) -> int;
void set_connected(bool status) { m_is_connected = status; };
void create_socket();
virtual auto acceptImpl(void *addr_out) -> Socket *;
private:
int m_domain = 0;
int m_type = 0;
int m_protocol = 0;
RawSocketType m_sockfd;
bool m_is_connected = false;
};
}