-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
147 lines (133 loc) · 2.73 KB
/
channel.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
137
138
139
140
141
142
143
144
145
146
147
package uim
import (
"errors"
"fmt"
"sync"
"time"
"github.com/uim/logger"
)
// ChannelImpl is a websocket implement of channel
type ChannelImpl struct {
sync.Mutex
id string
Conn
writechan chan []byte
once sync.Once
writeWait time.Duration
readwait time.Duration
closed *Event
}
var _ Channel = (*ChannelImpl)(nil)
func NewChannel(id string, conn Conn) Channel {
log := logger.WithFields(logger.Fields{
"module": "tcp_channel",
"id": id,
})
ch := &ChannelImpl{
id: id,
Conn: conn,
writechan: make(chan []byte, 5),
closed: NewEvent(),
writeWait: DefaultWriteWait, //default value
readwait: DefaultReadWait,
}
go func() {
err := ch.writeloop()
if err != nil {
log.Info(err)
}
}()
return ch
}
func (ch *ChannelImpl) writeloop() error {
for {
select {
case payload := <-ch.writechan:
err := ch.WriteFrame(OpBinary, payload)
if err != nil {
return err
}
// Batch write.
chanlen := len(ch.writechan)
for i := 0; i < chanlen; i++ {
payload = <-ch.writechan
err := ch.WriteFrame(OpBinary, payload)
if err != nil {
return err
}
}
err = ch.Flush()
if err != nil {
return err
}
case <-ch.closed.Done():
return nil
}
}
}
func (ch *ChannelImpl) ID() string {
return ch.id
}
func (ch *ChannelImpl) Push(payload []byte) error {
if ch.closed.HasFired() {
return fmt.Errorf("channel %s has closed", ch.id)
}
// Async write.
ch.writechan <- payload
return nil
}
// Close connection.
func (ch *ChannelImpl) Close() error {
ch.once.Do(func() {
close(ch.writechan)
ch.closed.Fire()
})
return nil
}
func (ch *ChannelImpl) SetWriteWait(writeWait time.Duration) {
if writeWait == 0 {
return
}
ch.writeWait = writeWait
}
func (ch *ChannelImpl) SetReadWait(readwait time.Duration) {
if readwait == 0 {
return
}
ch.readwait = readwait
}
func (ch *ChannelImpl) WriteFrame(code OpCode, payload []byte) error {
_ = ch.Conn.SetWriteDeadline(time.Now().Add(ch.writeWait))
return ch.Conn.WriteFrame(code, payload)
}
func (ch *ChannelImpl) Readloop(lst MessageListener) error {
ch.Lock()
defer ch.Unlock()
log := logger.WithFields(logger.Fields{
"struct": "ChannelImpl",
"func": "Readloop",
"id": ch.id,
})
for {
_ = ch.SetReadDeadline(time.Now().Add(ch.readwait))
logger.Info("read loop 0")
frame, err := ch.ReadFrame()
logger.Info("read loop")
if err != nil {
return err
}
if frame.GetOpCode() == OpClose {
return errors.New("remote side close the channel")
}
if frame.GetOpCode() == OpPing {
log.Trace("recv a ping; resp with a pong")
_ = ch.WriteFrame(OpPong, nil)
continue
}
payload := frame.GetPayload()
if len(payload) == 0 {
continue
}
go lst.Receive(ch, payload)
}
}