-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.hpp
More file actions
93 lines (75 loc) · 3.14 KB
/
Socket.hpp
File metadata and controls
93 lines (75 loc) · 3.14 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
/*
** Project CppSockets, 2022
**
** Author Francois Michaut
**
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
** Last update Wed Aug 20 14:01:21 2025 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);
~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 &;
auto read(std::size_t len = -1) -> std::string;
auto read(char *buff, std::size_t size) -> std::size_t;
auto write(const std::string &buff) -> std::size_t;
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;
void close();
auto connect(const IEndpoint &endpoint) -> int;
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) -> char *;
static auto strerror() -> char *;
protected:
static auto getsockopt(int fd, int level, int optname, SockOptType *optval, socklen_t *optlen) -> int;
auto bind(std::uint32_t addr, uint16_t port) -> int;
void set_connected(bool status) { m_is_connected = status; };
private:
int m_domain = 0;
int m_type = 0;
int m_protocol = 0;
RawSocketType m_sockfd;
bool m_is_connected = false;
};
}