@@ -14,85 +14,33 @@ import (
14
14
"github.com/gogf/gf/v2/errors/gerror"
15
15
)
16
16
17
- // Conn handles the UDP connection.
18
- type Conn struct {
19
- * net.UDPConn // Underlying UDP connection.
20
- remoteAddr * net.UDPAddr // Remote address.
21
- deadlineRecv time.Time // Timeout point for reading data.
22
- deadlineSend time.Time // Timeout point for writing data.
23
- bufferWaitRecv time.Duration // Interval duration for reading buffer.
17
+ // localConn provides common operations for udp connection.
18
+ type localConn struct {
19
+ * net.UDPConn // Underlying UDP connection.
20
+ deadlineRecv time.Time // Timeout point for reading data.
21
+ deadlineSend time.Time // Timeout point for writing data.
24
22
}
25
23
26
24
const (
27
25
defaultRetryInterval = 100 * time .Millisecond // Retry interval.
28
26
defaultReadBufferSize = 1024 // (Byte)Buffer size.
29
- receiveAllWaitTimeout = time .Millisecond // Default interval for reading buffer.
30
27
)
31
28
29
+ // Retry holds the retry options.
30
+ // TODO replace with standalone retry package.
32
31
type Retry struct {
33
32
Count int // Max retry count.
34
33
Interval time.Duration // Retry interval.
35
34
}
36
35
37
- // NewConn creates UDP connection to `remoteAddress`.
38
- // The optional parameter `localAddress` specifies the local address for connection.
39
- func NewConn (remoteAddress string , localAddress ... string ) (* Conn , error ) {
40
- if conn , err := NewNetConn (remoteAddress , localAddress ... ); err == nil {
41
- return NewConnByNetConn (conn ), nil
42
- } else {
43
- return nil , err
44
- }
45
- }
46
-
47
- // NewConnByNetConn creates an UDP connection object with given *net.UDPConn object.
48
- func NewConnByNetConn (udp * net.UDPConn ) * Conn {
49
- return & Conn {
50
- UDPConn : udp ,
51
- deadlineRecv : time.Time {},
52
- deadlineSend : time.Time {},
53
- bufferWaitRecv : receiveAllWaitTimeout ,
54
- }
55
- }
56
-
57
- // Send writes data to remote address.
58
- func (c * Conn ) Send (data []byte , retry ... Retry ) (err error ) {
59
- for {
60
- if c .remoteAddr != nil {
61
- _ , err = c .WriteToUDP (data , c .remoteAddr )
62
- } else {
63
- _ , err = c .Write (data )
64
- }
65
- if err != nil {
66
- // Connection closed.
67
- if err == io .EOF {
68
- return err
69
- }
70
- // Still failed even after retrying.
71
- if len (retry ) == 0 || retry [0 ].Count == 0 {
72
- err = gerror .Wrap (err , `Write data failed` )
73
- return err
74
- }
75
- if len (retry ) > 0 {
76
- retry [0 ].Count --
77
- if retry [0 ].Interval == 0 {
78
- retry [0 ].Interval = defaultRetryInterval
79
- }
80
- time .Sleep (retry [0 ].Interval )
81
- }
82
- } else {
83
- return nil
84
- }
85
- }
86
- }
87
-
88
36
// Recv receives and returns data from remote address.
89
- // The parameter `buffer` is used for customizing the receiving buffer size. If `buffer` <= 0,
90
- // it uses the default buffer size, which is 1024 byte.
37
+ // The parameter `buffer` is used for customizing the receiving buffer size.
38
+ // If `buffer` <= 0, it uses the default buffer size, which is 1024 byte.
91
39
//
92
40
// There's package border in UDP protocol, we can receive a complete package if specified
93
41
// buffer size is big enough. VERY NOTE that we should receive the complete package in once
94
42
// or else the leftover package data would be dropped.
95
- func (c * Conn ) Recv (buffer int , retry ... Retry ) ([]byte , error ) {
43
+ func (c * localConn ) Recv (buffer int , retry ... Retry ) ([]byte , * net. UDPAddr , error ) {
96
44
var (
97
45
err error // Reading error
98
46
size int // Reading size
@@ -106,9 +54,6 @@ func (c *Conn) Recv(buffer int, retry ...Retry) ([]byte, error) {
106
54
}
107
55
for {
108
56
size , remoteAddr , err = c .ReadFromUDP (data )
109
- if err == nil {
110
- c .remoteAddr = remoteAddr
111
- }
112
57
if err != nil {
113
58
// Connection closed.
114
59
if err == io .EOF {
@@ -131,51 +76,11 @@ func (c *Conn) Recv(buffer int, retry ...Retry) ([]byte, error) {
131
76
}
132
77
break
133
78
}
134
- return data [:size ], err
135
- }
136
-
137
- // SendRecv writes data to connection and blocks reading response.
138
- func (c * Conn ) SendRecv (data []byte , receive int , retry ... Retry ) ([]byte , error ) {
139
- if err := c .Send (data , retry ... ); err != nil {
140
- return nil , err
141
- }
142
- return c .Recv (receive , retry ... )
143
- }
144
-
145
- // RecvWithTimeout reads data from remote address with timeout.
146
- func (c * Conn ) RecvWithTimeout (length int , timeout time.Duration , retry ... Retry ) (data []byte , err error ) {
147
- if err = c .SetDeadlineRecv (time .Now ().Add (timeout )); err != nil {
148
- return nil , err
149
- }
150
- defer func () {
151
- _ = c .SetDeadlineRecv (time.Time {})
152
- }()
153
- data , err = c .Recv (length , retry ... )
154
- return
155
- }
156
-
157
- // SendWithTimeout writes data to connection with timeout.
158
- func (c * Conn ) SendWithTimeout (data []byte , timeout time.Duration , retry ... Retry ) (err error ) {
159
- if err = c .SetDeadlineSend (time .Now ().Add (timeout )); err != nil {
160
- return err
161
- }
162
- defer func () {
163
- _ = c .SetDeadlineSend (time.Time {})
164
- }()
165
- err = c .Send (data , retry ... )
166
- return
167
- }
168
-
169
- // SendRecvWithTimeout writes data to connection and reads response with timeout.
170
- func (c * Conn ) SendRecvWithTimeout (data []byte , receive int , timeout time.Duration , retry ... Retry ) ([]byte , error ) {
171
- if err := c .Send (data , retry ... ); err != nil {
172
- return nil , err
173
- }
174
- return c .RecvWithTimeout (receive , timeout , retry ... )
79
+ return data [:size ], remoteAddr , err
175
80
}
176
81
177
82
// SetDeadline sets the read and write deadlines associated with the connection.
178
- func (c * Conn ) SetDeadline (t time.Time ) (err error ) {
83
+ func (c * localConn ) SetDeadline (t time.Time ) (err error ) {
179
84
if err = c .UDPConn .SetDeadline (t ); err == nil {
180
85
c .deadlineRecv = t
181
86
c .deadlineSend = t
@@ -186,7 +91,7 @@ func (c *Conn) SetDeadline(t time.Time) (err error) {
186
91
}
187
92
188
93
// SetDeadlineRecv sets the read deadline associated with the connection.
189
- func (c * Conn ) SetDeadlineRecv (t time.Time ) (err error ) {
94
+ func (c * localConn ) SetDeadlineRecv (t time.Time ) (err error ) {
190
95
if err = c .SetReadDeadline (t ); err == nil {
191
96
c .deadlineRecv = t
192
97
} else {
@@ -196,23 +101,11 @@ func (c *Conn) SetDeadlineRecv(t time.Time) (err error) {
196
101
}
197
102
198
103
// SetDeadlineSend sets the deadline of sending for current connection.
199
- func (c * Conn ) SetDeadlineSend (t time.Time ) (err error ) {
104
+ func (c * localConn ) SetDeadlineSend (t time.Time ) (err error ) {
200
105
if err = c .SetWriteDeadline (t ); err == nil {
201
106
c .deadlineSend = t
202
107
} else {
203
108
err = gerror .Wrapf (err , `SetDeadlineSend for connection failed with "%s"` , t )
204
109
}
205
110
return err
206
111
}
207
-
208
- // SetBufferWaitRecv sets the buffer waiting timeout when reading all data from connection.
209
- // The waiting duration cannot be too long which might delay receiving data from remote address.
210
- func (c * Conn ) SetBufferWaitRecv (d time.Duration ) {
211
- c .bufferWaitRecv = d
212
- }
213
-
214
- // RemoteAddr returns the remote address of current UDP connection.
215
- // Note that it cannot use c.conn.RemoteAddr() as it is nil.
216
- func (c * Conn ) RemoteAddr () net.Addr {
217
- return c .remoteAddr
218
- }
0 commit comments