#p2p #networking #connection #node #tcp-connection #low-level #customizable

pea2pea

A simple, low-level, and customizable implementation of a TCP P2P node

66 releases (breaking)

0.49.0 Dec 29, 2023
0.48.0 Jul 3, 2023
0.47.0 May 18, 2023
0.45.0 Jan 13, 2023
0.8.0 Dec 31, 2020

#576 in Network programming

Download history 47/week @ 2023-12-31 175/week @ 2024-01-07 176/week @ 2024-01-14 132/week @ 2024-01-21 117/week @ 2024-01-28 359/week @ 2024-02-04 344/week @ 2024-02-11 284/week @ 2024-02-18 384/week @ 2024-02-25 353/week @ 2024-03-03 283/week @ 2024-03-10 427/week @ 2024-03-17 327/week @ 2024-03-24 449/week @ 2024-03-31 241/week @ 2024-04-07 263/week @ 2024-04-14

1,360 downloads per month
Used in 2 crates

CC0 license

230KB
1K SLoC

pea2pea

crates.io docs.rs dependencies actively developed issues

pea2pea is a simple, low-level, and customizable implementation of a TCP P2P node.

The core library only provides the most basic functionalities like starting, ending and maintaining connections; the rest is up to a few low-level, opt-in protocols:

  • Handshake requires connections to adhere to the given handshake logic before anything else can be done with them
  • Reading enables the node to receive messages based on the user-supplied Decoder
  • Writing enables the node to send messages based on the user-supplied Encoder
  • OnDisconnect makes the node perform specified actions whenever a connection with a peer is severed
  • OnConnect makes the node perform specified actions whenever a connection with a peer is fully established (post-handshake)

goals

  • small, simple, non-framework codebase: the entire library is ~1k LOC and there are few dependencies
  • ease of use: few objects and traits, no "turboeels" or generics/references that would force all parent objects to adapt
  • correctness: builds with stable Rust, there is no unsafe code, there's more code in tests than in the actual library
  • low-level: the user has full control over all connections and every byte sent or received
  • good performance: over 10GB/s in favorable scenarios, small memory footprint

how to use it

  1. define a clonable struct containing a Node and any extra state you'd like to carry alongside it
  2. implement the trivial Pea2Pea trait for it
  3. make it implement any/all of the protocols
  4. create that struct (or as many of them as you like)
  5. enable the protocols you'd like them to utilize

That's it!

examples

  • including noise encryption, simple interop with libp2p, or TLS connections

status

  • all the desired functionalities are complete
  • the crate follows semver, and some API breakage is still possible before 1.0
  • the project is actively developed, with all changes being recorded in the CHANGELOG
  • some of the desired features that are not yet available in Rust include associated type defaults
  • the project aims to always build with the current stable Rust compiler; legacy version support is not a goal, but they might also work

Dependencies

~4–15MB
~141K SLoC