-
Notifications
You must be signed in to change notification settings - Fork 33
/
channel.go
239 lines (204 loc) · 5.98 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Glue - Robust Go and Javascript Socket Library
* Copyright (C) 2015 Roland Singer <roland.singer[at]desertbit.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package glue
import (
"fmt"
"runtime/debug"
"sync"
"time"
"github.com/desertbit/glue/log"
"github.com/desertbit/glue/utils"
)
//#################//
//### Constants ###//
//#################//
const (
// The channel buffer size for received data.
readChanBuffer = 7
)
//####################//
//### Channel type ###//
//####################//
// A Channel is a separate communication channel.
type Channel struct {
s *Socket
readHandler *handler
name string
readChan chan string
}
func newChannel(s *Socket, name string) *Channel {
return &Channel{
s: s,
readHandler: newHandler(),
name: name,
readChan: make(chan string, readChanBuffer),
}
}
// Socket returns the channel's socket.
func (c *Channel) Socket() *Socket {
return c.s
}
// Write data to the channel.
func (c *Channel) Write(data string) {
// Prepend the socket command and send the channel name and data.
c.s.write(cmdChannelData + utils.MarshalValues(c.name, data))
}
// Read the next message from the channel. This method is blocking.
// One variadic argument sets a timeout duration.
// If no timeout is specified, this method will block forever.
// ErrSocketClosed is returned, if the socket connection is closed.
// ErrReadTimeout is returned, if the timeout is reached.
func (c *Channel) Read(timeout ...time.Duration) (string, error) {
timeoutChan := make(chan (struct{}))
// Create a timeout timer if a timeout is specified.
if len(timeout) > 0 && timeout[0] > 0 {
timer := time.AfterFunc(timeout[0], func() {
// Trigger the timeout by closing the channel.
close(timeoutChan)
})
// Always stop the timer on defer.
defer timer.Stop()
}
select {
case data := <-c.readChan:
return data, nil
case <-c.s.isClosedChan:
// The connection was closed.
// Return an error.
return "", ErrSocketClosed
case <-timeoutChan:
// The timeout was reached.
// Return an error.
return "", ErrReadTimeout
}
}
// OnRead sets the function which is triggered if new data is received on the channel.
// If this event function based method of reading data from the socket is used,
// then don't use the socket Read method.
// Either use the OnRead or the Read approach.
func (c *Channel) OnRead(f OnReadFunc) {
// Create a new read handler for this channel.
// Previous handlers are stopped first.
handlerStopped := c.readHandler.New()
// Start the handler goroutine.
go func() {
for {
select {
case data := <-c.readChan:
// Call the callback in a new goroutine.
go func() {
// Recover panics and log the error.
defer func() {
if e := recover(); e != nil {
log.L.Errorf("glue: panic while calling onRead function: %v\n%s", e, debug.Stack())
}
}()
// Trigger the on read event function.
f(data)
}()
case <-c.s.isClosedChan:
// Release this goroutine if the socket is closed.
return
case <-handlerStopped:
// Release this goroutine.
return
}
}
}()
}
// DiscardRead ignores and discars the data received from this channel.
// Call this method during initialization, if you don't read any data from
// this channel. If received data is not discarded, then the read buffer will block as soon
// as it is full, which will also block the keep-alive mechanism of the socket. The result
// would be a closed socket...
func (c *Channel) DiscardRead() {
// Create a new read handler for this channel.
// Previous handlers are stopped first.
handlerStopped := c.readHandler.New()
// Start the handler goroutine.
go func() {
for {
select {
case <-c.readChan:
// Don't do anything.
// Just discard the data.
case <-c.s.isClosedChan:
// Release this goroutine if the socket is closed.
return
case <-handlerStopped:
// Release this goroutine.
return
}
}
}()
}
func (c *Channel) triggerRead(data string) {
// Send the data to the read channel.
c.readChan <- data
}
//#####################//
//### Channels type ###//
//#####################//
type channels struct {
m map[string]*Channel
mutex sync.Mutex
}
func newChannels() *channels {
return &channels{
m: make(map[string]*Channel),
}
}
func (cs *channels) get(name string) *Channel {
// Lock the mutex.
cs.mutex.Lock()
defer cs.mutex.Unlock()
return cs.m[name]
}
func (cs *channels) triggerReadForChannel(name, data string) error {
// Get the channel.
c := cs.get(name)
if c == nil {
return fmt.Errorf("received data for channel '%s': channel does not exists", name)
}
// Trigger the read.
c.triggerRead(data)
return nil
}
//#################################//
//### Additional Socket Methods ###//
//#################################//
// Channel returns the corresponding channel value specified by the name.
// If no channel value exists for the given name, a new channel is created.
// Multiple calls to Channel with the same name, will always return the same
// channel value pointer.
func (s *Socket) Channel(name string) *Channel {
// Get the socket channel pointer.
cs := s.channels
// Lock the mutex.
cs.mutex.Lock()
defer cs.mutex.Unlock()
// Get the channel if it exists.
c, ok := cs.m[name]
if ok {
return c
}
// Create and add the new channel to the socket channels map.
c = newChannel(s, name)
cs.m[name] = c
return c
}