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_MAXDATAGRAMSIZE 65507
33#define FGE_SOCKET_TCP_DEFAULT_BUFFERSIZE 2048
50 using SocketDescriptor = uint64_t;
52 using SocketDescriptor =
unsigned int;
55 using SocketDescriptor = int;
76 ERR_SUCCESS = ERR_NOERROR,
77 ERR_DONE = ERR_NOERROR,
93 ERR_UNKNOWN = ERR_UNSUCCESS
103 [[nodiscard]]
inline Type getType()
const {
return this->g_type; }
111 [[nodiscard]]
inline IpAddress::Types
getAddressType()
const {
return this->g_addressType; }
252 explicit Socket(
Type type, IpAddress::Types addressType = IpAddress::Types::Ipv4);
256 IpAddress::Types g_addressType{IpAddress::Types::Ipv4};
257 SocketDescriptor g_socket;
269 explicit SocketUdp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
270 SocketUdp(IpAddress::Types addressType,
bool blocking,
bool broadcast);
322 sendTo(
void const* data, std::size_t size,
IpAddress const& remoteAddress, fge::net::Port remotePort);
335 std::size_t& received,
337 fge::net::Port& remotePort);
398 std::vector<uint8_t> g_buffer;
409 explicit SocketTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
410 explicit SocketTcp(IpAddress::Types addressType,
bool blocking);
518 std::size_t g_receivedSize;
519 std::size_t g_wantedSize;
520 std::vector<uint8_t> g_buffer;
531 explicit SocketListenerTcp(IpAddress::Types addressType = IpAddress::Types::Ipv4);
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.