Botball Game State
A game stared library for Botball, it helps model the table and different phases of the competition.
Loading...
Searching...
No Matches
Socket Class Reference
Inheritance diagram for Socket:
Client Server

Public Member Functions

void init_socket (uint16_t port)
void init_server (uint16_t port)
void init_client (const std::string &ip_address, uint16_t port)
void recv_all (void *buf, size_t len)
void send_all (const void *buf, size_t len) const
void send_json (const json &data) const
std::optional< json > recv_json ()
void shutdown_socket ()
void close_socket ()

Protected Attributes

int m_fd = -1
bool m_closed = true
std::shared_ptr< spdlog::logger > m_log

Member Function Documentation

◆ close_socket()

void Socket::close_socket ( )

closes the socket locally

◆ init_client()

void Socket::init_client ( const std::string & ip_address,
uint16_t port )

Opens an IPv4 TCP Socket, and connects to a remote socket via ip address and port number

Parameters
ip_addressof remote socket
portof remote socket

◆ init_server()

void Socket::init_server ( uint16_t port)

Opens an IPv4 TCP Socket, binds it to a specified port and makes it listen to every host

Parameters
portto bind to

◆ init_socket()

void Socket::init_socket ( uint16_t port)

Opens an IPv4 TCP Socket

◆ recv_all()

void Socket::recv_all ( void * buf,
size_t len )

gets data from a remote socket and keeps reading in until the buffer is full

Parameters
bufbuffer that bytes get loaded into
lenlength of the buffer

◆ recv_json()

std::optional< json > Socket::recv_json ( )
nodiscard

Receives JSON data from the remote client.

  1. Reads in 4 byte determining the size of the JSON data
  2. Prepares a buffer and reads in size bytes into the buffer (reads in the JSON data)
    Returns
    the received JSON data or a nullptr if nothing was sent

◆ send_all()

void Socket::send_all ( const void * buf,
size_t len ) const

sends data to a remote socket and keeps sending bytes until all bytes are sent

Parameters
bufbuffer that bytes get loaded from
lenlength of the buffer

◆ send_json()

void Socket::send_json ( const json & data) const

Sends JSON data to the remote client by following protocol orders

  1. send 4 byte containing the size of the JSON data, so the receiver can prepare a big enough buffer
  2. send the JSON data
    Parameters
    datato be sent over the socket

◆ shutdown_socket()

void Socket::shutdown_socket ( )

Shuts the connection down effectively letting the other client know that the connection should be closed now.


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