Skip to content

Commit

Permalink
Merge pull request #72 from Wondertan/fix/reset-error
Browse files Browse the repository at this point in the history
Remove dependency on go-libp2p-core and introduce new errors.
  • Loading branch information
Stebalien authored Feb 28, 2020
2 parents cd781ef + b36a181 commit 57c272b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module github.com/libp2p/go-mplex

go 1.13

require (
github.com/ipfs/go-log v0.0.1
github.com/libp2p/go-buffer-pool v0.0.2
github.com/libp2p/go-libp2p-core v0.3.0
)
4 changes: 1 addition & 3 deletions multiplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"net"
"testing"
"time"

tmux "github.com/libp2p/go-libp2p-core/mux"
)

func init() {
Expand Down Expand Up @@ -391,7 +389,7 @@ func TestResetAfterEOF(t *testing.T) {
sb.Reset()

n, err = sa.Read([]byte{0})
if n != 0 || err != tmux.ErrReset {
if n != 0 || err != ErrStreamReset {
t.Fatal(err)
}
}
Expand Down
15 changes: 9 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"sync"
"time"

"github.com/libp2p/go-libp2p-core/mux"

pool "github.com/libp2p/go-buffer-pool"
)

var (
ErrStreamReset = errors.New("stream reset")
ErrStreamClosed = errors.New("closed stream")
)

// streamID is a convenience type for operating on stream IDs
type streamID struct {
id uint64
Expand Down Expand Up @@ -74,7 +77,7 @@ func (s *Stream) waitForData() error {
case <-s.reset:
// This is the only place where it's safe to return these.
s.returnBuffers()
return mux.ErrReset
return ErrStreamReset
case read, ok := <-s.dataIn:
if !ok {
return io.EOF
Expand Down Expand Up @@ -112,7 +115,7 @@ func (s *Stream) returnBuffers() {
func (s *Stream) Read(b []byte) (int, error) {
select {
case <-s.reset:
return 0, mux.ErrReset
return 0, ErrStreamReset
default:
}
if s.extra == nil {
Expand Down Expand Up @@ -160,14 +163,14 @@ func (s *Stream) Write(b []byte) (int, error) {

func (s *Stream) write(b []byte) (int, error) {
if s.isClosed() {
return 0, errors.New("cannot write to closed stream")
return 0, ErrStreamClosed
}

err := s.mp.sendMsg(s.wDeadline.wait(), s.id.header(messageTag), b)

if err != nil {
if err == context.Canceled {
err = errors.New("cannot write to closed stream")
err = ErrStreamClosed
}
return 0, err
}
Expand Down

0 comments on commit 57c272b

Please sign in to comment.