2022-08-19 16:34:07 +00:00
|
|
|
package mplex
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
mp "github.com/libp2p/go-mplex"
|
|
|
|
)
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// stream implements network.MuxedStream over mplex.Stream.
|
2021-06-16 20:19:45 +00:00
|
|
|
type stream mp.Stream
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
var _ network.MuxedStream = &stream{}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
func (s *stream) Read(b []byte) (n int, err error) {
|
|
|
|
n, err = s.mplex().Read(b)
|
|
|
|
if err == mp.ErrStreamReset {
|
2022-04-01 16:16:46 +00:00
|
|
|
err = network.ErrReset
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stream) Write(b []byte) (n int, err error) {
|
|
|
|
n, err = s.mplex().Write(b)
|
|
|
|
if err == mp.ErrStreamReset {
|
2022-04-01 16:16:46 +00:00
|
|
|
err = network.ErrReset
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|