-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
119 lines (102 loc) · 2.43 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package emux
import (
"context"
"fmt"
"net"
"os"
"reflect"
"runtime"
"strings"
"sync"
"time"
)
const (
bufSize = 32 * 1024
)
var (
ErrClosed = net.ErrClosed
ErrTimeout = fmt.Errorf("timeout")
ErrAlreadyStarted = fmt.Errorf("session already started")
errUnknownStreamID = fmt.Errorf("unknown stream id")
errStreamIsAlreadyReady = fmt.Errorf("stream is already ready")
errNoFreeStreamID = fmt.Errorf("emux: no free stream id")
errShortRead = fmt.Errorf("read length not equal to body length")
errStreamAlreadyExists = fmt.Errorf("stream id already exists")
errNoData = fmt.Errorf("data length cannot be zero")
)
type Flusher interface {
Buffered() int
Flush() error
}
type ByteReader interface {
ReadByte() (byte, error)
}
// ListenConfig contains options for listening to an address.
type ListenConfig interface {
Listen(ctx context.Context, network, address string) (net.Listener, error)
}
// Dialer contains options for connecting to an address.
type Dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
// Logger is the interface for logging.
type Logger interface {
Println(v ...interface{})
}
// BytesPool is the interface for a bytes pool.
type BytesPool interface {
Get() []byte
Put([]byte)
}
type deadline interface {
SetDeadline(t time.Time) error
}
// isClosedConnError reports whether err is an error from use of a closed
// network connection.
func isClosedConnError(err error) bool {
if err == nil {
return false
}
if err == ErrClosed {
return true
}
str := err.Error()
if strings.Contains(str, "use of closed network connection") {
return true
}
if runtime.GOOS == "windows" {
if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
const WSAECONNABORTED = 10053
const WSAECONNRESET = 10054
if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
return true
}
}
}
}
return false
}
func errno(v error) uintptr {
if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
return uintptr(rv.Uint())
}
return 0
}
type onceError struct {
sync.Mutex // guards following
err error
}
func (a *onceError) Store(err error) {
a.Lock()
defer a.Unlock()
if a.err != nil {
return
}
a.err = err
}
func (a *onceError) Load() error {
a.Lock()
defer a.Unlock()
return a.err
}