FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
fge::net::SocketUdp Class Reference

A wrapper for UDP sockets inheriting from Socket. More...

#include <C_socket.hpp>

Inheritance diagram for fge::net::SocketUdp:
fge::net::Socket

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

 SocketUdp (IpAddress::Types addressType=IpAddress::Types::Ipv4)
 SocketUdp (IpAddress::Types addressType, bool blocking, bool broadcast)
Errors create () override
 Create a new socket.
Errors connect (IpAddress const &remoteAddress, Port remotePort)
 Connect the socket to a remote address and port.
Errors disconnect ()
Errors bind (Port port, IpAddress const &address)
 Bind the socket to a local address and port.
Errors send (void const *data, std::size_t size)
 Send data to the connected remote address.
Errors sendTo (void const *data, std::size_t size, IpAddress const &remoteAddress, Port remotePort)
 Send data 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 send (Packet &packet)
 Send a Packet to the connected remote address.
Errors sendTo (Packet &packet, IpAddress const &remoteAddress, Port remotePort)
 Send a Packet to the specified 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.
SocketUdpoperator= (SocketUdp &&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 std::optional< uint16_t > retrieveAdapterMTUForDestination (IpAddress const &destination)
 Helper to retrieve the MTU of the adapter used to reach the destination ip address.
static bool initSocket ()
 Init the low-level socket library.
static void uninitSocket ()
 Shutdown the low-level socket library.
static std::vector< AdapterInfogetAdaptersInfo (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

Detailed Description

A wrapper for UDP sockets inheriting from Socket.

Member Enumeration Documentation

◆ Errors

enum class fge::net::Socket::Errors
stronginherited

The error codes.

◆ Types

enum class fge::net::Socket::Types
stronginherited

The possible types of sockets.

Member Function Documentation

◆ bind()

Errors fge::net::SocketUdp::bind ( Port port,
IpAddress const & address )

Bind the socket to a local address and port.

From MSDN: The bind function associates a local address and port with a socket.

Parameters
portThe local port to bind to
addressThe local address to bind to
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ close()

void fge::net::Socket::close ( )
inherited

Close the socket.

◆ connect()

Errors fge::net::SocketUdp::connect ( IpAddress const & remoteAddress,
Port remotePort )

Connect the socket to a remote address and port.

From MSDN: For a connectionless socket the operation performed by connect is merely to establish a default destination address that can be used on subsequent send and receive calls. Any datagrams received from an address other than the destination address specified will be discarded.

Parameters
remoteAddressThe remote address to connect to
remotePortThe remote port to connect to
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ create()

Errors fge::net::SocketUdp::create ( )
overridevirtual

Create a new socket.

Returns
If successful, Error::ERR_NOERROR is returned, otherwise an error code is returned

Implements fge::net::Socket.

◆ getAdaptersInfo()

std::vector< AdapterInfo > fge::net::Socket::getAdaptersInfo ( IpAddress::Types type = IpAddress::Types::None)
staticnodiscardinherited

Retrieve adapters information.

The information retrieved is the name, description, mtu and unicast addresses of the adapters.

Parameters
typeThe type of address to get, if None, all addresses will be returned
Returns
A vector containing the adapters information or an empty vector

◆ getAddressType()

IpAddress::Types fge::net::Socket::getAddressType ( ) const
inlinenodiscardinherited

Get the address type of the socket.

By default, the address type is Ipv4.

Returns
The address type of the socket (Ipv4 or Ipv6)

◆ getLocalAddress()

IpAddress fge::net::Socket::getLocalAddress ( ) const
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.

Returns
The local address of the socket or IpAddress::None if there was an error

◆ getLocalPort()

Port fge::net::Socket::getLocalPort ( ) const
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.

Returns
The local port of the socket or 0 if there was an error

◆ getPlatformSpecifiedError()

int fge::net::Socket::getPlatformSpecifiedError ( )
staticnodiscardinherited

Get the last platform specific error code.

Returns
The last platform specific error code

◆ getRemoteAddress()

IpAddress fge::net::Socket::getRemoteAddress ( ) const
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.

Returns
The remote address of the socket or IpAddress::None if there was an error

◆ getRemotePort()

Port fge::net::Socket::getRemotePort ( ) const
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.

Returns
The remote port of the socket or 0 if there was an error

◆ getType()

Types fge::net::Socket::getType ( ) const
inlinenodiscardinherited

Get the type of the socket.

Type can be one of the type defined in the Socket::Type enum

Returns
The type of the socket

◆ initSocket()

bool fge::net::Socket::initSocket ( )
staticinherited

Init the low-level socket library.

This call WSAStartup() on Windows and nothing on other platforms

Returns
true if successful, false otherwise

◆ isBlocking()

bool fge::net::Socket::isBlocking ( ) const
nodiscardinherited

Check if the socket is in blocking mode.

Returns
true if the socket is in blocking mode, false otherwise

◆ isValid()

bool fge::net::Socket::isValid ( ) const
nodiscardinherited

Check if the socket is valid.

The socket is valid if it has been created and not closed

Returns
true if the socket is valid, false otherwise

◆ receive() [1/2]

Errors fge::net::SocketUdp::receive ( Packet & packet)

Receive a Packet from the connected remote address.

The connect function must be called before calling this function.

See also
SocketUdp::connect
Parameters
packetThe packet to receive into
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ receive() [2/2]

Errors fge::net::SocketUdp::receive ( void * data,
std::size_t size,
std::size_t & received )

Receive data from the connected remote address.

The connect function must be called before calling this function.

See also
SocketUdp::connect
Parameters
dataThe data buffer to receive into
sizeThe size of the data buffer
receivedThe number of bytes received
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ receiveFrom() [1/2]

Errors fge::net::SocketUdp::receiveFrom ( Packet & packet,
IpAddress & remoteAddress,
Port & remotePort )

Receive a Packet from an unspecified remote address.

Parameters
packetThe packet to receive into
remoteAddressThe remote address of the sender
remotePortThe remote port of the sender
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ receiveFrom() [2/2]

Errors fge::net::SocketUdp::receiveFrom ( void * data,
std::size_t size,
std::size_t & received,
IpAddress & remoteAddress,
Port & remotePort )

Receive data from an unspecified remote address.

Parameters
dataThe data buffer to receive into
sizeThe size of the data buffer
receivedThe number of bytes received
remoteAddressThe remote address of the sender
remotePortThe remote port of the sender
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ retrieveAdapterMTUForDestination()

std::optional< uint16_t > fge::net::SocketUdp::retrieveAdapterMTUForDestination ( IpAddress const & destination)
staticnodiscard

Helper to retrieve the MTU of the adapter used to reach the destination ip address.

This function will create a temporary socket, bind it to any port and connect it to the destination address.

Parameters
destinationThe destination address to reach
Returns
The MTU of the adapter used to reach the destination address or std::nullopt if an error occurred

◆ retrieveCurrentAdapterMTU()

std::optional< uint16_t > fge::net::Socket::retrieveCurrentAdapterMTU ( ) const
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.

Returns
The current adapter MTU or std::nullopt if an error occurred

◆ select()

Errors fge::net::Socket::select ( bool read,
uint32_t timeoutms )
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).

Parameters
readtrue if the socket should be checked for readability, false otherwise
timeoutmsThe timeout in milliseconds
Returns
Errors::ERR_NOERROR if the socket is ready, otherwise an error code

◆ send() [1/2]

Errors fge::net::SocketUdp::send ( Packet & packet)

Send a Packet to the connected remote address.

The connect function must be called before calling this function.

See also
SocketUdp::connect
Packet
Parameters
packetThe packet to send
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ send() [2/2]

Errors fge::net::SocketUdp::send ( void const * data,
std::size_t size )

Send data to the connected remote address.

The connect function must be called before calling this function.

See also
SocketUdp::connect
Parameters
dataThe data to send
sizeThe size of the data to send
Returns
Errors::ERR_NOERROR if successful, otherwise an error code

◆ sendTo() [1/2]

Errors fge::net::SocketUdp::sendTo ( Packet & packet,
IpAddress const & remoteAddress,
Port remotePort )

Send a Packet to the specified address.

Parameters
packetThe packet to send
remoteAddressThe remote address to send to
remotePortThe remote port to send to
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ sendTo() [2/2]

Errors fge::net::SocketUdp::sendTo ( void const * data,
std::size_t size,
IpAddress const & remoteAddress,
Port remotePort )

Send data to the specified address.

Parameters
dataThe data to send
sizeThe size of the data to send
remoteAddressThe remote address to send to
remotePortThe remote port to send to
Returns
Errors::ERR_NOERROR if successful, otherwise an error code

◆ setAddressType()

void fge::net::Socket::setAddressType ( IpAddress::Types type)
inherited

Set the address type of the socket.

When the address type is changed, the socket is closed and recreated.

Parameters
typeThe address type of the socket (Ipv4 or Ipv6)

◆ setBlocking()

Errors fge::net::Socket::setBlocking ( bool mode)
inherited

Set the blocking mode of the socket.

Parameters
modeThe blocking mode to set
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ setBroadcastOption()

Errors fge::net::Socket::setBroadcastOption ( bool mode)
inherited

Set if the socket support broadcast.

From MSDN: The SO_BROADCAST socket option enables the socket to send and receive broadcast messages.

Parameters
modeThe broadcast mode to set
Returns
Errors::ERR_NOERROR if successful, otherwise an error code

◆ setDontFragment()

Errors fge::net::Socket::setDontFragment ( bool mode)
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.

Parameters
modeIf the socket should append the DF flag
Returns
Errors::ERR_NOERROR if successful, otherwise an error code

◆ setIpv6Only()

Errors fge::net::Socket::setIpv6Only ( bool mode)
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.

Parameters
modeThe ipv6 only mode to set
Returns
Errors::ERR_NOERROR if successful, otherwise an error code

◆ setReuseAddress()

Errors fge::net::Socket::setReuseAddress ( bool mode)
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.

Parameters
modeThe reuse mode to set
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ uninitSocket()

void fge::net::Socket::uninitSocket ( )
staticinherited

Shutdown the low-level socket library.


The documentation for this class was generated from the following file: