forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
44 lines (35 loc) · 837 Bytes
/
socket.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
package utp
import (
"errors"
"time"
"github.com/micro/go-micro/transport"
)
func (u *utpSocket) Local() string {
return u.conn.LocalAddr().String()
}
func (u *utpSocket) Remote() string {
return u.conn.RemoteAddr().String()
}
func (u *utpSocket) Recv(m *transport.Message) error {
if m == nil {
return errors.New("message passed in is nil")
}
// set timeout if its greater than 0
if u.timeout > time.Duration(0) {
u.conn.SetDeadline(time.Now().Add(u.timeout))
}
return u.dec.Decode(&m)
}
func (u *utpSocket) Send(m *transport.Message) error {
// set timeout if its greater than 0
if u.timeout > time.Duration(0) {
u.conn.SetDeadline(time.Now().Add(u.timeout))
}
if err := u.enc.Encode(m); err != nil {
return err
}
return u.encBuf.Flush()
}
func (u *utpSocket) Close() error {
return u.conn.Close()
}