forked from cocaine/cocaine-framework-go
/
asocket.go
227 lines (198 loc) · 4.26 KB
/
asocket.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
package cocaine
import (
"net"
"sync"
"time"
"log"
)
var _ = log.Println
type socketIO interface {
Read() chan rawMessage
Write() chan rawMessage
IsClosed() <-chan struct{}
Close()
}
type socketWriter interface {
Write() chan rawMessage
IsClosed() <-chan struct{}
Close()
}
type asyncBuff struct {
in, out chan rawMessage
stop chan bool
wg sync.WaitGroup
}
func newAsyncBuf() *asyncBuff {
buf := asyncBuff{make(chan rawMessage), make(chan rawMessage), make(chan bool, 1), sync.WaitGroup{}}
buf.loop()
return &buf
}
func (bf *asyncBuff) loop() {
go func() {
bf.wg.Add(1)
defer bf.wg.Done()
var pending []rawMessage // data buffer
var _in chan rawMessage // incoming channel
_in = bf.in
finished := false // flag
for {
var first rawMessage
var _out chan rawMessage
if len(pending) > 0 {
first = pending[0]
_out = bf.out
} else if finished {
break
}
select {
case incoming, ok := <-_in:
if ok {
pending = append(pending, incoming)
} else {
finished = true
_in = nil
}
case _out <- first:
pending[0] = nil
pending = pending[1:]
case <-bf.stop:
close(bf.out) // Notify receiver
return
}
}
}()
}
func (bf *asyncBuff) Stop() (res bool) {
close(bf.stop)
bf.wg.Wait()
return
}
// Biderectional socket
type asyncRWSocket struct {
net.Conn
clientToSock *asyncBuff
socketToClient *asyncBuff
closed chan struct{} //broadcast channel
mutex sync.Mutex
}
func newAsyncRWSocket(family string, address string, timeout time.Duration) (*asyncRWSocket, error) {
conn, err := net.DialTimeout(family, address, timeout)
if err != nil {
return nil, err
}
sock := asyncRWSocket{conn, newAsyncBuf(), newAsyncBuf(), make(chan struct{}), sync.Mutex{}}
sock.readloop()
sock.writeloop()
return &sock, nil
}
func (sock *asyncRWSocket) Close() {
sock.clientToSock.Stop()
sock.socketToClient.Stop()
sock.close()
}
func (sock *asyncRWSocket) close() {
sock.mutex.Lock()
defer sock.mutex.Unlock()
select {
case <-sock.closed: // Already closed
default:
close(sock.closed)
sock.Conn.Close()
}
}
func (sock *asyncRWSocket) IsClosed() (broadcast <-chan struct{}) {
return sock.closed
}
func (sock *asyncRWSocket) Write() chan rawMessage {
return sock.clientToSock.in
}
func (sock *asyncRWSocket) Read() chan rawMessage {
return sock.socketToClient.out
}
func (sock *asyncRWSocket) writeloop() {
go func() {
for incoming := range sock.clientToSock.out {
_, err := sock.Conn.Write(incoming) //Add check for sending full
if err != nil {
sock.close()
return
}
}
}()
}
func (sock *asyncRWSocket) readloop() {
go func() {
buf := make([]byte, 65536)
for {
count, err := sock.Conn.Read(buf)
if err != nil {
close(sock.socketToClient.in)
sock.close()
return
}
bufferToSend := make([]byte, count)
copy(bufferToSend[:], buf[:count])
sock.socketToClient.in <- bufferToSend
}
}()
}
// WriteOnly Socket
type asyncWSocket struct {
net.Conn
clientToSock *asyncBuff
closed chan struct{} //broadcast channel
mutex sync.Mutex
}
func newWSocket(family string, address string, timeout time.Duration) (*asyncWSocket, error) {
conn, err := net.DialTimeout(family, address, timeout)
if err != nil {
return nil, err
}
sock := asyncWSocket{conn, newAsyncBuf(), make(chan struct{}), sync.Mutex{}}
sock.readloop()
sock.writeloop()
return &sock, nil
}
func (sock *asyncWSocket) Write() chan rawMessage {
return sock.clientToSock.in
}
func (sock *asyncWSocket) close() {
sock.mutex.Lock() // Is it really necessary???
defer sock.mutex.Unlock()
select {
case <-sock.closed: // Already closed
default:
close(sock.closed)
sock.Conn.Close()
}
}
func (sock *asyncWSocket) Close() {
sock.close()
sock.clientToSock.Stop()
}
func (sock *asyncWSocket) IsClosed() (broadcast <-chan struct{}) {
return sock.closed
}
func (sock *asyncWSocket) writeloop() {
go func() {
for incoming := range sock.clientToSock.out {
_, err := sock.Conn.Write(incoming) //Add check for sending full
if err != nil {
sock.close()
return
}
}
}()
}
func (sock *asyncWSocket) readloop() {
go func() {
buf := make([]byte, 2048)
for {
_, err := sock.Conn.Read(buf)
if err != nil {
sock.close()
return
}
}
}()
}