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

A TCP socket. More...

#include <gf/TcpSocket.h>

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

Public Member Functions

 TcpSocket ()=default
 Default constructor. More...
 
 TcpSocket (const std::string &hostname, const std::string &service, SocketFamily family=SocketFamily::Unspec)
 Full constructor. More...
 
 TcpSocket (const TcpSocket &)=delete
 Deleted copy constructor. More...
 
TcpSocketoperator= (const TcpSocket &)=delete
 Deleted copy assignment. More...
 
 TcpSocket (TcpSocket &&)=default
 Default move constructor. More...
 
TcpSocketoperator= (TcpSocket &&)=default
 Default move assignment. More...
 
 ~TcpSocket ()
 Destructor. More...
 
SocketAddress getRemoteAddress () const
 Get the remote address of the host. More...
 
SocketDataResult sendRawBytes (ArrayRef< uint8_t > buffer)
 Send some bytes over to the socket. More...
 
SocketDataResult recvRawBytes (BufferRef< uint8_t > buffer)
 Receive some bytes from the socket. More...
 
SocketStatus sendBytes (ArrayRef< uint8_t > buffer)
 Send a whole buffer to the socket. More...
 
SocketStatus recvBytes (BufferRef< uint8_t > buffer)
 Receive a whole buffer from the socket. More...
 
SocketStatus sendPacket (const Packet &packet)
 Send a packet to the socket. More...
 
SocketStatus recvPacket (Packet &packet)
 Receive a packet 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 TCP socket.

A TCP socket is a socket for TCP (Transmission Control Protocol). TCP provides a reliable communication between two hosts. TCP is connection-oriented i.e. once the connection is established, it can be used to send and/or receive data until it is shutdown.

// Create a socket to example.com on port 24680
gf::TcpSocket socket("example.com", "24680");
if (!socket) {
// Handle error
return;
}
uint8_t bytes[] = { 0x42, 0x69, 0x13, 0x12 };
if (socket.sendBytes(bytes) != gf::SocketStatus::Data) {
// Handle error
return;
}
if (socket.recvBytes(bytes) != gf::SocketStatus::Data) {
// Handle error
return;
}
See also
gf::TcpListener, gf::UdpSocket

Constructor & Destructor Documentation

◆ TcpSocket() [1/4]

gf::TcpSocket::TcpSocket ( )
default

Default constructor.

This constructor creates an invalid socket

◆ TcpSocket() [2/4]

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

Full constructor.

Parameters
hostnameThe name of the host to connect
serviceThe service on the host to connect
familyThe prefered socket family

◆ TcpSocket() [3/4]

gf::TcpSocket::TcpSocket ( const TcpSocket )
delete

Deleted copy constructor.

◆ TcpSocket() [4/4]

gf::TcpSocket::TcpSocket ( TcpSocket &&  )
default

Default move constructor.

◆ ~TcpSocket()

gf::TcpSocket::~TcpSocket ( )

Destructor.

The destructor shutdowns the socket before closing it.

Member Function Documentation

◆ getRemoteAddress()

SocketAddress gf::TcpSocket::getRemoteAddress ( ) const

Get the remote address of the host.

◆ operator=() [1/2]

TcpSocket& gf::TcpSocket::operator= ( const TcpSocket )
delete

Deleted copy assignment.

◆ operator=() [2/2]

TcpSocket& gf::TcpSocket::operator= ( TcpSocket &&  )
default

Default move assignment.

◆ recvBytes()

SocketStatus gf::TcpSocket::recvBytes ( BufferRef< uint8_t >  buffer)

Receive a whole buffer from the socket.

This function ensures the whole buffer is received unless an error occurs.

Parameters
bufferThe buffer to store the received bytes
Returns
The status of the connection

◆ recvPacket()

SocketStatus gf::TcpSocket::recvPacket ( Packet packet)

Receive a packet from the socket.

Parameters
packetThe packet to store the received bytes
Returns
The status of the connection

◆ recvRawBytes()

SocketDataResult gf::TcpSocket::recvRawBytes ( BufferRef< uint8_t >  buffer)

Receive some bytes from the socket.

Parameters
bufferThe buffer to store the received bytes
Returns
A result for the operation

◆ sendBytes()

SocketStatus gf::TcpSocket::sendBytes ( ArrayRef< uint8_t >  buffer)

Send a whole buffer to the socket.

This function ensures the whole buffer is sent unless an error occurs.

Parameters
bufferThe buffer that contains the bytes to send
Returns
The status of the connection

◆ sendPacket()

SocketStatus gf::TcpSocket::sendPacket ( const Packet packet)

Send a packet to the socket.

Parameters
packetThe packet that contains the bytes to send
Returns
The status of the connection

◆ sendRawBytes()

SocketDataResult gf::TcpSocket::sendRawBytes ( ArrayRef< uint8_t >  buffer)

Send some bytes over to the socket.

Parameters
bufferThe buffer that contains the bytes to send
Returns
A result for the operation