Categorygithub.com/multiformats/go-multistream
modulepackage
0.6.0
Repository: https://github.com/multiformats/go-multistream.git
Documentation: pkg.go.dev

# README

go-multistream

GoDoc Travis CI codecov.io

an implementation of the multistream protocol in go

This package implements a simple stream router for the multistream-select protocol. The protocol is defined here.

Table of Contents

Install

go-multistream is a standard Go module which can be installed with:

go get github.com/multiformats/go-multistream

Usage

Example

This example shows how to use a multistream muxer. A muxer uses user-added handlers to handle different "protocols". The first step when interacting with a connection handler by the muxer is to select the protocol (the example uses SelectProtoOrFail). This will then let the muxer use the right handler.

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"net"

	ms "github.com/multiformats/go-multistream"
)

// This example creates a multistream muxer, adds handlers for the protocols
// "/cats" and "/dogs" and exposes it on a localhost:8765. It then opens connections
// to that port, selects the protocols and tests that the handlers are working.
func main() {
	mux := ms.NewMultistreamMuxer[string]()
	mux.AddHandler("/cats", func(proto string, rwc io.ReadWriteCloser) error {
		fmt.Fprintln(rwc, proto, ": HELLO I LIKE CATS")
		return rwc.Close()
	})
	mux.AddHandler("/dogs", func(proto string, rwc io.ReadWriteCloser) error {
		fmt.Fprintln(rwc, proto, ": HELLO I LIKE DOGS")
		return rwc.Close()
	})

	list, err := net.Listen("tcp", ":8765")
	if err != nil {
		panic(err)
	}

	go func() {
		for {
			con, err := list.Accept()
			if err != nil {
				panic(err)
			}

			go mux.Handle(con)
		}
	}()

	// The Muxer is ready, let's test it
	conn, err := net.Dial("tcp", ":8765")
	if err != nil {
		panic(err)
	}

	// Create a new multistream to talk to the muxer
	// which will negotiate that we want to talk with /cats
	mstream := ms.NewMSSelect(conn, "/cats")
	cats, err := ioutil.ReadAll(mstream)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", cats)
	mstream.Close()

	// A different way of talking to the muxer
	// is to manually selecting the protocol ourselves
	conn, err = net.Dial("tcp", ":8765")
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	err = ms.SelectProtoOrFail("/dogs", conn)
	if err != nil {
		panic(err)
	}
	dogs, err := ioutil.ReadAll(conn)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", dogs)
	conn.Close()
}

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2016 Jeromy Johnson

# Functions

NewMSSelect returns a new Multistream which is able to perform protocol selection with a MultistreamMuxer.
NewMultistream returns a multistream for the given protocol.
NewMultistreamMuxer creates a muxer.
ReadNextToken extracts a token from a Reader.
ReadNextTokenBytes extracts a token from a Reader.
SelectOneOf will perform handshakes with the protocols on the given slice until it finds one which is supported by the muxer.
SelectProtoOrFail performs the initial multistream handshake to inform the muxer of the protocol that will be used to communicate on this ReadWriteCloser.

# Constants

ProtocolID identifies the multistream protocol itself and makes sure the multistream muxers on both sides of a channel can work with each other.

# Variables

ErrIncorrectVersion is an error reported when the muxer protocol negotiation fails because of a ProtocolID mismatch.
ErrNoProtocols is the error returned when the no protocols have been specified.
ErrTooLarge is an error to signal that an incoming message was too large.

# Structs

ErrNotSupported is the error returned when the muxer doesn't support the protocols tried for the handshake.
No description provided by the author
Handler is a wrapper to HandlerFunc which attaches a name (protocol) and a match function which can optionally be used to select a handler by other means than the name.
MultistreamMuxer is a muxer for multistream.

# Interfaces

LazyConn is the connection type returned by the lazy negotiation functions.
StringLike is an interface that supports all types with underlying type string.

# Type aliases

HandlerFunc is a user-provided function used by the MultistreamMuxer to handle a protocol/stream.