streammux

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2015 License: MIT, MIT Imports: 2 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/jbenet/go-stream-muxer

Implementations

Badge

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

Client example

import (
  "net"
  "fmt"
  "io"
  ymux "github.com/jbenet/go-stream-muxer/yamux"
  smux "github.com/jbenet/go-stream-muxer"
)

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

  go sconn.Serve(func(smux.Stream) {}) // no-op

  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"
  ymux "github.com/jbenet/go-stream-muxer/yamux"
  smux "github.com/jbenet/go-stream-muxer"
)

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

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

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

      go sc.Serve(func(s smux.Stream) {
        fmt.Println("serving connection")
        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 NoOpHandler = func(s Stream) { s.Close() }

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

Functions

This section is empty.

Types

type Conn

type Conn interface {
	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)

	// Serve starts a loop, accepting incoming requests and calling
	// `StreamHandler with them. (Use _instead of_ accept. not both.)
	Serve(StreamHandler)
}

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

type Stream

type Stream interface {
	io.Reader
	io.Writer
	io.Closer
}

Stream is a bidirectional io pipe within a connection

type StreamHandler

type StreamHandler func(Stream)

StreamHandler is a function that handles streams (usually those opened by the remote side)

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.

Directories

Path Synopsis
package multistream implements a peerstream transport using go-multistream to select the underlying stream muxer
package multistream implements a peerstream transport using go-multistream to select the underlying stream muxer

Jump to

Keyboard shortcuts

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