Wizard Engine
2D cross-platform game engine built around SDL2
Loading...
Searching...
No Matches
udp_socket.hpp
Go to the documentation of this file.
1
/*
2
Wizard Engine
3
Copyright (C) 2023-2024 Zana Domán
4
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
8
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
12
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
20
*/
21
30
#ifndef WIZARD_ENGINE_UDP_SOCKET_HPP
31
#define WIZARD_ENGINE_UDP_SOCKET_HPP
32
33
#include <
wizard_engine/errors.hpp
>
34
#include <
wizard_engine/exception.hpp
>
35
#include <
wizard_engine/export.hpp
>
36
#include <
wizard_engine/net.hpp
>
37
38
namespace
wze
{
47
class
udp_socket
final {
48
public
:
55
explicit
udp_socket
(
wze::ipv4
const
&
ipv4
)
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
}
67
72
[[nodiscard]]
wze::ipv4
const
&
ipv4
()
const
{
73
return
_outgoing.address;
74
}
75
86
template
<
typename
packet> [[nodiscard]] int32_t
receive
(packet& buffer) {
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
}
103
110
template
<
typename
packet>
void
send
(packet
const
& buffer) {
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
}
120
121
private
:
122
UDPpacket _incoming;
123
UDPpacket _outgoing;
124
std::shared_ptr<_UDPsocket> _socket;
125
};
126
}
/* namespace wze */
127
128
#endif
/* WIZARD_ENGINE_UDP_SOCKET_HPP */
wze::exception
Generic exception.
Definition
exception.hpp:41
wze::udp_socket
UDP socket.
Definition
udp_socket.hpp:47
wze::udp_socket::udp_socket
udp_socket(wze::ipv4 const &ipv4)
Explicit constructor.
Definition
udp_socket.hpp:55
wze::udp_socket::receive
int32_t receive(packet &buffer)
Receives data from the server.
Definition
udp_socket.hpp:86
wze::udp_socket::send
void send(packet const &buffer)
Sends data to the server.
Definition
udp_socket.hpp:110
wze::udp_socket::ipv4
wze::ipv4 const & ipv4() const
Gets the wze::ipv4 address of the server.
Definition
udp_socket.hpp:72
errors.hpp
Error types.
exception.hpp
Generic exception.
export.hpp
Export header of the Wizard Engine.
wze
Wizard Engine.
Definition
animatable.hpp:34
wze::ipv4
IPaddress ipv4
IPv4 address.
Definition
net.hpp:39
net.hpp
Network modul.
include
wizard_engine
udp_socket.hpp
Generated on Wed Dec 11 2024 10:33:00 for Wizard Engine by
1.12.0