@@ -15,18 +15,18 @@ import (
15
15
"time"
16
16
)
17
17
18
- // TCP connection object.
18
+ // Conn is the TCP connection object.
19
19
type Conn struct {
20
- net.Conn // Underlying TCP connection object.
21
- reader * bufio.Reader // Buffer reader for connection.
22
- recvDeadline time.Time // Timeout point for reading.
23
- sendDeadline time.Time // Timeout point for writing.
24
- recvBufferWait time.Duration // Interval duration for reading buffer.
20
+ net.Conn // Underlying TCP connection object.
21
+ reader * bufio.Reader // Buffer reader for connection.
22
+ receiveDeadline time.Time // Timeout point for reading.
23
+ sendDeadline time.Time // Timeout point for writing.
24
+ receiveBufferWait time.Duration // Interval duration for reading buffer.
25
25
}
26
26
27
27
const (
28
28
// Default interval for reading buffer.
29
- gRECV_ALL_WAIT_TIMEOUT = time .Millisecond
29
+ receiveAllWaitTimeout = time .Millisecond
30
30
)
31
31
32
32
// NewConn creates and returns a new connection with given address.
@@ -61,11 +61,11 @@ func NewConnKeyCrt(addr, crtFile, keyFile string) (*Conn, error) {
61
61
// NewConnByNetConn creates and returns a TCP connection object with given net.Conn object.
62
62
func NewConnByNetConn (conn net.Conn ) * Conn {
63
63
return & Conn {
64
- Conn : conn ,
65
- reader : bufio .NewReader (conn ),
66
- recvDeadline : time.Time {},
67
- sendDeadline : time.Time {},
68
- recvBufferWait : gRECV_ALL_WAIT_TIMEOUT ,
64
+ Conn : conn ,
65
+ reader : bufio .NewReader (conn ),
66
+ receiveDeadline : time.Time {},
67
+ sendDeadline : time.Time {},
68
+ receiveBufferWait : receiveAllWaitTimeout ,
69
69
}
70
70
}
71
71
@@ -84,7 +84,7 @@ func (c *Conn) Send(data []byte, retry ...Retry) error {
84
84
if len (retry ) > 0 {
85
85
retry [0 ].Count --
86
86
if retry [0 ].Interval == 0 {
87
- retry [0 ].Interval = gDEFAULT_RETRY_INTERVAL
87
+ retry [0 ].Interval = defaultRetryInternal
88
88
}
89
89
time .Sleep (retry [0 ].Interval )
90
90
}
@@ -113,13 +113,13 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
113
113
if length > 0 {
114
114
buffer = make ([]byte , length )
115
115
} else {
116
- buffer = make ([]byte , gDEFAULT_READ_BUFFER_SIZE )
116
+ buffer = make ([]byte , defaultReadBufferSize )
117
117
}
118
118
119
119
for {
120
120
if length < 0 && index > 0 {
121
121
bufferWait = true
122
- if err = c .SetReadDeadline (time .Now ().Add (c .recvBufferWait )); err != nil {
122
+ if err = c .SetReadDeadline (time .Now ().Add (c .receiveBufferWait )); err != nil {
123
123
return nil , err
124
124
}
125
125
}
@@ -132,9 +132,9 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
132
132
break
133
133
}
134
134
} else {
135
- if index >= gDEFAULT_READ_BUFFER_SIZE {
135
+ if index >= defaultReadBufferSize {
136
136
// If it exceeds the buffer size, it then automatically increases its buffer size.
137
- buffer = append (buffer , make ([]byte , gDEFAULT_READ_BUFFER_SIZE )... )
137
+ buffer = append (buffer , make ([]byte , defaultReadBufferSize )... )
138
138
} else {
139
139
// It returns immediately if received size is lesser than buffer size.
140
140
if ! bufferWait {
@@ -150,7 +150,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
150
150
}
151
151
// Re-set the timeout when reading data.
152
152
if bufferWait && isTimeout (err ) {
153
- if err = c .SetReadDeadline (c .recvDeadline ); err != nil {
153
+ if err = c .SetReadDeadline (c .receiveDeadline ); err != nil {
154
154
return nil , err
155
155
}
156
156
err = nil
@@ -163,7 +163,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
163
163
}
164
164
retry [0 ].Count --
165
165
if retry [0 ].Interval == 0 {
166
- retry [0 ].Interval = gDEFAULT_RETRY_INTERVAL
166
+ retry [0 ].Interval = defaultRetryInternal
167
167
}
168
168
time .Sleep (retry [0 ].Interval )
169
169
continue
@@ -230,10 +230,10 @@ func (c *Conn) RecvTil(til []byte, retry ...Retry) ([]byte, error) {
230
230
231
231
// RecvWithTimeout reads data from the connection with timeout.
232
232
func (c * Conn ) RecvWithTimeout (length int , timeout time.Duration , retry ... Retry ) (data []byte , err error ) {
233
- if err := c .SetRecvDeadline (time .Now ().Add (timeout )); err != nil {
233
+ if err := c .SetreceiveDeadline (time .Now ().Add (timeout )); err != nil {
234
234
return nil , err
235
235
}
236
- defer c .SetRecvDeadline (time.Time {})
236
+ defer c .SetreceiveDeadline (time.Time {})
237
237
data , err = c .Recv (length , retry ... )
238
238
return
239
239
}
@@ -269,16 +269,16 @@ func (c *Conn) SendRecvWithTimeout(data []byte, length int, timeout time.Duratio
269
269
func (c * Conn ) SetDeadline (t time.Time ) error {
270
270
err := c .Conn .SetDeadline (t )
271
271
if err == nil {
272
- c .recvDeadline = t
272
+ c .receiveDeadline = t
273
273
c .sendDeadline = t
274
274
}
275
275
return err
276
276
}
277
277
278
- func (c * Conn ) SetRecvDeadline (t time.Time ) error {
278
+ func (c * Conn ) SetreceiveDeadline (t time.Time ) error {
279
279
err := c .SetReadDeadline (t )
280
280
if err == nil {
281
- c .recvDeadline = t
281
+ c .receiveDeadline = t
282
282
}
283
283
return err
284
284
}
@@ -291,8 +291,8 @@ func (c *Conn) SetSendDeadline(t time.Time) error {
291
291
return err
292
292
}
293
293
294
- // SetRecvBufferWait sets the buffer waiting timeout when reading all data from connection.
294
+ // SetreceiveBufferWait sets the buffer waiting timeout when reading all data from connection.
295
295
// The waiting duration cannot be too long which might delay receiving data from remote address.
296
- func (c * Conn ) SetRecvBufferWait (bufferWaitDuration time.Duration ) {
297
- c .recvBufferWait = bufferWaitDuration
296
+ func (c * Conn ) SetreceiveBufferWait (bufferWaitDuration time.Duration ) {
297
+ c .receiveBufferWait = bufferWaitDuration
298
298
}
0 commit comments