All Projects → libp2p → go-stream-muxer

libp2p / go-stream-muxer

Licence: MIT license
[DEPRECATED] stream multiplexer interface; use https://github.com/libp2p/go-libp2p-core/ instead.

Programming Languages

go
31211 projects - #10 most used programming language

go-stream-muxer - generalized stream multiplexing

Discourse posts

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

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

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")
}

The last gx published version of this module was: 3.1.0: QmVtV1y2e8W4eQgzsP6qfSpCCZ6zWYE4m6NzJjB7iswwrT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].