-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketbase.go
77 lines (66 loc) · 1.66 KB
/
socketbase.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
package netgo
import (
"net"
"sync"
"sync/atomic"
"time"
)
// base of kcpSocket/tcpSocket/unixSocket
type socketBase struct {
userData atomic.Value
packetReceiver PacketReceiver
conn net.Conn
closeOnce sync.Once
}
func (base *socketBase) init(conn net.Conn, packetReceiver ...PacketReceiver) {
base.conn = conn
if len(packetReceiver) == 0 || packetReceiver[0] == nil {
base.packetReceiver = &defaultPacketReceiver{}
} else {
base.packetReceiver = packetReceiver[0]
}
}
func (base *socketBase) GetUnderConn() interface{} {
return base.conn
}
func (base *socketBase) Close() {
base.closeOnce.Do(func() {
base.conn.Close()
})
}
func (base *socketBase) LocalAddr() net.Addr {
return base.conn.LocalAddr()
}
func (base *socketBase) RemoteAddr() net.Addr {
return base.conn.RemoteAddr()
}
func (base *socketBase) SetUserData(ud interface{}) {
base.userData.Store(userdata{
data: ud,
})
}
func (base *socketBase) GetUserData() interface{} {
if ud, ok := base.userData.Load().(userdata); ok {
return ud.data
} else {
return nil
}
}
func (base *socketBase) Send(data []byte, deadline ...time.Time) (int, error) {
d := time.Time{}
if len(deadline) > 0 && !deadline[0].IsZero() {
d = deadline[0]
}
if err := base.conn.SetWriteDeadline(d); err != nil {
return 0, err
} else {
return base.conn.Write(data)
}
}
func (base *socketBase) Recv(deadline ...time.Time) ([]byte, error) {
if len(deadline) > 0 && !deadline[0].IsZero() {
return base.packetReceiver.Recv(base.conn, deadline[0])
} else {
return base.packetReceiver.Recv(base.conn, time.Time{})
}
}