FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_socket.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_SOCKET_HPP_INCLUDED_
18#define _FGE_C_SOCKET_HPP_INCLUDED_
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/network/C_ipAddress.hpp"
29#include <cstdint>
30#include <vector>
31
32#define FGE_SOCKET_MAXDATAGRAMSIZE 65507
33#define FGE_SOCKET_TCP_DEFAULT_BUFFERSIZE 2048
34
35namespace fge::net
36{
37
38class Packet;
39
45class FGE_API Socket
46{
47public:
48#ifdef _WIN32
49 #if defined(_WIN64)
50 using SocketDescriptor = uint64_t;
51 #else
52 using SocketDescriptor = unsigned int;
53 #endif
54#else
55 using SocketDescriptor = int;
56#endif
57
62 enum Type
63 {
64 TYPE_UDP,
65 TYPE_TCP,
66 TYPE_LISTENER_TCP
67 };
68
73 enum Error
74 {
75 ERR_NOERROR = 0,
76 ERR_SUCCESS = ERR_NOERROR,
77 ERR_DONE = ERR_NOERROR,
78
79 ERR_PARTIAL,
80 ERR_NOTREADY,
81 ERR_DISCONNECTED,
82 ERR_REFUSED,
83
84 ERR_ALREADYCONNECTED,
85 ERR_ALREADYUSED,
86 ERR_TOOMANYSOCKET,
87
88 ERR_NOTINIT,
89
90 ERR_INVALIDARGUMENT,
91
92 ERR_UNSUCCESS,
93 ERR_UNKNOWN = ERR_UNSUCCESS
94 };
95
103 [[nodiscard]] inline Type getType() const { return this->g_type; }
111 [[nodiscard]] inline IpAddress::Types getAddressType() const { return this->g_addressType; }
119 void setAddressType(IpAddress::Types type);
120
126 virtual Error create() = 0;
130 void close();
138 [[nodiscard]] bool isValid() const;
139
148 [[nodiscard]] fge::net::Port getLocalPort() const;
167 [[nodiscard]] fge::net::Port getRemotePort() const;
178
184 [[nodiscard]] bool isBlocking() const;
185
192 Error setBlocking(bool mode);
213
226 Error select(bool read, uint32_t timeoutms);
227
235 static bool initSocket();
239 static void uninitSocket();
240
246 [[nodiscard]] static int getPlatformSpecifiedError();
247
248 fge::net::Socket& operator=(fge::net::Socket const& r) = delete;
249 Socket(fge::net::Socket const& r) = delete;
250
251protected:
252 explicit Socket(Type type, IpAddress::Types addressType = IpAddress::Types::Ipv4);
253 virtual ~Socket() = default;
254
255 Type g_type;
256 IpAddress::Types g_addressType{IpAddress::Types::Ipv4};
257 SocketDescriptor g_socket;
258 bool g_isBlocking;
259};
260
266class FGE_API SocketUdp : public fge::net::Socket
267{
268public:
269 explicit SocketUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
270 SocketUdp(IpAddress::Types addressType, bool blocking, bool broadcast);
271 ~SocketUdp() override;
272
274
287 fge::net::Socket::Error connect(fge::net::IpAddress const& remoteAddress, fge::net::Port remotePort);
298 fge::net::Socket::Error bind(fge::net::Port port, IpAddress const& address);
299
311 fge::net::Socket::Error send(void const* data, std::size_t size);
322 sendTo(void const* data, std::size_t size, IpAddress const& remoteAddress, fge::net::Port remotePort);
334 std::size_t size,
335 std::size_t& received,
336 fge::net::IpAddress& remoteAddress,
337 fge::net::Port& remotePort);
350 fge::net::Socket::Error receive(void* data, std::size_t size, std::size_t& received);
351
372 fge::net::Socket::Error sendTo(fge::net::Packet& packet, IpAddress const& remoteAddress, fge::net::Port remotePort);
382 receiveFrom(fge::net::Packet& packet, fge::net::IpAddress& remoteAddress, fge::net::Port& remotePort);
394
395 fge::net::SocketUdp& operator=(fge::net::SocketUdp&& r) noexcept;
396
397private:
398 std::vector<uint8_t> g_buffer;
399};
400
406class FGE_API SocketTcp : public fge::net::Socket
407{
408public:
409 explicit SocketTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
410 explicit SocketTcp(IpAddress::Types addressType, bool blocking);
411 ~SocketTcp() override;
412
418 void flush();
419
429 fge::net::Socket::Error create(fge::net::Socket::SocketDescriptor sck);
439
449 connect(fge::net::IpAddress const& remoteAddress, fge::net::Port remotePort, uint32_t timeoutms);
450
458 fge::net::Socket::Error send(void const* data, std::size_t size);
467 fge::net::Socket::Error send(void const* data, std::size_t size, std::size_t& sent);
476 fge::net::Socket::Error receive(void* data, std::size_t size, std::size_t& received);
477
495
505 sendAndReceive(fge::net::Packet& sendPacket, fge::net::Packet& receivePacket, uint32_t timeoutms);
514
515 fge::net::SocketTcp& operator=(fge::net::SocketTcp&& r) noexcept;
516
517private:
518 std::size_t g_receivedSize;
519 std::size_t g_wantedSize;
520 std::vector<uint8_t> g_buffer;
521};
522
529{
530public:
531 explicit SocketListenerTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
532 explicit SocketListenerTcp(IpAddress::Types addressType, bool blocking);
533 ~SocketListenerTcp() override;
534
541
549 fge::net::Socket::Error listen(fge::net::Port port, fge::net::IpAddress const& address);
557
559};
560
561} // namespace fge::net
562
563#endif // _FGE_C_SOCKET_HPP_INCLUDED_
A class to represent an IP address.
Definition C_ipAddress.hpp:57
Definition C_packet.hpp:70
A wrapper for TCP listener sockets inheriting from Socket.
Definition C_socket.hpp:529
fge::net::Socket::Error create() override
Create the socket listener.
fge::net::Socket::Error accept(fge::net::SocketTcp &socket)
Accept a new connection.
fge::net::Socket::Error listen(fge::net::Port port, fge::net::IpAddress const &address)
Start listening for new connections from a port.
A wrapper for TCP sockets inheriting from Socket.
Definition C_socket.hpp:407
fge::net::Socket::Error send(fge::net::Packet &packet)
Send a fge::net::Packet to the connected remote address.
fge::net::Socket::Error receive(fge::net::Packet &packet)
Receive a fge::net::Packet from the connected remote address.
fge::net::Socket::Error sendAndReceive(fge::net::Packet &sendPacket, fge::net::Packet &receivePacket, uint32_t timeoutms)
Utility function to send and receive data.
fge::net::Socket::Error create(fge::net::Socket::SocketDescriptor sck)
Create the socket with an existing descriptor.
fge::net::Socket::Error send(void const *data, std::size_t size)
Send data to the connected remote address.
void flush()
Flush the internal data buffer.
fge::net::Socket::Error receive(void *data, std::size_t size, std::size_t &received)
Receive data from the connected remote address.
fge::net::Socket::Error send(void const *data, std::size_t size, std::size_t &sent)
Send data to the connected remote address.
fge::net::Socket::Error create() override
Create a socket.
fge::net::Socket::Error connect(fge::net::IpAddress const &remoteAddress, fge::net::Port remotePort, uint32_t timeoutms)
Connect to a remote address.
fge::net::Socket::Error receive(fge::net::Packet &packet, uint32_t timeoutms)
Receive a packet from the connected remote address with a timeout.
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:267
fge::net::Socket::Error create() override
Create a new socket.
fge::net::Socket::Error receiveFrom(void *data, std::size_t size, std::size_t &received, fge::net::IpAddress &remoteAddress, fge::net::Port &remotePort)
Receive data from an unspecified remote address.
fge::net::Socket::Error connect(fge::net::IpAddress const &remoteAddress, fge::net::Port remotePort)
Connect the socket to a remote address and port.
fge::net::Socket::Error sendTo(fge::net::Packet &packet, IpAddress const &remoteAddress, fge::net::Port remotePort)
Send a fge::net::Packet to the specified address.
fge::net::Socket::Error bind(fge::net::Port port, IpAddress const &address)
Bind the socket to a local address and port.
fge::net::Socket::Error sendTo(void const *data, std::size_t size, IpAddress const &remoteAddress, fge::net::Port remotePort)
Send data to the specified address.
fge::net::Socket::Error receive(fge::net::Packet &packet)
Receive a fge::net::Packet from the connected remote address.
fge::net::Socket::Error receive(void *data, std::size_t size, std::size_t &received)
Receive data from the connected remote address.
fge::net::Socket::Error send(fge::net::Packet &packet)
Send a fge::net::Packet to the connected remote address.
fge::net::Socket::Error send(void const *data, std::size_t size)
Send data to the connected remote address.
fge::net::Socket::Error receiveFrom(fge::net::Packet &packet, fge::net::IpAddress &remoteAddress, fge::net::Port &remotePort)
Receive a fge::net::Packet from an unspecified remote address.
A base class wrapper for low-level network functions.
Definition C_socket.hpp:46
IpAddress::Types getAddressType() const
Get the address type of the socket.
Definition C_socket.hpp:111
Error setBroadcastOption(bool mode)
Set if the socket support broadcast.
Type getType() const
Get the type of the socket.
Definition C_socket.hpp:103
static int getPlatformSpecifiedError()
Get the last platform specific error code.
virtual Error create()=0
Create a new socket.
fge::net::IpAddress getLocalAddress() const
Get the local address of the socket.
Error setReuseAddress(bool mode)
Set if the socket reuse the address.
fge::net::Port getLocalPort() const
Get the local port of the socket.
void close()
Close the socket.
void setAddressType(IpAddress::Types type)
Set the address type of the socket.
bool isValid() const
Check if the socket is valid.
fge::net::Port getRemotePort() const
Get the remote port of the socket.
fge::net::IpAddress getRemoteAddress() const
Get the remote address of the socket.
static bool initSocket()
Init the low-level socket library.
static void uninitSocket()
Shutdown the low-level socket library.
Type
The possible types of sockets.
Definition C_socket.hpp:63
Error select(bool read, uint32_t timeoutms)
Check the socket for readability or writability.
Error setBlocking(bool mode)
Set the blocking mode of the socket.
Error
The error codes.
Definition C_socket.hpp:74
bool isBlocking() const
Check if the socket is in blocking mode.