forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
223 lines (190 loc) · 5.1 KB
/
conn.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package coap
import (
"bytes"
"log"
"net"
"sync/atomic"
"time"
)
type writeReq interface {
sendResp(err error, timeout time.Duration)
waitResp(timeout time.Duration) error
data() Message
}
type writeReqBase struct {
req Message
respChan chan error // channel must have size 1 for non-blocking write to channel
}
func (wreq *writeReqBase) sendResp(err error, timeout time.Duration) {
select {
case wreq.respChan <- err:
return
default:
log.Fatal("Exactly one error can be send as resp. This is err.")
}
}
func (wreq *writeReqBase) waitResp(timeout time.Duration) error {
select {
case err := <-wreq.respChan:
return err
case <-time.After(timeout):
return ErrTimeout
}
}
func (wreq *writeReqBase) data() Message {
return wreq.req
}
type writeReqTCP struct {
writeReqBase
}
type writeReqUDP struct {
writeReqBase
sessionData *SessionUDPData
}
// Conn represents the connection
type Conn interface {
// LocalAddr get local address of the connection
LocalAddr() net.Addr
// RemoteAddr get peer address of the connection
RemoteAddr() net.Addr
// Close close the connection
Close() error
write(w writeReq, timeout time.Duration) error
}
type connWriter interface {
writeHandler(srv *Server) bool
writeEndHandler(timeout time.Duration) bool
sendFinish(timeout time.Duration)
writeHandlerWithFunc(srv *Server, writeFunc func(srv *Server, wreq writeReq) error) bool
}
type connBase struct {
writeChan chan writeReq
closeChan chan bool
finChan chan bool
closed int32
}
func (conn *connBase) finishWrite() {
if !atomic.CompareAndSwapInt32(&conn.closed, conn.closed, 1) {
return
}
conn.closeChan <- true
<-conn.finChan
}
func (conn *connBase) writeHandlerWithFunc(srv *Server, writeFunc func(srv *Server, wreq writeReq) error) bool {
select {
case wreq := <-conn.writeChan:
wreq.sendResp(writeFunc(srv, wreq), srv.syncTimeout())
return true
case <-conn.closeChan:
return false
}
}
func (conn *connBase) sendFinish(timeout time.Duration) {
select {
case conn.finChan <- true:
case <-time.After(timeout):
log.Fatal("Client cannot recv start: Timeout")
}
}
func (conn *connBase) writeEndHandler(timeout time.Duration) bool {
select {
case wreq := <-conn.writeChan:
wreq.sendResp(ErrConnectionClosed, timeout)
return true
default:
return false
}
}
func (conn *connBase) write(w writeReq, timeout time.Duration) error {
if atomic.LoadInt32(&conn.closed) > 0 {
return ErrConnectionClosed
}
select {
case conn.writeChan <- w:
return w.waitResp(timeout)
case <-time.After(timeout):
return ErrTimeout
}
}
type connTCP struct {
connBase
connection net.Conn // i/o connection if TCP was used
num int32
}
func (conn *connTCP) LocalAddr() net.Addr {
return conn.connection.LocalAddr()
}
func (conn *connTCP) RemoteAddr() net.Addr {
return conn.connection.RemoteAddr()
}
func (conn *connTCP) Close() error {
conn.finishWrite()
return conn.connection.Close()
}
func (conn *connTCP) writeHandler(srv *Server) bool {
return conn.writeHandlerWithFunc(srv, func(srv *Server, wreq writeReq) error {
data := wreq.data()
wr := srv.acquireWriter(conn.connection)
defer srv.releaseWriter(wr)
writeTimeout := srv.writeTimeout()
conn.connection.SetWriteDeadline(time.Now().Add(writeTimeout))
err := data.MarshalBinary(wr)
if err != nil {
return err
}
wr.Flush()
return nil
})
}
type connUDP struct {
connBase
connection *net.UDPConn // i/o connection if UDP was used
}
func (conn *connUDP) LocalAddr() net.Addr {
return conn.connection.LocalAddr()
}
func (conn *connUDP) RemoteAddr() net.Addr {
return conn.connection.RemoteAddr()
}
func (conn *connUDP) SetReadDeadline(timeout time.Time) error {
return conn.connection.SetReadDeadline(timeout)
}
func (conn *connUDP) ReadFromSessionUDP(m []byte) (int, *SessionUDPData, error) {
return ReadFromSessionUDP(conn.connection, m)
}
func (conn *connUDP) Close() error {
conn.finishWrite()
return conn.connection.Close()
}
func (conn *connUDP) writeHandler(srv *Server) bool {
return conn.writeHandlerWithFunc(srv, func(srv *Server, wreq writeReq) error {
data := wreq.data()
wreqUDP := wreq.(*writeReqUDP)
writeTimeout := srv.writeTimeout()
buf := &bytes.Buffer{}
err := data.MarshalBinary(buf)
if err != nil {
return err
}
conn.connection.SetWriteDeadline(time.Now().Add(writeTimeout))
_, err = WriteToSessionUDP(conn.connection, buf.Bytes(), wreqUDP.sessionData)
return err
})
}
func newConnectionTCP(c net.Conn, srv *Server) Conn {
connection := &connTCP{connBase: connBase{writeChan: make(chan writeReq, 10000), closeChan: make(chan bool), finChan: make(chan bool), closed: 0}, connection: c}
go writeToConnection(connection, srv)
return connection
}
func newConnectionUDP(c *net.UDPConn, srv *Server) Conn {
connection := &connUDP{connBase: connBase{writeChan: make(chan writeReq, 10000), closeChan: make(chan bool), finChan: make(chan bool), closed: 0}, connection: c}
go writeToConnection(connection, srv)
return connection
}
func writeToConnection(conn connWriter, srv *Server) {
for conn.writeHandler(srv) {
}
for conn.writeEndHandler(srv.syncTimeout()) {
}
conn.sendFinish(srv.syncTimeout())
}