Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Public Member Functions | List of all members
gf::UdpSocket Class Reference

A UDP socket. More...

#include <gf/UdpSocket.h>

Inheritance diagram for gf::UdpSocket:
Inheritance graph
[legend]

Public Member Functions

 UdpSocket ()=default
 Default constructor. More...
 
 UdpSocket (AnyType, SocketFamily family=SocketFamily::Unspec)
 Full constructor. More...
 
 UdpSocket (const std::string &service, SocketFamily family=SocketFamily::Unspec)
 Full constructor. More...
 
SocketAddress getRemoteAddress (const std::string &hostname, const std::string &service)
 Get a remote address for this socket. More...
 
SocketDataResult sendRawBytesTo (ArrayRef< uint8_t > buffer, const SocketAddress &address)
 Send some bytes over to the socket. More...
 
SocketDataResult recvRawBytesFrom (BufferRef< uint8_t > buffer, SocketAddress &address)
 Receive some bytes from the socket. More...
 
bool sendBytesTo (ArrayRef< uint8_t > buffer, const SocketAddress &address)
 Send a whole buffer to the socket. More...
 
bool recvBytesFrom (BufferRef< uint8_t > buffer, SocketAddress &address)
 Receive a whole buffer from the socket. More...
 
- Public Member Functions inherited from gf::Socket
 ~Socket ()
 Destructor. More...
 
 Socket (const Socket &)=delete
 Deleted copy constructor. More...
 
Socketoperator= (const Socket &)=delete
 Deleted copy assignment. More...
 
 Socket (Socket &&other) noexcept
 Move constructor. More...
 
Socketoperator= (Socket &&other) noexcept
 Move assignment. More...
 
 operator bool () const noexcept
 Boolean conversion. More...
 
SocketAddress getLocalAddress () const
 Get the local address of the socket. More...
 
void setBlocking ()
 Set the socket in blocking mode. More...
 
void setNonBlocking ()
 Set the socket in non-blocking mode. More...
 

Detailed Description

A UDP socket.

A UDP socket is a socket for UDP (User Datagram Protocol). UDP provides an unreliable communication between two hosts. UDP is connectionless i.e. no connection is established, the receiver must be set for each communication.

// Create a socket
if (!socket) {
// Handle error
return;
}
gf::SocketAddress address = socket.getRemoteAddress("example.com", "24680");
uint8_t bytes[] = { 0x42, 0x69, 0x13, 0x12 };
if (!socket.sendBytesTo(bytes, address)) {
// Handle error
return;
}
if (!socket.recvBytesFrom(bytes, address)) {
// Handle error
return;
}
See also
gf::TcpSocket

Constructor & Destructor Documentation

◆ UdpSocket() [1/3]

gf::UdpSocket::UdpSocket ( )
default

Default constructor.

This constructor creates an invalid socket

◆ UdpSocket() [2/3]

gf::UdpSocket::UdpSocket ( AnyType  ,
SocketFamily  family = SocketFamily::Unspec 
)

Full constructor.

It creates a UDP socket that is not bound to a specific port. The port is chosen by the system. It may be used by a client that do not need a specific port.

Parameters
familyThe prefered socket family

◆ UdpSocket() [3/3]

gf::UdpSocket::UdpSocket ( const std::string &  service,
SocketFamily  family = SocketFamily::Unspec 
)

Full constructor.

It creates a UDP socket that is bound on a specific port. It must be used in case a server wants to be joined with UDP on a specific port.

Parameters
serviceThe bound service
familyThe prefered socket family

Member Function Documentation

◆ getRemoteAddress()

SocketAddress gf::UdpSocket::getRemoteAddress ( const std::string &  hostname,
const std::string &  service 
)

Get a remote address for this socket.

This function provides a suitable socket address that can be used with the sending functions.

Parameters
hostnameThe name of the host to communicate with
serviceThe service on the host to communicate with

◆ recvBytesFrom()

bool gf::UdpSocket::recvBytesFrom ( BufferRef< uint8_t >  buffer,
SocketAddress address 
)

Receive a whole buffer from the socket.

This function ensures the buffer is received in a single packet.

Parameters
bufferThe buffer to store the received bytes
addressThe address of the sending host
Returns
True if no error occurred and the buffer was received

◆ recvRawBytesFrom()

SocketDataResult gf::UdpSocket::recvRawBytesFrom ( BufferRef< uint8_t >  buffer,
SocketAddress address 
)

Receive some bytes from the socket.

Parameters
bufferThe buffer to store the received bytes
addressThe address of the sending host
Returns
A result for the operation

◆ sendBytesTo()

bool gf::UdpSocket::sendBytesTo ( ArrayRef< uint8_t >  buffer,
const SocketAddress address 
)

Send a whole buffer to the socket.

This function ensures the buffer is sent in a single packet.

Parameters
bufferThe buffer that contains the bytes to send
addressThe address of the host to communicate with
Returns
True if no error occurred and the buffer was sent

◆ sendRawBytesTo()

SocketDataResult gf::UdpSocket::sendRawBytesTo ( ArrayRef< uint8_t >  buffer,
const SocketAddress address 
)

Send some bytes over to the socket.

Parameters
bufferThe buffer that contains the bytes to send
addressThe address of the host to communicate with
Returns
A result for the operation