Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::udp_socket Class Referencefinal

UDP socket. More...

#include <udp_socket.hpp>

Public Member Functions

 udp_socket (wze::ipv4 const &ipv4)
 Explicit constructor.
 
wze::ipv4 const & ipv4 () const
 Gets the wze::ipv4 address of the server.
 
template<typename packet >
int32_t receive (packet &buffer)
 Receives data from the server.
 
template<typename packet >
void send (packet const &buffer)
 Sends data to the server.
 

Detailed Description

UDP socket.

Template Parameters
incomingType of the incoming data.
outgoingType of the outgoing data.
See also
net
udp_socket
tcp_socket

Definition at line 47 of file udp_socket.hpp.

Constructor & Destructor Documentation

◆ udp_socket()

wze::udp_socket::udp_socket ( wze::ipv4 const & ipv4)
inlineexplicit

Explicit constructor.

Parameters
ipv4IPv4 address of the server.
Exceptions
wze::exception<socket_error> udp_socket cannot be opened.
See also
net::resolve(std::string const& hostname, uint16_t port = 0)

Definition at line 55 of file udp_socket.hpp.

56 : _incoming{-1, nullptr, 0, 0, 0, {INADDR_NONE, 0}},
57 _outgoing{-1, nullptr, 0, 0, 0, {ipv4}} {
58 if (this->ipv4().host == INADDR_ANY ||
59 this->ipv4().host == INADDR_NONE) {
60 throw exception<socket_error>{{"Invalid IPv4 address"}};
61 }
62 _socket = {SDLNet_UDP_Open(0), SDLNet_UDP_Close};
63 if (!_socket) {
64 throw exception<socket_error>{{SDLNet_GetError()}};
65 }
66 }
wze::ipv4 const & ipv4() const
Gets the wze::ipv4 address of the server.

Member Function Documentation

◆ ipv4()

wze::ipv4 const & wze::udp_socket::ipv4 ( ) const
inlinenodiscard

Gets the wze::ipv4 address of the server.

Returns
wze::ipv4 address of the server.

Definition at line 72 of file udp_socket.hpp.

72 {
73 return _outgoing.address;
74 }

◆ receive()

template<typename packet >
int32_t wze::udp_socket::receive ( packet & buffer)
inlinenodiscard

Receives data from the server.

Parameters
bufferData buffer.
Returns
Integrity of the received data.
Return values
trueReceived appropriate data.
falseReceived invalid data.
Exceptions
wze::exception<socket_error> Data cannot be received properly.
See also
send(outgoing const& buffer)

Definition at line 86 of file udp_socket.hpp.

86 {
87 static_assert(sizeof(buffer) <= std::numeric_limits<int32_t>::max());
88 _incoming.data = static_cast<uint8_t*>(&buffer);
89 _incoming.len = 0;
90 _incoming.maxlen = sizeof(buffer);
91 switch (SDLNet_UDP_Recv(_socket.get(), &_incoming)) {
92 case 1:
93 if (_incoming.address.host == ipv4().host &&
94 _incoming.address.port == ipv4().port) {
95 return _incoming.len;
96 }
97 case 0:
98 return 0;
99 default:
100 throw exception<socket_error>{{SDLNet_GetError()}};
101 }
102 }

◆ send()

template<typename packet >
void wze::udp_socket::send ( packet const & buffer)
inline

Sends data to the server.

Parameters
bufferData buffer.
Exceptions
wze::exceptionData cannot be sent properly.
See also
receive(incoming& buffer)

Definition at line 110 of file udp_socket.hpp.

110 {
111 static_assert(sizeof(buffer) <= std::numeric_limits<int32_t>::max());
112 _outgoing.data = static_cast<uint8_t*>(&buffer);
113 _outgoing.len = sizeof(buffer);
114 _outgoing.maxlen = sizeof(buffer);
115 if (!static_cast<bool>(
116 SDLNet_UDP_Send(_socket.get(), -1, &_outgoing))) {
117 throw exception<socket_error>{{SDLNet_GetError()}};
118 }
119 }

The documentation for this class was generated from the following file: