1 #ifndef LIBFORB_SOCKET_HPP 2 #define LIBFORB_SOCKET_HPP 8 #include <sys/socket.h> 11 #include <forb/stream/stream.hpp> 18 inline bool is_big_endian() {
22 } bint = {0x01020304};
24 return bint.c[0] == 1;
31 using sockaddr_in_t =
struct sockaddr_in;
32 using sockaddr_t =
struct sockaddr;
40 struct socket_create_t {
44 static constexpr
int AF_TYPE = AF_INET;
45 static constexpr
int PROTOCOL = SOCK_STREAM;
46 static constexpr socklen_t SOCKADDR_SIZE =
sizeof(sockaddr_in_t);
54 std::string _local_address =
"";
60 std::string _remote_address =
"";
66 bool _is_server =
false;
81 socket(
int sock_fd,
const std::string &remote_address,
int remote_port);
85 socket(
const std::string &remote_address,
int remote_port);
128 socket new_socket = socket_create_t();
129 new_socket.bind(address, port);
130 new_socket.listen(listen_queue_size);
136 socket new_socket = socket_create_t();
137 new_socket.connect(address, port);
145 static inline sockaddr_in_t build_sockaddr(
const std::string &address,
int port);
148 void _move_attributes(
socket &other) noexcept;
160 void bind(
const std::string &address,
int port);
163 void listen(
int listen_queue_size);
166 void connect(
const std::string &address,
int port);
175 void send(
const void *buffer, std::size_t size)
override;
179 void recv(
void *buffer, std::size_t size)
override;
182 void close() noexcept
override;
194 return !is_big_endian();
202 #endif // #ifndef LIBFORB_SOCKET_HPP static socket make_server(int port, int listen_queue_size=10)
Creates a new server socket.
Definition: socket.hpp:119
C++ wrapper of the Socket POSIX API, which uses IPv4 and blocking communication.
Definition: socket.hpp:28
type get_type() const override
Returns the type of the stream.
Definition: socket.hpp:185
bool require_marshal() const override
Returns true if the execution platform requires marshalling before sending data on the socket...
Definition: socket.hpp:193
Virtual class that represents a stream.
Definition: stream.hpp:69
~socket() override
Virtual destructor that closes the socket (if open).
Definition: socket.hpp:94
void close() noexcept override
Closes the socket, called by the virtual destructor.
Definition: socket.cpp:37
This function implements a memcpy abstract that is valid also for volatile data.
Definition: base_skeleton.hpp:15
void recv(void *buffer, std::size_t size) override
Blocking wrapper of the POSIX recv function.
Definition: socket.cpp:264
static socket make_client(const std::string &address, int port)
Creates a new client socket, connected to the specified remote host.
Definition: socket.hpp:135
void send(const void *buffer, std::size_t size) override
Blocking wrapper of the POSIX send function.
Definition: socket.cpp:228
type
The type of the stream.
Definition: stream.hpp:78
static socket make_server(const std::string &address, int port, int listen_queue_size=10)
Creates a new server socket.
Definition: socket.hpp:127
socket & operator=(const socket &other)=delete
This class does not support copy assignment.
socket accept()
Performs the accept operation on the given server socket.
Definition: socket.cpp:181
socket()=default
Default constructor, creates an empty socket, which cannot be used to exchange data.