GithubHelp home page GithubHelp logo

cinaral / udp_msg Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 118 KB

Simple header-only library to send key-value arrays over UDP.

License: MIT License

C++ 91.33% Shell 8.67%
cpp header-only udp udp-socket

udp_msg's Introduction

1. udp_msg: Key-value arrays over UDP

This lightweight header-only library sends/receives key and value arrays over UDP packets.

  • Can be used to open and manage sockets, translate multiple key-value arrays from and into messages.
  • Maximum message size is limited to 512 MB to prevent fragmentation of the message into multiple UDP packets.
  • The default and the only translation done by this library interprets the value array as the specified value type. However, the keys can be used to communicate the types of the values if it is different from VALUE_T.
  • It was tested on Windows 10, Linux Mint, Raspberry Pi OS (2022).

2. Installation

Include all or some of the headers in include/ into your project. You could use FetchContent() in your CMakeLists.txt:

FetchContent_Declare(udp_msg URL https://github.com/cinaral/udp_msg/releases/download/<RELEASE_TAG>/src.zip)
FetchContent_MakeAvailable(udp_msg)

Use CTest to test the library before using. There is one test, which is a single send-then-receive test over the loopback address.

3. Usage

Create a socket object to send and receive:

	udp_msg::sock<KEY_T, VAL_T, KEY_DIM, VAL_DIM> udp_msg_obj(
		const char hostname[], 
		const unsigned port, 
		const bool is_binding = true,
		const bool is_nonblocking = true, 
		const int af = AF_INET, 
		const int type = SOCK_DGRAM,
		const int protocol = 0
	);
	//* send
	udp.send(key_sent, val_sent);

Set is_binding to false if you do not wish to bind the socket. Set is_nonblocking to false if you wish blocking behavior. af, type, and protocol are passed to ::socket(af, type, protocol) (defined in sys/socket.h) and can be used to change the socket type.

To receive use udp_msg::receive():

int receive(KEY_T (&key_arr)[KEY_DIM], VAL_T (&val_arr)[VAL_DIM]);

To send use udp_msg::send():

int send(const KEY_T (&key_arr)[KEY_DIM], const VAL_T (&val_arr)[VAL_DIM]);

4. Examples

4.1. Example: Receive

#include "udp_msg.hpp"
//...
int
main()
{
	//* create a socket to receive one char and one float from 192.168.1.1:8887
	udp_msg::sock<char, float, 1, 1> soc("192.168.1.1", 8887, true);
	//* buffer to hold the received message
	char key_arr[1]; 
	float val_arr[1];
	
	if (soc.receive(key_arr, val_arr) > 0) {
		//* received something...
	}
	return 0;
}

See receive.cpp for details.

4.2. Example: Send

#include "udp_msg.hpp"
//...
int
main()
{
	//* create a socket to send ASCII '0' and 3.14f to 192.168.1.1:8887
	udp_msg::sock<char, float, 1, 1> soc("192.168.1.2", 8887);
	
	if (udp.send(0x30, 3.14) > 0) {
		//* send successful...
	}
	return 0;
}

See send.cpp for details.

5. Key-Value arrays

This library sends and receives two separate arrays, the first array contains the KEY_DIM keys of type KEY_T, and the second array contains VALUE_DIM values of type VAL_T.

  • Size of 1 message = KEY_DIM * sizeof(KEY_T) + VALUE_DIM * sizeof(VAL_T) bytes
  • Number of keys need not be equal to number of values.
  • Example 52-byte message, which consists of four (KEY_DIM=4) three-dimensional coordinate frame IDs represented by characters (KEY_T=char, one byte), and twelve (VALUE_DIM=12) coordinate values represented by single-precision floating-point numbers (VAL_T=float, four bytes):
    Key (char[4]) Value (float[12])
    A $x_A$, $y_A$, $z_A$
    B $x_B$, $y_B$, $z_B$
    C $\theta_C$, $\phi_C$, $\psi_C$
    D $\theta_D$, $\phi_D$, $\psi_D$
      char key[4] = {'A', 'B', 'C', 'D'};
      float val[12] = {x_A, y_A, z_A, x_B, y_B, z_B, theta_C, phi_C, psi_C, theta_D, phi_D, psi_D};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.