TCP socket.
More...
#include <tcp_socket.hpp>
|
| tcp_socket (wze::ipv4 ipv4) |
| Explicit constructor.
|
|
wze::ipv4 | ipv4 () const |
| Gets the wze::ipv4 address of the server.
|
|
template<typename packet > |
void | send (packet const &buffer) |
| Receives data from the server.
|
|
template<typename packet > |
int32_t | receive (packet &buffer) |
| Sends data to the server.
|
|
TCP socket.
- Template Parameters
-
incoming | Type of the incoming data. |
outgoing | Type of the outgoing data. |
- See also
- net
-
udp_socket
-
tcp_socket
Definition at line 47 of file tcp_socket.hpp.
◆ tcp_socket()
wze::tcp_socket::tcp_socket |
( |
wze::ipv4 | ipv4 | ) |
|
|
inlineexplicit |
Explicit constructor.
- Parameters
-
ipv4 | IPv4 address of the server. |
- Exceptions
-
- See also
- net::resolve(std::string const& hostname, uint16_t port = 0)
Definition at line 55 of file tcp_socket.hpp.
55 {
56 if (
ipv4.host == INADDR_ANY ||
ipv4.host == INADDR_NONE) {
57 throw exception<socket_error>{{"Invalid IPv4 address"}};
58 }
59 _socket_set = {SDLNet_AllocSocketSet(1), SDLNet_FreeSocketSet};
60 if (!_socket_set) {
61 throw exception<socket_error>{{SDLNet_GetError()}};
62 }
63 _socket = {SDLNet_TCP_Open(&
ipv4), SDLNet_TCP_Close};
64 if (!_socket) {
65 throw exception<socket_error>{{SDLNet_GetError()}};
66 }
67 if (SDLNet_TCP_AddSocket(_socket_set.get(), _socket.get()) != 1) {
68 throw exception<socket_error>{{SDLNet_GetError()}};
69 }
70 }
wze::ipv4 ipv4() const
Gets the wze::ipv4 address of the server.
◆ ipv4()
Gets the wze::ipv4 address of the server.
- Returns
- wze::ipv4 address of the server.
Definition at line 76 of file tcp_socket.hpp.
76 {
77 return *SDLNet_TCP_GetPeerAddress(_socket.get());
78 }
◆ send()
template<typename packet >
void wze::tcp_socket::send |
( |
packet const & | buffer | ) |
|
|
inline |
Receives data from the server.
- Parameters
-
- Returns
- Integrity of the received data.
- Return values
-
true | Received appropriate data. |
false | Received invalid data. |
- Exceptions
-
- See also
- send(outgoing const& buffer)
Definition at line 89 of file tcp_socket.hpp.
89 {
90 static_assert(sizeof(buffer) <= std::numeric_limits<int32_t>::max());
91 if (SDLNet_TCP_Send(_socket.get(), &buffer, sizeof(buffer)) !=
92 sizeof(buffer)) {
93 throw exception<socket_error>{{SDLNet_GetError()}};
94 }
95 }
◆ receive()
template<typename packet >
int32_t wze::tcp_socket::receive |
( |
packet & | buffer | ) |
|
|
inlinenodiscard |
Sends data to the server.
- Parameters
-
- Exceptions
-
- See also
- receive(incoming& buffer)
Definition at line 103 of file tcp_socket.hpp.
103 {
104 static_assert(sizeof(buffer) <= std::numeric_limits<int32_t>::max());
105 if (!static_cast<bool>(SDLNet_CheckSockets(_socket_set.get(), 0)) ||
106
107 !static_cast<bool>(SDLNet_SocketReady(_socket.get()))) {
108 return 0;
109 }
110 int32_t size{SDLNet_TCP_Recv(_socket.get(), &buffer, sizeof(buffer))};
111 if (size <= 0) {
112 throw exception<socket_error>{{SDLNet_GetError()}};
113 }
114 return size;
115 }
The documentation for this class was generated from the following file: