commit 3023356beb10273993a8a6d2f9c069194f9b35ca Author: Juan Batiz-Benet Date: Sat Jul 11 18:51:47 2015 -0700 extracted from go-peerstream diff --git a/p2p/muxer/mplex/multiplex.go b/p2p/muxer/mplex/multiplex.go new file mode 100644 index 000000000..62868a841 --- /dev/null +++ b/p2p/muxer/mplex/multiplex.go @@ -0,0 +1,44 @@ +package peerstream_multiplex + +import ( + "net" + + smux "github.com/jbenet/go-stream-mux" + mp "github.com/jbenet/go-stream-mux/Godeps/_workspace/src/github.com/whyrusleeping/go-multiplex" // Conn is a connection to a remote peer. +) + +type conn struct { + *mp.Multiplex +} + +func (c *conn) Close() error { + return c.Multiplex.Close() +} + +func (c *conn) IsClosed() bool { + return c.Multiplex.IsClosed() +} + +// OpenStream creates a new stream. +func (c *conn) OpenStream() (smux.Stream, error) { + return c.Multiplex.NewStream(), nil +} + +// Serve starts listening for incoming requests and handles them +// using given StreamHandler +func (c *conn) Serve(handler smux.StreamHandler) { + c.Multiplex.Serve(func(s *mp.Stream) { + handler(s) + }) +} + +// Transport is a go-peerstream transport that constructs +// multiplex-backed connections. +type Transport struct{} + +// DefaultTransport has default settings for multiplex +var DefaultTransport = &Transport{} + +func (t *Transport) NewConn(nc net.Conn, isServer bool) (smux.Conn, error) { + return &conn{mp.NewMultiplex(nc, isServer)}, nil +} diff --git a/p2p/muxer/mplex/multiplex_test.go b/p2p/muxer/mplex/multiplex_test.go new file mode 100644 index 000000000..1b8ffd76a --- /dev/null +++ b/p2p/muxer/mplex/multiplex_test.go @@ -0,0 +1,11 @@ +package peerstream_multiplex + +import ( + "testing" + + test "github.com/jbenet/go-stream-mux/test" +) + +func TestMultiplexTransport(t *testing.T) { + test.SubtestAll(t, DefaultTransport) +}