-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconn.go
136 lines (116 loc) · 2.71 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
/**
* connection wrapper
*/
package tok
import (
"errors"
"log"
"sync"
"time"
)
var (
// ReadTimeout read timeout duration
ReadTimeout time.Duration
// WriteTimeout write timeout duration
WriteTimeout = time.Minute
// AuthTimeout auth timeout duration
AuthTimeout = time.Second * 5
// ServerPingInterval server ping interval duration
ServerPingInterval = time.Second * 30
)
// abstract connection,
type connection struct {
sync.RWMutex
wLock sync.Mutex
dv *Device
adapter conAdapter
hub *Hub
closed bool
}
type conState struct {
con *connection
online bool
}
func (conn *connection) ShareConn(other *connection) bool {
return conn.adapter.ShareConn(other.adapter)
}
// conAdapter is adapter for real connection.
// For now, net.Conn and websocket.Conn are supported.
// This interface is useful for building test application
type conAdapter interface {
Read() ([]byte, error) // Read payload data from real connection. Unpack from basic data frame
Write([]byte) error // Write payload data to real connection. Pack into basic data frame
Close() // Close the real connection
ShareConn(adapter conAdapter) bool // if two adapters share one net connection (tcp/ws)
}
func (conn *connection) uid() interface{} {
return conn.dv.UID()
}
func (conn *connection) isClosed() bool {
conn.RLock()
defer conn.RUnlock()
return conn.closed
}
func (conn *connection) readLoop() {
for {
if conn.isClosed() {
return
}
b, err := conn.adapter.Read()
if err != nil {
// log.Println("read err", err)
conn.hub.stateChange(conn, false)
return
}
conn.hub.receive(conn.dv, b)
}
}
func (conn *connection) close() {
conn.Lock()
defer conn.Unlock()
conn.closed = true
conn.adapter.Close()
}
func (conn *connection) Write(b []byte) error {
conn.wLock.Lock()
defer conn.wLock.Unlock()
if conn.isClosed() {
return errors.New("Can't write to closed connection")
}
if err := conn.adapter.Write(b); err != nil {
conn.hub.stateChange(conn, false)
return err
}
return nil
}
func initConnection(dv *Device, adapter conAdapter, hub *Hub) {
conn := &connection{
dv: dv,
adapter: adapter,
hub: hub,
}
hub.stateChange(conn, true)
// start server ping loop if necessary
if hub.actor.Ping() != nil {
ticker := time.NewTicker(ServerPingInterval)
go func() {
for range ticker.C {
if conn.isClosed() {
ticker.Stop()
return
}
b, err := hub.actor.BeforeSend(dv, hub.actor.Ping())
if err == nil {
if b == nil {
b = hub.actor.Ping()
}
if err := conn.Write(b); err != nil {
log.Println("[tok] write ping error", err)
}
}
}
}()
}
// block on read
conn.readLoop()
}