Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
tcp_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_TCP_SOCKET_HPP
31#define WIZARD_ENGINE_TCP_SOCKET_HPP
32
36#include <wizard_engine/net.hpp>
37
38namespace wze {
47class tcp_socket final {
48 public:
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 }
71
76 [[nodiscard]] wze::ipv4 ipv4() const {
77 return *SDLNet_TCP_GetPeerAddress(_socket.get());
78 }
79
89 template <typename packet> void send(packet const& buffer) {
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 }
96
103 template <typename packet> [[nodiscard]] int32_t receive(packet& buffer) {
104 static_assert(sizeof(buffer) <= std::numeric_limits<int32_t>::max());
105 if (!static_cast<bool>(SDLNet_CheckSockets(_socket_set.get(), 0)) ||
106 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
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 }
116
117 private:
118 std::shared_ptr<_SDLNet_SocketSet> _socket_set;
119 std::shared_ptr<_TCPsocket> _socket;
120};
121} /* namespace wze */
122
123#endif /* WIZARD_ENGINE_TCP_SOCKET_HPP */
Generic exception.
Definition exception.hpp:41
TCP socket.
void send(packet const &buffer)
Receives data from the server.
tcp_socket(wze::ipv4 ipv4)
Explicit constructor.
int32_t receive(packet &buffer)
Sends data to the server.
wze::ipv4 ipv4() const
Gets the wze::ipv4 address of the server.
Error types.
Generic exception.
Export header of the Wizard Engine.
Wizard Engine.
IPaddress ipv4
IPv4 address.
Definition net.hpp:39
Network modul.