17#ifndef _FGE_C_SOCKET_HPP_INCLUDED_
18#define _FGE_C_SOCKET_HPP_INCLUDED_
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/network/C_ipAddress.hpp"
32#define FGE_SOCKET_ETHERNET_MTU 1500
33#define FGE_SOCKET_IPV4_MIN_MTU 576
34#define FGE_SOCKET_IPV6_MIN_MTU 1280
35#define FGE_SOCKET_IPV4_HEADER_SIZE 20
36#define FGE_SOCKET_IPV6_HEADER_SIZE 40
37#define FGE_SOCKET_UDP_HEADER_SIZE 8
44#define FGE_SOCKET_MAX_MTU (16384)
46#define FGE_SOCKET_FULL_DATAGRAM_SIZE (0xFFFF)
47#define FGE_SOCKET_IPV4_MAX_DATAGRAM_SIZE \
48 (FGE_SOCKET_FULL_DATAGRAM_SIZE - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
49#define FGE_SOCKET_IPV6_MAX_DATAGRAM_SIZE \
50 (FGE_SOCKET_FULL_DATAGRAM_SIZE - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
51#define FGE_SOCKET_IPV4_MAX_DATAGRAM_MTU_SIZE \
52 (FGE_SOCKET_IPV4_MIN_MTU - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
53#define FGE_SOCKET_IPV6_MAX_DATAGRAM_MTU_SIZE \
54 (FGE_SOCKET_IPV6_MIN_MTU - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
55#define FGE_SOCKET_IPV4_MAX_DATAGRAM_ETHMTU_SIZE \
56 (FGE_SOCKET_ETHERNET_MTU - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
57#define FGE_SOCKET_IPV6_MAX_DATAGRAM_ETHMTU_SIZE \
58 (FGE_SOCKET_ETHERNET_MTU - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
60#define FGE_SOCKET_TCP_DEFAULT_BUFFERSIZE 2048
77 using SocketDescriptor = uint64_t;
79 using SocketDescriptor =
unsigned int;
82 using SocketDescriptor = int;
104 ERR_SUCCESS = ERR_NOERROR,
105 ERR_DONE = ERR_NOERROR,
109 ERR_DISCONNECTED = 3,
112 ERR_ALREADYCONNECTED = 5,
114 ERR_TOOMANYSOCKET = 7,
118 ERR_INVALIDARGUMENT = 9,
121 ERR_UNKNOWN = ERR_UNSUCCESS
132 std::string _description;
134 std::vector<Data> _data;
152 [[nodiscard]]
inline IpAddress::Types
getAddressType()
const {
return this->g_addressType; }
327 [[nodiscard]]
static std::vector<AdapterInfo>
getAdaptersInfo(IpAddress::Types type = IpAddress::Types::None);
336 Socket& operator=(Socket
const& r) =
delete;
337 Socket(Socket
const& r) =
delete;
340 explicit Socket(
Types type, IpAddress::Types addressType = IpAddress::Types::Ipv4);
344 IpAddress::Types g_addressType{IpAddress::Types::Ipv4};
345 SocketDescriptor g_socket;
354class FGE_API SocketUdp :
public Socket
357 explicit SocketUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
358 SocketUdp(IpAddress::Types addressType,
bool blocking,
bool broadcast);
359 ~SocketUdp()
override =
default;
488 SocketUdp& operator=(SocketUdp&& r)
noexcept;
491 std::vector<uint8_t> g_buffer;
499class FGE_API SocketTcp :
public Socket
502 explicit SocketTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
503 explicit SocketTcp(IpAddress::Types addressType,
bool blocking);
504 ~SocketTcp()
override =
default;
559 Errors send(
void const* data, std::size_t size, std::size_t& sent);
569 Errors receive(
void* data, std::size_t size, std::size_t& received, uint32_t timeoutms);
607 SocketTcp& operator=(SocketTcp&& r)
noexcept;
610 std::size_t g_receivedSize;
611 std::size_t g_wantedSize;
612 std::vector<uint8_t> g_buffer;
620class FGE_API SocketListenerTcp :
public Socket
623 explicit SocketListenerTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
624 explicit SocketListenerTcp(IpAddress::Types addressType,
bool blocking);
625 ~SocketListenerTcp()
override =
default;
650 SocketListenerTcp& operator=(SocketListenerTcp&& r)
noexcept;
A class to represent an IP address.
Definition C_ipAddress.hpp:57
Definition C_packet.hpp:52
Errors accept(SocketTcp &socket)
Accept a new connection.
Errors create() override
Create the socket listener.
Errors listen(Port port, IpAddress const &address)
Start listening for new connections from a port.
A wrapper for TCP sockets inheriting from Socket.
Definition C_socket.hpp:500
Errors receive(Packet &packet, uint32_t timeoutms)
Receive a packet from the connected remote address with a timeout.
Errors create() override
Create a socket.
void flush()
Flush the internal data buffer.
Errors send(Packet &packet)
Send a Packet to the connected remote address.
Errors receive(void *data, std::size_t size, std::size_t &received)
Receive data from the connected remote address.
Errors sendAndReceive(Packet &sendPacket, Packet &receivePacket, uint32_t timeoutms)
Utility function to send and receive data.
Errors connect(IpAddress const &remoteAddress, Port remotePort, uint32_t timeoutms)
Connect to a remote address.
Errors create(SocketDescriptor sck)
Create the socket with an existing descriptor.
Errors send(void const *data, std::size_t size)
Send data to the connected remote address.
Errors send(void const *data, std::size_t size, std::size_t &sent)
Send data to the connected remote address.
Errors receive(Packet &packet)
Receive a Packet from the connected remote address.
Errors bind(Port port, IpAddress const &address)
Bind the socket to a local address and port.
Errors sendTo(void const *data, std::size_t size, IpAddress const &remoteAddress, Port remotePort)
Send data to the specified address.
Errors send(Packet &packet)
Send a Packet to the connected remote address.
Errors send(void const *data, std::size_t size)
Send data to the connected remote address.
Errors create() override
Create a new socket.
Errors connect(IpAddress const &remoteAddress, Port remotePort)
Connect the socket to a remote address and port.
Errors sendTo(Packet &packet, IpAddress const &remoteAddress, Port remotePort)
Send a Packet to the specified address.
Errors receiveFrom(void *data, std::size_t size, std::size_t &received, IpAddress &remoteAddress, Port &remotePort)
Receive data from an unspecified remote address.
Errors receive(void *data, std::size_t size, std::size_t &received)
Receive data from the connected remote address.
Errors receiveFrom(Packet &packet, IpAddress &remoteAddress, Port &remotePort)
Receive a Packet from an unspecified remote address.
Errors receive(Packet &packet)
Receive a Packet from the connected remote address.
static std::optional< uint16_t > retrieveAdapterMTUForDestination(IpAddress const &destination)
Helper to retrieve the MTU of the adapter used to reach the destination ip address.
Errors setBlocking(bool mode)
Set the blocking mode of the socket.
IpAddress::Types getAddressType() const
Get the address type of the socket.
Definition C_socket.hpp:152
std::optional< uint16_t > retrieveCurrentAdapterMTU() const
Retrieve the current adapter MTU.
Errors setBroadcastOption(bool mode)
Set if the socket support broadcast.
Errors
The error codes.
Definition C_socket.hpp:102
Errors setReuseAddress(bool mode)
Set if the socket reuse the address.
Port getLocalPort() const
Get the local port of the socket.
static int getPlatformSpecifiedError()
Get the last platform specific error code.
Errors setDontFragment(bool mode)
Set if the socket should append DF flag to the packet.
Types getType() const
Get the type of the socket.
Definition C_socket.hpp:144
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.
Errors setIpv6Only(bool mode)
Set if ipv6 socket should only use ipv6.
Port getRemotePort() const
Get the remote port of the socket.
Errors select(bool read, uint32_t timeoutms)
Check the socket for readability or writability.
Types
The possible types of sockets.
Definition C_socket.hpp:90
static bool initSocket()
Init the low-level socket library.
IpAddress getRemoteAddress() const
Get the remote address of the socket.
virtual Errors create()=0
Create a new socket.
static void uninitSocket()
Shutdown the low-level socket library.
IpAddress getLocalAddress() const
Get the local address of the socket.
static std::vector< AdapterInfo > getAdaptersInfo(IpAddress::Types type=IpAddress::Types::None)
Retrieve adapters information.
bool isBlocking() const
Check if the socket is in blocking mode.
Definition C_socket.hpp:127
Definition C_socket.hpp:125