-
Notifications
You must be signed in to change notification settings - Fork 2
/
netconn.go
159 lines (131 loc) · 2.84 KB
/
netconn.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package mockconn
import (
"errors"
"fmt"
"net"
"sync"
"time"
)
var (
ErrConnNotEstablished error = errors.New("NetConn is not established")
)
type NetConn struct {
sendConn *UniConn
recvConn *UniConn
readMu sync.RWMutex
pauseRead bool
writeMu sync.RWMutex
pauseWrite bool
}
// An implement of net.Conn interface
func NewNetConn(sendConn, recvConn *UniConn) *NetConn {
nc := &NetConn{sendConn: sendConn, recvConn: recvConn}
return nc
}
func (nc *NetConn) Write(b []byte) (n int, err error) {
if nc.sendConn == nil {
return 0, ErrConnNotEstablished
}
nc.writeMu.RLock()
defer nc.writeMu.RUnlock()
if nc.pauseWrite {
return 0, errors.New("NetConn writing is paused")
}
return nc.sendConn.Write(b)
}
func (nc *NetConn) Read(b []byte) (n int, err error) {
if nc.recvConn == nil {
return 0, ErrConnNotEstablished
}
nc.readMu.RLock()
defer nc.readMu.RUnlock()
if nc.pauseRead {
return 0, errors.New("NetConn reading is paused")
}
return nc.recvConn.Read(b)
}
func (nc *NetConn) Close() error {
if nc.sendConn == nil || nc.recvConn == nil {
return ErrConnNotEstablished
}
nc.sendConn.CloseWrite()
nc.recvConn.CloseRead()
return nil
}
func (nc *NetConn) CloseRead() error {
return nc.recvConn.CloseRead()
}
func (nc *NetConn) CloseWrite() error {
return nc.sendConn.CloseWrite()
}
func (nc *NetConn) LocalAddr() net.Addr {
if nc.sendConn == nil {
return nil
}
return nc.sendConn.LocalAddr()
}
func (nc *NetConn) RemoteAddr() net.Addr {
if nc.sendConn == nil {
return nil
}
return nc.sendConn.RemoteAddr()
}
func (nc *NetConn) SetDeadline(t time.Time) error {
if nc.sendConn == nil {
return ErrConnNotEstablished
}
if nc.recvConn == nil {
return ErrConnNotEstablished
}
err := nc.sendConn.SetDeadline(t)
if err != nil {
return err
}
err = nc.recvConn.SetDeadline(t)
if err != nil {
return err
}
return nil
}
func (nc *NetConn) SetReadDeadline(t time.Time) error {
if nc.recvConn == nil {
return ErrConnNotEstablished
}
return nc.recvConn.SetReadDeadline(t)
}
func (nc *NetConn) SetWriteDeadline(t time.Time) error {
if nc.sendConn == nil {
return ErrConnNotEstablished
}
return nc.sendConn.SetWriteDeadline(t)
}
func (nc *NetConn) PrintMetrics() {
if nc.recvConn == nil {
return
}
nc.recvConn.PrintMetrics()
}
func (nc *NetConn) String() string {
return fmt.Sprintf("NetConn endpoint %v", nc.LocalAddr())
}
// The following functions are to better stimulating of network exceptions
func (nc *NetConn) PauseRead() {
nc.readMu.Lock()
defer nc.readMu.Unlock()
nc.pauseRead = true
}
func (nc *NetConn) ResumeRead() {
nc.readMu.Lock()
defer nc.readMu.Unlock()
nc.pauseRead = false
}
func (nc *NetConn) PauseWrite() {
nc.writeMu.Lock()
defer nc.writeMu.Unlock()
nc.pauseWrite = true
}
func (nc *NetConn) ResumeWrite() {
nc.writeMu.Lock()
defer nc.writeMu.Unlock()
nc.pauseWrite = false
}