/
tcp.go
171 lines (150 loc) · 4.49 KB
/
tcp.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
160
161
162
163
164
165
166
167
168
169
170
171
package channel
import (
"io"
"net"
"net/url"
"time"
)
func init() {
RegisterURLHandler("tcp", NewTCPChannel)
RegisterURLHandler("udp", NewUDPChannel)
}
// A TCP channel is a channel over a simple TCP connection with just one peer.
type SocketChannel struct {
peerAddr string
conn net.Conn
timeout time.Duration
proto string
manualTimeout bool
}
// Create a new TCP channel for connecting to "dest".
// If "timeout" is greater than 0, set deadlines on all operations to
// the current time plus "timeout".
func NewTCPChannel(dest *url.URL, timeout time.Duration) (ch Channel, err error) {
return NewSocketChannel("tcp", dest.Host, timeout)
}
// Create a new UDP channel for connecting to "dest".
// If "timeout" is greater than 0, set deadlines on all operations to
// the current time plus "timeout".
func NewUDPChannel(dest *url.URL, timeout time.Duration) (ch Channel, err error) {
return NewSocketChannel("udp", dest.Host, timeout)
}
// Encapsulate the specified socket in a channel.
func NewChannelFromSocket(connection net.Conn, timeout time.Duration) Channel {
return &SocketChannel{
peerAddr: connection.RemoteAddr().String(),
conn: connection,
timeout: timeout,
proto: connection.RemoteAddr().Network(),
manualTimeout: false,
}
}
// Create a new socket channel for connecting to "dest".
// If "timeout" is greater than 0, set deadlines on all operations to
// the current time plus "timeout".
func NewSocketChannel(proto, dest string, timeout time.Duration) (ch Channel, err error) {
var conn net.Conn
if timeout > 0 {
conn, err = net.DialTimeout(proto, dest, timeout)
} else {
conn, err = net.Dial(proto, dest)
}
if err != nil {
return
}
return &SocketChannel{
peerAddr: dest,
conn: conn,
timeout: timeout,
proto: proto,
manualTimeout: false,
}, nil
}
// Socket channels are connected as soon as the constructor returns.
func (*SocketChannel) WaitForNonEmpty(unused time.Duration) error {
return nil
}
// On single-destination Socket connections, switching channels is a no-op.
func (*SocketChannel) NextBackend() {
}
// Closing a Socket channel closes the underlying Socket socket.
func (t *SocketChannel) Close() error {
return t.conn.Close()
}
// Local address is whatever the connection feels like.
func (t *SocketChannel) LocalAddr() net.Addr {
return t.conn.LocalAddr()
}
// Remote address is whatever the connection thinks it is.
func (t *SocketChannel) RemoteAddr() net.Addr {
return t.conn.RemoteAddr()
}
// Set the deadline for reading and writing and stop automated
// deadline management.
func (s *SocketChannel) SetDeadline(t time.Time) error {
s.manualTimeout = true
return s.conn.SetDeadline(t)
}
// Set the deadline for reading and stop automated deadline management.
func (s *SocketChannel) SetReadDeadline(t time.Time) error {
s.manualTimeout = true
return s.conn.SetReadDeadline(t)
}
// Set the deadline for writing and stop automated deadline management.
func (s *SocketChannel) SetWriteDeadline(t time.Time) error {
s.manualTimeout = true
return s.conn.SetWriteDeadline(t)
}
// Reading from a Socket channel is basically just reading from the
// underlying Socket connection.
func (t *SocketChannel) Read(p []byte) (n int, err error) {
if t.timeout > 0 {
var deadline time.Time = time.Now().Add(t.timeout)
t.conn.SetReadDeadline(deadline)
}
return t.conn.Read(p)
}
// Sockets only have a single backend, so the largest subset of
// channels making up this channel is this channel itself.
func (t *SocketChannel) GetAllSubchannels() []Channel {
return []Channel{t}
}
// Sockets only have a single backend.
func (s *SocketChannel) NumBackends() uint64 {
return 1
}
// Writing to a Socket channel is slightly more involved than reading from
// it. If the channel was closed, it must be reestablished.
func (t *SocketChannel) Write(p []byte) (n int, err error) {
var deadline time.Time
if t.timeout > 0 {
deadline = time.Now().Add(t.timeout)
}
for {
if deadline.UnixNano() > 0 {
t.conn.SetWriteDeadline(deadline)
}
n, err = t.conn.Write(p)
if err == nil {
return
} else {
if err == io.ErrClosedPipe {
t.conn.Close()
// Don't try to wait beyond the deadline, that's pointless.
if time.Now().After(deadline) {
return
}
if t.timeout > 0 {
t.conn, err = net.DialTimeout(t.proto, t.peerAddr, t.timeout)
} else {
t.conn, err = net.Dial(t.proto, t.peerAddr)
}
if err != nil {
return
}
} else {
return
}
}
}
}