Get started
Pick your language. Every binding speaks the same protocol, so a Python client can talk to a C++ server, and a Rust server to a Node.js client.
C++
Install
DataPath is a small static library built with CMake 3.16+ and any compiler from C++11 on (MSVC needs C++14, its lowest mode). It builds as C++17 by default; set DATAPATH_CXX_STANDARD (11, 14, 17, 20, 23) to pick another. Pull it in with FetchContent:
include(FetchContent)
FetchContent_Declare(datapath
GIT_REPOSITORY https://github.com/Xaymar/datapath.git
GIT_TAG root)
FetchContent_MakeAvailable(datapath)
target_link_libraries(my-app PRIVATE datapath::datapath)
Or add a checkout with add_subdirectory(datapath). Tests and samples are only built when DataPath is the top-level project.
Send a message
#include <datapath/datapath.hpp>
// Server: echo every message.
std::shared_ptr<datapath::server> server;
std::vector<std::shared_ptr<datapath::socket>> clients;
datapath::host(server, "my-service");
server->on_accept([&](std::shared_ptr<datapath::socket> client) {
datapath::socket* raw = client.get(); // don't capture the shared_ptr in its own handler
client->on_message([raw](const uint8_t* data, size_t size) {
raw->send(data, size);
});
clients.push_back(client); // keep it, or the connection closes
return true;
});
// Client, usually in another process.
std::shared_ptr<datapath::socket> sock;
if (datapath::connect(sock, "my-service") == datapath::error::Success) {
sock->on_message([](const uint8_t* data, size_t size) { /* valid during the call */ });
sock->send("hello", 5);
}
Shared memory
On by default on Windows and Linux. A peer with it off, or on macOS, connects over the pipe or socket as usual. To turn it off for a connection, or to skip the copy out of the ring for a peer you trust:
datapath::options opts;
opts.shared_memory = false; // pipe or socket only
// opts.shared_memory_zero_copy = true; // trusted same-user peer: no copy before on_message
datapath::host(server, "my-service", opts);
datapath::connect(sock, "my-service", opts);
Next: semantics and tuning, the headers in include/datapath, and a complete echo program.
C
Install
The C API is part of the same library: link datapath::datapath as shown for C++. Build with -DDATAPATH_SHARED=ON for a shared library that other languages can load through their FFI (Java via JNI, C# via P/Invoke); on Linux it exports only the C API.
Send a message
#include <datapath/datapath.h>
#include <stdio.h>
static void on_echo(void* user, datapath_socket* socket, const uint8_t* data, size_t size)
{
datapath_socket_send(socket, data, size); /* safe from callbacks */
}
static int on_accept(void* user, datapath_server* server, datapath_socket* client)
{
*(datapath_socket**)user = client; /* accepted: ours to release later */
datapath_socket_set_on_message(client, on_echo, NULL, NULL);
return 1; /* 0 rejects the client */
}
static void on_reply(void* user, datapath_socket* socket, const uint8_t* data, size_t size)
{
printf("%.*s\n", (int)size, (const char*)data);
}
int main(void)
{
datapath_server* server = NULL;
datapath_socket* peer = NULL;
if (datapath_host("my-service", NULL, NULL, &server) != DATAPATH_SUCCESS)
return 1;
datapath_server_set_on_accept(server, on_accept, &peer, NULL);
datapath_socket* sock = NULL;
datapath_error err = datapath_connect("my-service", NULL, NULL, &sock);
if (err != DATAPATH_SUCCESS) {
fprintf(stderr, "connect: %s\n", datapath_error_string(err));
return 1;
}
datapath_socket_set_on_message(sock, on_reply, NULL, NULL);
datapath_socket_send(sock, "hello", 5);
/* ... run until done ... */
datapath_socket_release(sock);
datapath_socket_release(peer);
datapath_server_release(server);
return 0;
}
Initialise option structs with datapath_options_init() before changing fields (shared memory is on by default; set shared_memory = 0 to turn it off), or pass NULL for the defaults.
Next: datapath.h documents threading and ownership for every call.
Rust
Install
The crate builds the library from source, which needs CMake 3.16+ and a C++17 compiler.
cargo add com-xaymar-datapath --rename datapath
Send a message
use datapath::Socket;
use std::sync::{Arc, Mutex};
// Echo server. The accept handler owns each new client socket.
let server = datapath::host("my-service")?;
let clients = Arc::new(Mutex::new(Vec::new()));
let keep = clients.clone();
server.on_accept(move |client: Socket| {
client.on_message(|socket, data| { let _ = socket.send(data); }).ok();
keep.lock().unwrap().push(client); // keep it, or the connection closes
})?;
// Client.
let socket = datapath::connect("my-service")?;
socket.on_message(|_, data| println!("got {} bytes", data.len()))?;
socket.send(b"hello")?;
Shared memory is on by default. To turn it off, set shared_memory = false on an Options::default() and pass it to Server::host and Socket::connect.
Next: the Rust guide.
Python
Install
Wheels for CPython 3.10+ on Windows and Linux, x86-64 and ARM64, including free-threaded builds.
pip install com.xaymar.datapath
Send a message
import datapath
# Server: echo every message.
clients = []
def on_accept(sock):
clients.append(sock) # keep it, or the connection closes
sock.on_message(lambda data: sock.send(data)) # runs on an I/O thread
server = datapath.host("my-service", on_accept=on_accept)
# Client, in another process
sock = datapath.connect("my-service")
sock.on_message(lambda data: print(data)) # b'hello'
sock.send(b"hello")
The blocking accept(), recv() and recv_into() are deprecated since 1.1 and go away in 2.0. Shared memory is on by default; pass shared_memory=False to connect() or host() to turn it off.
Next: the Python guide.
Node.js
Install
Prebuilt binaries for Windows and Linux on Node.js 18+, with TypeScript typings. Other platforms compile from source.
npm install @xaymar/datapath
Send a message
const datapath = require('@xaymar/datapath');
// Server: echo every message.
const server = datapath.host('my-service', (socket) => {
socket.on('message', (data) => socket.send(data));
});
// Client.
const socket = await datapath.connect('my-service');
socket.on('message', (data) => console.log(data.toString()));
socket.send('hello');
Shared memory is on by default; pass sharedMemory: false in the options to turn it off.
Next: the Node.js guide and how it differs from C++.
Names and paths
A bare name such as my-service maps to \\.\pipe\my-service on Windows and to the abstract socket datapath.my-service on Linux. On Linux and macOS a path starting with / is a filesystem socket. Only the current user may connect unless the server allows Group or World.
Things to know
- Handlers run on I/O threads. Keep them short.
on_messagefor one socket never runs concurrently with itself. - Receiving starts with the first
on_message. Until then data waits in the kernel, and a peer closing goes unnoticed. send()never blocks. It returnsWouldBlockpast 64 MiB queued andTooLargeabove the negotiated message size (64 MiB by default).- Dropping the last handle closes. Keep accepted sockets somewhere, and don't capture a socket's owning handle in its own handler.