< Previous: Performance Metrics | 🏠 Home
Architecture & Examples
cpp-udpnet is designed for high performance and low latency. It achieves this by decoupling the OS-level socket polling from the user-level data processing.
Threading Model
The library employs a producer-consumer architecture:
- Event Loop Thread (Producer): A single background thread continuously polls the UDP socket using native system calls (
pollon POSIX,WSAPollon Windows). When a datagram arrives, it is immediately read into a buffer. - Worker Pool (Consumers): The datagram is then dispatched to a built-in thread pool provided by
cpp-asyncworker.
Peer Affinity
To ensure correct ordering, cpp-udpnet guarantees Peer Affinity. This means that all datagrams received from a specific PeerAddress (IP + Port) are always executed serially by the same worker thread. This prevents race conditions in your application logic, while still allowing data from different peers to be processed concurrently.
Included Examples
The examples/ directory contains several complete, buildable programs demonstrating how to use cpp-udpnet in various scenarios:
server.cpp: Demonstrates setting up aUdpListener, configuring the worker pool, and handling basic echo logic.client.cpp: Shows how to useUdpSenderto communicate with a server and receive asynchronous replies.multicast_sender.cpp: Broadcasts UDP datagrams to a multicast group on a regular interval.multicast_receiver.cpp: Joins a multicast group to receive the datagrams sent by the multicast sender.throughput.cpp: Measures client/sender throughput, packets echoed, packet loss rate, and calculates peak/average throughput metrics.
To build the examples, simply configure CMake from the project root:
mkdir build && cd build
cmake ..
cmake --build .
| < Previous: Performance Metrics | 🏠 Home |