EasySockets v2.2.1
A simple, cross platform socket library for C++.
Loading...
Searching...
No Matches
PosixSocket.hpp
1#pragma once
2
3#include <EasySockets/Api/SocketTypes.hpp>
4#include <EasySockets/Api/Addresses.hpp>
5#include <EasySockets/Api/Documentation.hpp>
6#include <netdb.h>
7
8namespace es
9{
11 ES_API_DOC(socket)
24 {
25 public:
26 ES_API_DOC(socket_default_constructor)
32
33 ES_API_DOC(socket_constructor)
39 PosixSocket(IpVersion ip_version, Protocol protocol);
40
42
43 PosixSocket(const PosixSocket&) = delete;
44
46
47 PosixSocket(PosixSocket&& other) noexcept;
48
49 PosixSocket& operator=(PosixSocket&& other) noexcept;
50
51 ES_API_DOC(socket_bind_to)
58 void bind_to(const EndPoint& end_point);
59
60 ES_API_DOC(socket_connect_to)
65 void connect_to(const EndPoint& end_point);
66
67 ES_API_DOC(socket_listen_for_connections)
77 void listen_for_connections(int backlog);
78
79 ES_API_DOC(socket_accept_connection)
87
88 ES_API_DOC(socket_receive_data)
96 int64_t receive_data(char* buffer, int buffer_size);
97
98 ES_API_DOC(socket_receive_data_from)
108 int64_t receive_data_from(char* buffer, int buffer_size, EndPoint& sender_end_point);
109
110 ES_API_DOC(socket_send_data)
118 int64_t send_data(const char* buffer, int buffer_size);
119
120 ES_API_DOC(socket_send_data_to)
129 int64_t send_data_to(const char* buffer, int buffer_size, const EndPoint& end_point);
130
131 ES_API_DOC(socket_close)
138 void close();
139
140 ES_API_DOC(make_connected_tcp)
146 static PosixSocket make_connected_tcp(const EndPoint& end_point);
147
148 ES_API_DOC(make_connected_udp)
154 static PosixSocket make_connected_udp(const EndPoint& end_point);
155
156 ES_API_DOC(make_bound_tcp)
165
166 ES_API_DOC(make_bound_udp)
175
176 private:
177 struct SocketData
178 {
179 int af;
180 int type;
181 int protocol;
182 };
183
184 static addrinfo* resolve_address(const SocketData& socket_data, const EndPoint& end_point); // must call freeaddrinfo afterward
185
186 int m_socket;
187 SocketData m_socket_data;
188 };
189}
void listen_for_connections(int backlog)
Makes the socket start listening for incoming connections.
Definition PosixSocket.cpp:123
PosixSocket()
Default constructs a socket.
Definition PosixSocket.cpp:13
static PosixSocket make_connected_tcp(const EndPoint &end_point)
Makes a TCP socket connected to the given endpoint.
Definition PosixSocket.cpp:231
void connect_to(const EndPoint &end_point)
Connects a socket to the given end point.
Definition PosixSocket.cpp:103
PosixSocket(const PosixSocket &)=delete
PosixSocket accept_connection()
Accepts incoming connections.
Definition PosixSocket.cpp:129
static PosixSocket make_bound_udp(Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
Makes a UDP socket bound to the given port.
Definition PosixSocket.cpp:305
static PosixSocket make_bound_tcp(Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
Makes a TCP socket bound to the given port.
Definition PosixSocket.cpp:291
static PosixSocket make_connected_udp(const EndPoint &end_point)
Makes a UDP socket connected to the given endpoint.
Definition PosixSocket.cpp:261
PosixSocket & operator=(const PosixSocket &)=delete
void bind_to(const EndPoint &end_point)
Binds a socket to the given end point.
Definition PosixSocket.cpp:83
int64_t send_data_to(const char *buffer, int buffer_size, const EndPoint &end_point)
Sends data to the provided end point.
Definition PosixSocket.cpp:187
void close()
Closes the socket.
Definition PosixSocket.cpp:209
int64_t receive_data_from(char *buffer, int buffer_size, EndPoint &sender_end_point)
Waits to receive data from any endpoint.
Definition PosixSocket.cpp:152
int64_t send_data(const char *buffer, int buffer_size)
Sends data to the connected end point.
Definition PosixSocket.cpp:178
int64_t receive_data(char *buffer, int buffer_size)
Waits to receive data from the connected end point.
Definition PosixSocket.cpp:143
Protocol
Layer 4/transport layer protocol.
Definition SocketTypes.hpp:26
IpVersion
Version of IP address.
Definition SocketTypes.hpp:11
uint16_t Port
16-bit port number.
Definition Addresses.hpp:20
@ DUAL_STACK
An IPv6 address which allows for conversions from IPv4.
Definition SocketTypes.hpp:17
Definition Addresses.hpp:7
Represents the address + port that identifies a connection. Initialized by {Address,...
Definition Addresses.hpp:29