-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update boxo and internalize mplex
- Loading branch information
Showing
21 changed files
with
368 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/conn.go | ||
package mplex | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p/core/network" | ||
|
||
mp "github.com/libp2p/go-mplex" | ||
) | ||
|
||
type conn mp.Multiplex | ||
|
||
var _ network.MuxedConn = &conn{} | ||
|
||
// NewMuxedConn constructs a new Conn from a *mp.Multiplex. | ||
func NewMuxedConn(m *mp.Multiplex) network.MuxedConn { | ||
return (*conn)(m) | ||
} | ||
|
||
func (c *conn) Close() error { | ||
return c.mplex().Close() | ||
} | ||
|
||
func (c *conn) IsClosed() bool { | ||
return c.mplex().IsClosed() | ||
} | ||
|
||
// OpenStream creates a new stream. | ||
func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) { | ||
s, err := c.mplex().NewStream(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return (*stream)(s), nil | ||
} | ||
|
||
// AcceptStream accepts a stream opened by the other side. | ||
func (c *conn) AcceptStream() (network.MuxedStream, error) { | ||
s, err := c.mplex().Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return (*stream)(s), nil | ||
} | ||
|
||
func (c *conn) mplex() *mp.Multiplex { | ||
return (*mp.Multiplex)(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/stream.go | ||
package mplex | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p/core/network" | ||
|
||
mp "github.com/libp2p/go-mplex" | ||
) | ||
|
||
// stream implements network.MuxedStream over mplex.Stream. | ||
type stream mp.Stream | ||
|
||
var _ network.MuxedStream = &stream{} | ||
|
||
func (s *stream) Read(b []byte) (n int, err error) { | ||
n, err = s.mplex().Read(b) | ||
if err == mp.ErrStreamReset { | ||
err = network.ErrReset | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func (s *stream) Write(b []byte) (n int, err error) { | ||
n, err = s.mplex().Write(b) | ||
if err == mp.ErrStreamReset { | ||
err = network.ErrReset | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func (s *stream) Close() error { | ||
return s.mplex().Close() | ||
} | ||
|
||
func (s *stream) CloseWrite() error { | ||
return s.mplex().CloseWrite() | ||
} | ||
|
||
func (s *stream) CloseRead() error { | ||
return s.mplex().CloseRead() | ||
} | ||
|
||
func (s *stream) Reset() error { | ||
return s.mplex().Reset() | ||
} | ||
|
||
func (s *stream) SetDeadline(t time.Time) error { | ||
return s.mplex().SetDeadline(t) | ||
} | ||
|
||
func (s *stream) SetReadDeadline(t time.Time) error { | ||
return s.mplex().SetReadDeadline(t) | ||
} | ||
|
||
func (s *stream) SetWriteDeadline(t time.Time) error { | ||
return s.mplex().SetWriteDeadline(t) | ||
} | ||
|
||
func (s *stream) mplex() *mp.Stream { | ||
return (*mp.Stream)(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/transport.go | ||
package mplex | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/libp2p/go-libp2p/core/network" | ||
|
||
mp "github.com/libp2p/go-mplex" | ||
) | ||
|
||
// DefaultTransport has default settings for Transport | ||
var DefaultTransport = &Transport{} | ||
|
||
const ID = "/mplex/6.7.0" | ||
|
||
var _ network.Multiplexer = &Transport{} | ||
|
||
// Transport implements mux.Multiplexer that constructs | ||
// mplex-backed muxed connections. | ||
type Transport struct{} | ||
|
||
func (t *Transport) NewConn(nc net.Conn, isServer bool, scope network.PeerScope) (network.MuxedConn, error) { | ||
m, err := mp.NewMultiplex(nc, isServer, scope) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewMuxedConn(m), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Code copied from https://github.com/libp2p/go-libp2p/blob/9bd85029550a084fca63ec6ff9184122cdf06591/p2p/muxer/mplex/transport_test.go | ||
package mplex | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
"github.com/libp2p/go-libp2p/core/network" | ||
test "github.com/libp2p/go-libp2p/p2p/muxer/testsuite" | ||
) | ||
|
||
func TestDefaultTransport(t *testing.T) { | ||
test.SubtestAll(t, DefaultTransport) | ||
} | ||
|
||
type memoryScope struct { | ||
network.PeerScope | ||
limit int | ||
reserved int | ||
} | ||
|
||
func (m *memoryScope) ReserveMemory(size int, prio uint8) error { | ||
if m.reserved+size > m.limit { | ||
return errors.New("too much") | ||
} | ||
m.reserved += size | ||
return nil | ||
} | ||
|
||
func (m *memoryScope) ReleaseMemory(size int) { | ||
m.reserved -= size | ||
if m.reserved < 0 { | ||
panic("too much memory released") | ||
} | ||
} | ||
|
||
type memoryLimitedTransport struct { | ||
Transport | ||
} | ||
|
||
func (t *memoryLimitedTransport) NewConn(nc net.Conn, isServer bool, scope network.PeerScope) (network.MuxedConn, error) { | ||
return t.Transport.NewConn(nc, isServer, &memoryScope{ | ||
limit: 3 * 1 << 20, | ||
PeerScope: scope, | ||
}) | ||
} | ||
|
||
func TestDefaultTransportWithMemoryLimit(t *testing.T) { | ||
test.SubtestAll(t, &memoryLimitedTransport{ | ||
Transport: *DefaultTransport, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.