streammux

package module
Version: v0.0.0-...-2ba2316 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2018 License: MIT Imports: 4 Imported by: 0

README

go-stream-muxer - generalized stream multiplexing

go-stream-muxer is a common interface for stream muxers, with common tests. It wraps other stream muxers (like muxado, spdystream and yamux).

A test suite and interface you can use to implement a stream muxer.

Godoc: https://godoc.org/github.com/libp2p/go-stream-muxer

Implementations

Badge

Include this badge in your readme if you make a new module that uses abstract-stream-muxer API.

Installation

go get -d github.com/libp2p/go-stream-muxer
cd $GOPATH/src/github.com/libp2p/go-stream-muxer
make deps

Client example

import (
  "net"
  "fmt"
  "io"
  "os"

  ymux "github.com/whyrusleeping/go-smux-yamux"
)

func dial() {
  nconn, _ := net.Dial("tcp", "localhost:1234")
  sconn, _ := ymux.DefaultTransport.NewConn(nconn, false) // false == client

  go func() {
    // no-op
    for {
      sconn.AcceptStream()
    }
  }()

  s1, _ := sconn.OpenStream()
  s1.Write([]byte("hello"))

  s2, _ := sconn.OpenStream()
  s2.Write([]byte("world"))

  length := 20
  buf2 := make([]byte, length)
  fmt.Printf("reading %d bytes from stream (echoed)\n", length)

  s1.Read(buf2)

  fmt.Printf("received %s as a response\n", string(buf2))

  s3, _ := sconn.OpenStream()
  io.Copy(s3, os.Stdin)
}

Server example

import (
  "net"
  "fmt"
  "io"

  smux "github.com/libp2p/go-stream-muxer"
  ymux "github.com/whyrusleeping/go-smux-yamux"
)

func listen() {
  tr := ymux.DefaultTransport
  l, _ := net.Listen("tcp", "localhost:1234")

  for {
    c, _ := l.Accept()

    fmt.Println("accepted connection")
    sc, _ := tr.NewConn(c, true)

    go func() {
      fmt.Println("serving connection")
      for {
        s, _ := sc.AcceptStream()
        echoStream(s)
      }
    }()
  }
}

func echoStream(s smux.Stream) {
  defer s.Close()

  fmt.Println("accepted stream")
  io.Copy(s, s) // echo everything
  fmt.Println("closing stream")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrReset = errors.New("stream reset")

ErrReset is returned when reading or writing on a reset stream.

View Source
var NoOpHandler = func(s Stream) { s.Reset() }

NoOpHandler do nothing. Resets streams as soon as they are opened.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	// Close closes the stream muxer and the the underlying net.Conn.
	io.Closer

	// IsClosed returns whether a connection is fully closed, so it can
	// be garbage collected.
	IsClosed() bool

	// OpenStream creates a new stream.
	OpenStream() (Stream, error)

	// AcceptStream accepts a stream opened by the other side.
	AcceptStream() (Stream, error)
}

Conn is a stream-multiplexing connection to a remote peer.

type Stream

type Stream interface {
	io.Reader
	io.Writer

	// Close closes the stream for writing. Reading will still work (that
	// is, the remote side can still write).
	io.Closer

	// Reset closes both ends of the stream. Use this to tell the remote
	// side to hang up and go away.
	Reset() error

	SetDeadline(time.Time) error
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
}

Stream is a bidirectional io pipe within a connection.

type Transport

type Transport interface {

	// NewConn constructs a new connection
	NewConn(c net.Conn, isServer bool) (Conn, error)
}

Transport constructs go-stream-muxer compatible connections.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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