#client-server #sockets #wrapper #unix #tokio #unix-domain-socket #byte-array

local-socket

Simple client/server wrapper for a tokio unix domain socket

1 unstable release

new 0.1.0 Apr 25, 2024

#10 in #unix-domain-socket

Custom license

11KB
211 lines

local-socket

Simple client / server wrapper for a tokio backed unix domnain socket.

There is no need to roll your own message framing. Under the hood, this handles message framing when sending byte arrays over the wire.

When a client sends a byte array to the server - and vice-versa - the server is guarenteed to receive a single event on the other side with the complete array.

Usage

Server:

let dir = TempDir::new().unwrap();
let socket = dir.path().join("foobar.socket");

// Create a new server listening at a filesystem path
let mut server = SocketServer::listen(socket).unwrap();

loop {

  // Handle inbound connections
  let mut connection = server.next().await.unwrap().unwrap();

  // Receive a message from the client
  let msg = connection.next().await.unwrap().unwrap();

  // Send the client a message
  connection.write("pong".into()).unwrap();
}

Client:

let mut connection = SocketConnection::connect(socket).await.unwrap();

// Ping - Pong
loop {
  // Send the server a message
  connection.write("ping".into()).unwrap();

  // Receive a message from the server
  let msg = connection.next().await.unwrap().unwrap();
}

Dependencies

~4–15MB
~136K SLoC