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
39#define FGE_SOCKET_FULL_DATAGRAM_SIZE (0xFFFF)
40#define FGE_SOCKET_IPV4_MAX_DATAGRAM_SIZE \
41 (FGE_SOCKET_FULL_DATAGRAM_SIZE - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
42#define FGE_SOCKET_IPV6_MAX_DATAGRAM_SIZE \
43 (FGE_SOCKET_FULL_DATAGRAM_SIZE - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
44#define FGE_SOCKET_IPV4_MAX_DATAGRAM_MTU_SIZE \
45 (FGE_SOCKET_IPV4_MIN_MTU - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
46#define FGE_SOCKET_IPV6_MAX_DATAGRAM_MTU_SIZE \
47 (FGE_SOCKET_IPV6_MIN_MTU - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
48#define FGE_SOCKET_IPV4_MAX_DATAGRAM_ETHMTU_SIZE \
49 (FGE_SOCKET_ETHERNET_MTU - FGE_SOCKET_IPV4_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
50#define FGE_SOCKET_IPV6_MAX_DATAGRAM_ETHMTU_SIZE \
51 (FGE_SOCKET_ETHERNET_MTU - FGE_SOCKET_IPV6_HEADER_SIZE - FGE_SOCKET_UDP_HEADER_SIZE)
53#define FGE_SOCKET_TCP_DEFAULT_BUFFERSIZE 2048
70 using SocketDescriptor = uint64_t;
72 using SocketDescriptor =
unsigned int;
75 using SocketDescriptor = int;
97 ERR_SUCCESS = ERR_NOERROR,
98 ERR_DONE = ERR_NOERROR,
102 ERR_DISCONNECTED = 3,
105 ERR_ALREADYCONNECTED = 5,
107 ERR_TOOMANYSOCKET = 7,
111 ERR_INVALIDARGUMENT = 9,
114 ERR_UNKNOWN = ERR_UNSUCCESS
125 std::string _description;
127 std::vector<Data> _data;
145 [[nodiscard]]
inline IpAddress::Types
getAddressType()
const {
return this->g_addressType; }
320 [[nodiscard]]
static std::vector<AdapterInfo>
getAdaptersInfo(IpAddress::Types type = IpAddress::Types::None);
329 Socket& operator=(Socket
const& r) =
delete;
330 Socket(Socket
const& r) =
delete;
333 explicit Socket(
Types type, IpAddress::Types addressType = IpAddress::Types::Ipv4);
337 IpAddress::Types g_addressType{IpAddress::Types::Ipv4};
338 SocketDescriptor g_socket;
347class FGE_API SocketUdp :
public Socket
350 explicit SocketUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
351 SocketUdp(IpAddress::Types addressType,
bool blocking,
bool broadcast);
352 ~SocketUdp()
override =
default;
481 SocketUdp& operator=(SocketUdp&& r)
noexcept;
484 std::vector<uint8_t> g_buffer;
492class FGE_API SocketTcp :
public Socket
495 explicit SocketTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
496 explicit SocketTcp(IpAddress::Types addressType,
bool blocking);
497 ~SocketTcp()
override =
default;
552 Errors send(
void const* data, std::size_t size, std::size_t& sent);
562 Errors receive(
void* data, std::size_t size, std::size_t& received, uint32_t timeoutms);
600 SocketTcp& operator=(SocketTcp&& r)
noexcept;
603 std::size_t g_receivedSize;
604 std::size_t g_wantedSize;
605 std::vector<uint8_t> g_buffer;
613class FGE_API SocketListenerTcp :
public Socket
616 explicit SocketListenerTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
617 explicit SocketListenerTcp(IpAddress::Types addressType,
bool blocking);
618 ~SocketListenerTcp()
override =
default;
643 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:493
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:145
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:95
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:137
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:83
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:120
Definition C_socket.hpp:118