@@ -3,6 +3,7 @@ package network
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"io"
7
8
"net"
8
9
"time"
@@ -11,6 +12,33 @@ import (
11
12
// ErrReset is returned when reading or writing on a reset stream.
12
13
var ErrReset = errors .New ("stream reset" )
13
14
15
+ type StreamErrorCode uint32
16
+
17
+ type StreamError struct {
18
+ ErrorCode StreamErrorCode
19
+ Remote bool
20
+ TransportError error
21
+ }
22
+
23
+ func (s * StreamError ) Error () string {
24
+ side := "local"
25
+ if s .Remote {
26
+ side = "remote"
27
+ }
28
+ if s .TransportError != nil {
29
+ return fmt .Sprintf ("stream reset (%s): code: %d: transport error: %s" , side , s .ErrorCode , s .TransportError )
30
+ }
31
+ return fmt .Sprintf ("stream reset (%s): code: %d" , side , s .ErrorCode )
32
+ }
33
+
34
+ func (s * StreamError ) Is (target error ) bool {
35
+ return target == ErrReset
36
+ }
37
+
38
+ func (s * StreamError ) Unwrap () error {
39
+ return s .TransportError
40
+ }
41
+
14
42
// MuxedStream is a bidirectional io pipe within a connection.
15
43
type MuxedStream interface {
16
44
io.Reader
@@ -61,6 +89,13 @@ type MuxedStream interface {
61
89
SetWriteDeadline (time.Time ) error
62
90
}
63
91
92
+ type ResetWithErrorer interface {
93
+ // ResetWithError closes both ends of the stream with errCode. The errCode is sent
94
+ // to the peer on a best effort basis. For transports that do not support sending
95
+ // error codes to remote peer, the behavior is identical to calling Reset
96
+ ResetWithError (errCode StreamErrorCode ) error
97
+ }
98
+
64
99
// MuxedConn represents a connection to a remote peer that has been
65
100
// extended to support stream multiplexing.
66
101
//
@@ -86,6 +121,12 @@ type MuxedConn interface {
86
121
AcceptStream () (MuxedStream , error )
87
122
}
88
123
124
+ type CloseWithErrorer interface {
125
+ // CloseWithError closes the connection with errCode. The errCode is sent
126
+ // to the peer.
127
+ CloseWithError (errCode ConnErrorCode ) error
128
+ }
129
+
89
130
// Multiplexer wraps a net.Conn with a stream multiplexing
90
131
// implementation and returns a MuxedConn that supports opening
91
132
// multiple streams over the underlying net.Conn
0 commit comments