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

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

#include <C_socket.hpp>

Inheritance diagram for fge::net::SocketTcp:
fge::net::Socket

Public Types

enum  Type { TYPE_UDP , TYPE_TCP , TYPE_LISTENER_TCP }
 The possible types of sockets. More...
 
enum  Error {
  ERR_NOERROR = 0 , ERR_SUCCESS = ERR_NOERROR , ERR_DONE = ERR_NOERROR , ERR_PARTIAL ,
  ERR_NOTREADY , ERR_DISCONNECTED , ERR_REFUSED , ERR_ALREADYCONNECTED ,
  ERR_ALREADYUSED , ERR_TOOMANYSOCKET , ERR_NOTINIT , ERR_INVALIDARGUMENT ,
  ERR_UNSUCCESS , 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.
 
fge::net::Socket::Error create (fge::net::Socket::SocketDescriptor sck)
 Create the socket with an existing descriptor.
 
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 send (void const *data, std::size_t size)
 Send data to 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 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 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 receive (fge::net::Packet &packet, uint32_t timeoutms)
 Receive a packet from the connected remote address with a timeout.
 
fge::net::SocketTcpoperator= (fge::net::SocketTcp &&r) noexcept
 
Type 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.
 
fge::net::Port getLocalPort () const
 Get the local port of the socket.
 
fge::net::IpAddress getLocalAddress () const
 Get the local address of the socket.
 
fge::net::Port getRemotePort () const
 Get the remote port of the socket.
 
fge::net::IpAddress getRemoteAddress () const
 Get the remote address of the socket.
 
bool isBlocking () const
 Check if the socket is in blocking mode.
 
Error setBlocking (bool mode)
 Set the blocking mode of the socket.
 
Error setReuseAddress (bool mode)
 Set if the socket reuse the address.
 
Error setBroadcastOption (bool mode)
 Set if the socket support broadcast.
 
Error select (bool read, uint32_t timeoutms)
 Check the socket for readability or writability.
 

Static Public Member Functions

static bool initSocket ()
 Init the low-level socket library.
 
static void uninitSocket ()
 Shutdown the low-level socket library.
 
static int getPlatformSpecifiedError ()
 Get the last platform specific error code.
 

Protected Attributes

Type g_type
 
IpAddress::Types g_addressType {IpAddress::Types::Ipv4}
 
SocketDescriptor g_socket
 
bool g_isBlocking
 

Detailed Description

A wrapper for TCP sockets inheriting from Socket.

Member Enumeration Documentation

◆ Error

enum fge::net::Socket::Error
inherited

The error codes.

◆ Type

enum fge::net::Socket::Type
inherited

The possible types of sockets.

Member Function Documentation

◆ close()

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

Close the socket.

◆ connect()

fge::net::Socket::Error fge::net::SocketTcp::connect ( fge::net::IpAddress const & remoteAddress,
fge::net::Port remotePort,
uint32_t timeoutms )

Connect to a remote address.

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

◆ create() [1/2]

fge::net::Socket::Error fge::net::SocketTcp::create ( )
overridevirtual

Create a socket.

by default, the nagle algorithm is disabled. On Mac OS X, disable the SIGPIPE signal on disconnection.

Returns
Error::ERR_NOERROR if successful, otherwise an error code

Implements fge::net::Socket.

◆ create() [2/2]

fge::net::Socket::Error fge::net::SocketTcp::create ( fge::net::Socket::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.

Parameters
sckThe socket descriptor
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ flush()

void fge::net::SocketTcp::flush ( )

Flush the internal data buffer.

This function will discard all received data until now.

◆ 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()

fge::net::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 fge::net::IpAddress::None if there was an error

◆ getLocalPort()

fge::net::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()

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

Get the last platform specific error code.

Returns
The last platform specific error code

◆ getRemoteAddress()

fge::net::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 fge::net::IpAddress::None if there was an error

◆ getRemotePort()

fge::net::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()

Type 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()

static 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/3]

fge::net::Socket::Error fge::net::SocketTcp::receive ( fge::net::Packet & packet)

Receive a fge::net::Packet from the connected remote address.

If this function return fge::net::Socket::ERR_PARTIAL then the packet is not complete and you should call this function again to receive the rest of the data.

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

◆ receive() [2/3]

fge::net::Socket::Error fge::net::SocketTcp::receive ( fge::net::Packet & packet,
uint32_t timeoutms )

Receive a packet from the connected remote address with a timeout.

Parameters
packetThe packet to receive into
timeoutmsThe timeout in milliseconds
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ receive() [3/3]

fge::net::Socket::Error fge::net::SocketTcp::receive ( void * data,
std::size_t size,
std::size_t & received )

Receive data from the connected remote address.

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

◆ select()

Error 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
Error::ERR_NOERROR if the socket is ready, otherwise an error code

◆ send() [1/3]

fge::net::Socket::Error fge::net::SocketTcp::send ( fge::net::Packet & packet)

Send a fge::net::Packet to the connected remote address.

Parameters
packetThe packet to send
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ send() [2/3]

fge::net::Socket::Error fge::net::SocketTcp::send ( void const * data,
std::size_t size )

Send data to the connected remote address.

Parameters
dataThe data to send
sizeThe size of the data to send
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ send() [3/3]

fge::net::Socket::Error fge::net::SocketTcp::send ( void const * data,
std::size_t size,
std::size_t & sent )

Send data to the connected remote address.

Parameters
dataThe data to send
sizeThe size of the data to send
sentThe number of bytes sent
Returns
Error::ERR_NOERROR if successful, otherwise an error code

◆ sendAndReceive()

fge::net::Socket::Error fge::net::SocketTcp::sendAndReceive ( fge::net::Packet & sendPacket,
fge::net::Packet & receivePacket,
uint32_t timeoutms )

Utility function to send and receive data.

Parameters
sendPacketThe packet to send
receivePacketThe packet to receive into
timeoutmsThe timeout in milliseconds
Returns
Error::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()

Error 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()

Error 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
Error::ERR_NOERROR if successful, otherwise an error code

◆ setReuseAddress()

Error 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()

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

Shutdown the low-level socket library.


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