gostream

package
v0.0.0-...-8b9b725 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2019 License: MIT, MIT Imports: 7 Imported by: 4

README

go-libp2p-gostream

Build Status codecov standard-readme compliant

Go "net" wrappers for LibP2P

Package gostream allows to replace the standard net stack in Go with LibP2P streams.

Given a libp2p.Host, gostream provides Dial() and Listen() methods which return implementations of net.Conn and net.Listener.

Instead of the regular "host:port" addressing, gostream uses a Peer ID, and rather than a raw TCP connection, gostream will use libp2p's net.Stream. This means your connections will take advantage of LibP2P's multi-routes, NAT transversal and stream multiplexing.

Table of Contents

Install

This package uses gx for dependencies and should be imported with gx on other projects:

$ gx import github.com/hsanjuan/go-libp2p-gostream

The code can be downloaded and tested with:

$ go get -u -d github.com/hsanjuan/go-libp2p-gostream
$ cd $GOPATH/src/github.com/hsanjuan/go-libp2p-gostream
$ make test

Usage

Documentation can be read at Godoc. The important bits follow.

A simple communication between peers -one acting as server and the other as client- would work like:

go func() {
	listener, _ := Listen(srvHost, tag)
	defer listener.Close()
	servConn, _ := listener.Accept()
	defer servConn.Close()
	reader := bufio.NewReader(servConn)
	msg, _ := reader.ReadString('\n')
	fmt.Println(msg)
	servConn.Write([]byte("answer!\n"))
}()
clientConn, _ := Dial(clientHost, srvHost.ID(), tag)
clientConn.Write([]byte("question?\n"))
resp, _ := ioutil.ReadAll(clientConn)
fmt.Println(resp)

Note error handling above is ommited.

Contribute

PRs accepted.

License

MIT © Hector Sanjuan

Documentation

Overview

Package gostream allows to replace the standard net stack in Go with [LibP2P](https://github.com/libp2p/libp2p) streams.

Given a libp2p.Host, gostream provides Dial() and Listen() methods which return implementations of net.Conn and net.Listener.

Instead of the regular "host:port" addressing, `gostream` uses a Peer ID, and rather than a raw TCP connection, gostream will use libp2p's net.Stream. This means your connections will take advantage of LibP2P's multi-routes, NAT transversal and stream multiplexing.

Note that LibP2P hosts cannot dial to themselves, so there is no possibility of using the same Host as server and as client.

Index

Constants

This section is empty.

Variables

View Source
var Network = "libp2p"

Network is the "net.Addr.Network()" name returned by addresses used by gostream connections. In turn, the "net.Addr.String()" will be a peer ID.

Functions

func Dial

func Dial(h host.Host, pid peer.ID, tag protocol.ID) (net.Conn, error)

Dial opens a stream to the destination address (which should parseable to a peer ID) using the given host and returns it as a standard net.Conn.

func Listen

func Listen(h host.Host, tag protocol.ID) (net.Listener, error)

Listen provides a standard net.Listener ready to accept "connections". Under the hood, these connections are libp2p streams tagged with the given protocol.ID.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL