![]() |
FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
|
A wrapper for TCP sockets inheriting from Socket. More...
#include <C_socket.hpp>
Public Types | |
enum class | Types { UDP , TCP , TCP_LISTENER , UNKNOWN } |
The possible types of sockets. More... | |
enum class | Errors { ERR_NOERROR = 0 , ERR_SUCCESS = ERR_NOERROR , ERR_DONE = ERR_NOERROR , ERR_PARTIAL = 1 , ERR_NOTREADY = 2 , ERR_DISCONNECTED = 3 , ERR_REFUSED = 4 , ERR_ALREADYCONNECTED = 5 , ERR_ALREADYUSED = 6 , ERR_TOOMANYSOCKET = 7 , ERR_NOTINIT = 8 , ERR_INVALIDARGUMENT = 9 , ERR_UNSUCCESS = 10 , ERR_UNKNOWN = ERR_UNSUCCESS } |
The error codes. More... | |
using | SocketDescriptor = int |
Public Member Functions | |
SocketTcp (IpAddress::Types addressType=IpAddress::Types::Ipv4) | |
SocketTcp (IpAddress::Types addressType, bool blocking) | |
void | flush () |
Flush the internal data buffer. | |
Errors | create (SocketDescriptor sck) |
Create the socket with an existing descriptor. | |
Errors | create () override |
Create a socket. | |
Errors | connect (IpAddress const &remoteAddress, Port remotePort, uint32_t timeoutms) |
Connect to a remote address. | |
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 (void *data, std::size_t size, std::size_t &received) |
Receive data from the connected remote address. | |
Errors | receive (void *data, std::size_t size, std::size_t &received, uint32_t timeoutms) |
Errors | send (Packet &packet) |
Send a Packet to the connected remote address. | |
Errors | receive (Packet &packet) |
Receive a Packet from the connected remote address. | |
Errors | sendAndReceive (Packet &sendPacket, Packet &receivePacket, uint32_t timeoutms) |
Utility function to send and receive data. | |
Errors | receive (Packet &packet, uint32_t timeoutms) |
Receive a packet from the connected remote address with a timeout. | |
SocketTcp & | operator= (SocketTcp &&r) noexcept |
Types | getType () const |
Get the type of the socket. | |
IpAddress::Types | getAddressType () const |
Get the address type of the socket. | |
void | setAddressType (IpAddress::Types type) |
Set the address type of the socket. | |
void | close () |
Close the socket. | |
bool | isValid () const |
Check if the socket is valid. | |
Port | getLocalPort () const |
Get the local port of the socket. | |
IpAddress | getLocalAddress () const |
Get the local address of the socket. | |
Port | getRemotePort () const |
Get the remote port of the socket. | |
IpAddress | getRemoteAddress () const |
Get the remote address of the socket. | |
bool | isBlocking () const |
Check if the socket is in blocking mode. | |
Errors | setBlocking (bool mode) |
Set the blocking mode of the socket. | |
Errors | setReuseAddress (bool mode) |
Set if the socket reuse the address. | |
Errors | setBroadcastOption (bool mode) |
Set if the socket support broadcast. | |
Errors | setIpv6Only (bool mode) |
Set if ipv6 socket should only use ipv6. | |
Errors | setDontFragment (bool mode) |
Set if the socket should append DF flag to the packet. | |
Errors | select (bool read, uint32_t timeoutms) |
Check the socket for readability or writability. | |
std::optional< uint16_t > | retrieveCurrentAdapterMTU () const |
Retrieve the current adapter MTU. |
Static Public Member Functions | |
static bool | initSocket () |
Init the low-level socket library. | |
static void | uninitSocket () |
Shutdown the low-level socket library. | |
static std::vector< AdapterInfo > | getAdaptersInfo (IpAddress::Types type=IpAddress::Types::None) |
Retrieve adapters information. | |
static int | getPlatformSpecifiedError () |
Get the last platform specific error code. |
Protected Attributes | |
Types | g_type |
IpAddress::Types | g_addressType {IpAddress::Types::Ipv4} |
SocketDescriptor | g_socket |
bool | g_isBlocking |
A wrapper for TCP sockets inheriting from Socket.
|
stronginherited |
The error codes.
|
stronginherited |
The possible types of sockets.
|
inherited |
Close the socket.
Errors fge::net::SocketTcp::connect | ( | IpAddress const & | remoteAddress, |
Port | remotePort, | ||
uint32_t | timeoutms ) |
Connect to a remote address.
remoteAddress | The remote address to connect to |
remotePort | The remote port to connect to |
timeoutms | The timeout in milliseconds |
|
overridevirtual |
Create a socket.
by default, the nagle algorithm is disabled. On Mac OS X, disable the SIGPIPE signal on disconnection.
Implements fge::net::Socket.
Errors fge::net::SocketTcp::create | ( | SocketDescriptor | sck | ) |
Create the socket with an existing descriptor.
the nagle algorithm will be disabled On Mac OS X, disable the SIGPIPE signal on disconnection.
sck | The socket descriptor |
void fge::net::SocketTcp::flush | ( | ) |
Flush the internal data buffer.
This function will discard all received data until now.
|
staticnodiscardinherited |
Retrieve adapters information.
The information retrieved is the name, description, mtu and unicast addresses of the adapters.
type | The type of address to get, if None, all addresses will be returned |
|
inlinenodiscardinherited |
Get the address type of the socket.
By default, the address type is Ipv4.
|
nodiscardinherited |
Get the local address of the socket.
From MSDN: This function provide the only way to determine the local association that has been set by the system.
|
nodiscardinherited |
Get the local port of the socket.
From MSDN: This function provide the only way to determine the local association that has been set by the system.
|
staticnodiscardinherited |
Get the last platform specific error code.
|
nodiscardinherited |
Get the remote address of the socket.
From MSDN: For datagram sockets, only the address of a peer specified in a previous connect call will be returned. Any address specified by a previous sendto call will not be returned.
|
nodiscardinherited |
Get the remote port of the socket.
From MSDN: For datagram sockets, only the address of a peer specified in a previous connect call will be returned. Any address specified by a previous sendto call will not be returned.
|
inlinenodiscardinherited |
Get the type of the socket.
Type can be one of the type defined in the Socket::Type enum
|
staticinherited |
Init the low-level socket library.
This call WSAStartup() on Windows and nothing on other platforms
|
nodiscardinherited |
Check if the socket is in blocking mode.
|
nodiscardinherited |
Check if the socket is valid.
The socket is valid if it has been created and not closed
Receive a Packet from the connected remote address.
If this function return Socket::ERR_PARTIAL then the packet is not complete, and you should call this function again to receive the rest of the data.
packet | The packet to receive into |
Receive a packet from the connected remote address with a timeout.
packet | The packet to receive into |
timeoutms | The timeout in milliseconds |
Errors fge::net::SocketTcp::receive | ( | void * | data, |
std::size_t | size, | ||
std::size_t & | received ) |
Receive data from the connected remote address.
data | The data buffer to receive into |
size | The size of the data buffer |
received | The number of bytes received |
|
nodiscardinherited |
Retrieve the current adapter MTU.
From MSDN: This function get all the adapter addresses and compare the local address with the current local address in order to find the adapter that is currently used.
From a connection-less socket, this function can only work if the socket is connected to a remote address or/and if I/O operations have been performed.
|
inherited |
Check the socket for readability or writability.
From MSDN: The select function allows an application to determine the status of a socket.
If timeout is 0, the function returns immediately (polling).
read | true if the socket should be checked for readability, false otherwise |
timeoutms | The timeout in milliseconds |
Send a Packet to the connected remote address.
packet | The packet to send |
Errors fge::net::SocketTcp::send | ( | void const * | data, |
std::size_t | size ) |
Send data to the connected remote address.
data | The data to send |
size | The size of the data to send |
Errors fge::net::SocketTcp::send | ( | void const * | data, |
std::size_t | size, | ||
std::size_t & | sent ) |
Send data to the connected remote address.
data | The data to send |
size | The size of the data to send |
sent | The number of bytes sent |
Errors fge::net::SocketTcp::sendAndReceive | ( | Packet & | sendPacket, |
Packet & | receivePacket, | ||
uint32_t | timeoutms ) |
Utility function to send and receive data.
sendPacket | The packet to send |
receivePacket | The packet to receive into |
timeoutms | The timeout in milliseconds |
|
inherited |
Set the address type of the socket.
When the address type is changed, the socket is closed and recreated.
type | The address type of the socket (Ipv4 or Ipv6) |
|
inherited |
Set the blocking mode of the socket.
mode | The blocking mode to set |
|
inherited |
Set if the socket support broadcast.
From MSDN: The SO_BROADCAST socket option enables the socket to send and receive broadcast messages.
mode | The broadcast mode to set |
|
inherited |
Set if the socket should append DF flag to the packet.
From MSDN: (IP_DONTFRAGMENT) Indicates that data should not be fragmented regardless of the local MTU. Valid only for message oriented protocols. Microsoft TCP/IP providers respect this option for UDP and ICMP.
Note that on GNU/Linux, this option is IP_MTU_DISCOVER with IP_PMTUDISC_DO and IP_PMTUDISC_DONT flags.
mode | If the socket should append the DF flag |
|
inherited |
Set if ipv6 socket should only use ipv6.
From MSDN: The IPV6_V6ONLY socket option indicates if a socket created for the AF_INET6 address family is restricted to IPv6 communications only.
mode | The ipv6 only mode to set |
|
inherited |
Set if the socket reuse the address.
From MSDN: The SO_REUSEADDR socket option allows the application to bind to an address that is already in use.
mode | The reuse mode to set |
|
staticinherited |
Shutdown the low-level socket library.