Internet Protocol
Header Only Library

UDP Client

How to use UDP client class

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class UDPClient

Example code

#include <iostream>
#include <IP/UDP/UDPClient.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    UDPClient client;
    client.set_host("localhost", "3000");
    client.on_message_received = [&](const FUdpMessage message) {
        std::cout << message.toString() << std::endl;
    };
    client.connect();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            client.close();
            break;
        }
        client.send_str(input);
    }
 
    return 0;
}

Variables

FUdpMessage

namespace InternetProtocol
 
struct FUdpMessage {
    FUdpMessage() { rawData.resize(1024); }
    std::vector<std::byte> rawData;
    size_t size = 0;
};

Functions

Set Host

  • Set the adress, port, protocol of host
void set_host(const std::string &ip, const std::string &port, const EProtocolType protocol)

Get Socket

  • Get socket connection properties
asio::ip::udp::socket &get_socket()

Set Max Receive Buffer Size

  • Set max size in bytes of receive buffer
void set_max_sen_buffer_size(int value = 1024)

Get Max Receive Buffer Size

  • Get max size in bytes of receive buffer
int get_max_send_buffer_size() const

Set Split Package

  • If the size of a message exceeds the maximum allowed size for the send buffer, it will be divided into smaller packets before being sent.
void set_split_package(bool value = true)

Get Split Package

  • Get if Split Package is enabled
bool get_split_package() const

Send Str

  • Send a string message
  • Return false if message is empty or socket is closed
  • Warning: If you’re using Windows, ensure that you are sending characters compatible with UTF-8
bool send_str(const std::string &message)

Send Buffer

  • Send a buffer message
  • Return false if buffer is empty or socket is closed
Message
bool send_buffer(const std::vector<uint8_t> &buffer)

Connect

  • Connect to the host
  • Return false if socket is already open
bool connect()

Close

  • Close connection
void close()

Get Error Code

  • Get latest error code
asio::error_code get_error_code() const

Events

On Connected

  • Event triggered when a connection is estabilished
std::function<void()> on_connected;

On Bytes Transferred

  • Event triggered when socket send or receive data
  • size_t: bytes sent
  • size_t: bytes received
std::function<void(const size_t, const size_t)> on_bytes_transfered

On Message Sent

  • Event triggered when a message is sent
  • const asio::error_code &: if error occur, error code will be different from 0
std::function<void(const asio::error_code &)> on_message_sent

On Message Received

  • Event triggered when a message is received
  • FUdpMessage: message struct
  • Use buffer_to_string() function to converte raw_data to std::string
std::function<void(const FUdpMessage)> on_message_received

On Close

  • Event triggered when socket is closed
std::function<void()> on_close

On Error

  • Event triggered if any error occur during async process
  • const asio::error_code &: if error occur, error code will be different from 0
  • Normally, when an error occurs, the socket is automatically closed, triggering the on_close event.
std::function<void(const asio::error_code &)> on_error