libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
Socket.hxx
1#pragma once
2
3// C++
4#include <string_view>
5#include <string>
6
7// cosmos
8#include <cosmos/fs/FDFile.hxx>
9#include <cosmos/net/SocketOptions.hxx>
10#include <cosmos/net/types.hxx>
11#include <cosmos/utils.hxx>
12
13namespace cosmos {
14
15class SocketAddress;
16class SendMessageHeader;
17class ReceiveMessageHeader;
18
20
37class COSMOS_API Socket :
38 public FDFile {
39
40public: // types
41
43 enum class Direction : int {
44 READ = SHUT_RD,
45 WRITE = SHUT_WR,
46 READ_WRITE = SHUT_RDWR
47 };
48
51
52public: // functions
53
54 auto sockOptions() {
55 return SocketOptions{m_fd};
56 }
57
58 auto sockOptions() const {
59 return SocketOptions{m_fd};
60 }
61
63 void getSockName(SocketAddress &addr);
64
66
74 void shutdown(const Direction dir);
75
76protected: // functions
77
79 Socket(
80 const SocketFamily family,
81 const SocketType type,
82 const SocketFlags flags = SocketFlags{SocketFlag::CLOEXEC},
83 const SocketProtocol protocol = SocketProtocol::DEFAULT);
84
86 Socket(FileDescriptor fd, const AutoCloseFD auto_close) :
87 FDFile{fd, auto_close}
88 {}
89
91
99 void bind(const SocketAddress &addr);
100
102
116 void connect(const SocketAddress &addr);
117
119
127 void listen(const size_t backlog);
128
130
143 FileDescriptor accept(SocketAddress *addr);
144
146
150 size_t send(const void *buf, size_t length, const MessageFlags flags = MessageFlags{});
151
153 size_t send(const std::string_view data, const MessageFlags flags = MessageFlags{}) {
154 return send(data.data(), data.size(), flags);
155 }
156
158
167 size_t sendTo(const void *buf, size_t length, const SocketAddress &addr,
168 const MessageFlags flags = MessageFlags{});
169
171 size_t sendTo(const std::string_view data, const SocketAddress &addr,
172 const MessageFlags flags = MessageFlags{}) {
173 return sendTo(data.data(), data.size(), addr, flags);
174 }
175
177
197 void sendMessage(SendMessageHeader &header, const SocketAddress* addr = nullptr);
198
200
204 size_t receive(void *buf, size_t length, const MessageFlags flags = MessageFlags{});
205
207
217 std::pair<size_t, AddressFilledIn> receiveFrom(
218 void *buf, size_t length, SocketAddress &addr,
219 const MessageFlags flags = MessageFlags{});
220
222
244 AddressFilledIn receiveMessage(ReceiveMessageHeader &header, SocketAddress *addr = nullptr);
245};
246
247} // end ns
File objects that are opened from existing FileDescriptor objects.
Definition FDFile.hxx:16
Thin Wrapper around OS file descriptors.
Strong template type to wrap boolean values in a named type.
Definition utils.hxx:50
Base class for all types of socket addresses.
Generic socket level option setter/getter helper.
Base class for Socket types with ownership of a FileDescriptor.
Definition Socket.hxx:38
Direction
Type used in Socket::shutdown().
Definition Socket.hxx:43
Socket(FileDescriptor fd, const AutoCloseFD auto_close)
Creates a new socket from the given existing file descriptor.
Definition Socket.hxx:86
size_t sendTo(const std::string_view data, const SocketAddress &addr, const MessageFlags flags=MessageFlags{})
Variant of sendTo() that takes a std::string_view container instead of a raw input buffer.
Definition Socket.hxx:171
size_t send(const std::string_view data, const MessageFlags flags=MessageFlags{})
Variant of send() that takes a std::string_view container instead of a raw input buffer.
Definition Socket.hxx:153
SocketProtocol
Specific protocol to use on a socket.
Definition types.hxx:71