-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsock_mux.go
127 lines (110 loc) · 2.81 KB
/
websock_mux.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
package main
import (
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
)
var wsMuxMap = make(map[string]*wsMux)
var wsMuxMapMutex = &sync.Mutex{}
type wsMux struct {
addr string
readBuffers map[uint64]chan []byte
bufMux *sync.Mutex
mapMutex *sync.Mutex
}
type wsMuxConn struct {
mux *wsMux
reqNum uint64
}
type wsMuxClientAddr struct{ addr string }
func (wsMuxClientAddr) Network() string { return "ws-mux" }
func (a *wsMuxClientAddr) String() string { return a.addr }
func getWSMux(addr string) *wsMux {
wsMuxMapMutex.Lock()
defer wsMuxMapMutex.Unlock()
if ret, have := wsMuxMap[addr]; have {
return ret
} else {
wsMuxMap[addr] = &wsMux{
addr: addr,
readBuffers: make(map[uint64]chan []byte),
bufMux: &sync.Mutex{},
mapMutex: &sync.Mutex{},
}
return wsMuxMap[addr]
}
}
func (m *wsMux) NewConn(r *http.Request) *wsMuxConn {
m.bufMux.Lock()
defer m.bufMux.Unlock()
ret := &wsMuxConn{mux: m, reqNum: uint64(r.Context().Value(requestNumberContext).(int))}
m.mapMutex.Lock()
m.readBuffers[ret.reqNum] = make(chan []byte, 1)
m.mapMutex.Unlock()
return ret
}
func (m *wsMuxConn) Close() error {
m.mux.bufMux.Lock()
defer m.mux.bufMux.Unlock()
m.mux.mapMutex.Lock()
defer m.mux.mapMutex.Unlock()
close(m.mux.readBuffers[m.reqNum])
delete(m.mux.readBuffers, m.reqNum)
return nil
}
func (m *wsMuxConn) LocalAddr() net.Addr {
return &wsMuxClientAddr{m.mux.addr}
}
func (m *wsMuxConn) RemoteAddr() net.Addr {
return &wsMuxClientAddr{fmt.Sprintf("%s[%d]", m.mux.addr, m.reqNum)}
}
func (m *wsMux) getDataChannel(reqNum uint64) chan []byte {
m.mapMutex.Lock()
defer m.mapMutex.Unlock()
return m.readBuffers[reqNum]
}
func (m *wsMux) getDataChannelsExcept(excludedReqNum uint64) (chanList []chan []byte) {
m.mapMutex.Lock()
defer m.mapMutex.Unlock()
for reqNum := range m.readBuffers {
if reqNum != excludedReqNum {
chanList = append(chanList, m.readBuffers[reqNum])
}
}
return chanList
}
func (m *wsMuxConn) ReadNext() (buf []byte, err error) {
d := <-m.mux.getDataChannel(m.reqNum)
if d == nil {
return nil, io.EOF
}
return d, nil
}
func (m *wsMuxConn) Read(b []byte) (n int, err error) {
dataChannel := m.mux.getDataChannel(m.reqNum)
d := <-dataChannel
if d == nil {
return 0, io.EOF
}
m.mux.bufMux.Lock()
defer m.mux.bufMux.Unlock()
n = copy(b, d)
if n < len(d) {
dataChannel <- d[n:]
}
return n, err
}
func (m *wsMuxConn) Write(b []byte) (n int, err error) {
m.mux.bufMux.Lock()
defer m.mux.bufMux.Unlock()
for _, dest := range m.mux.getDataChannelsExcept(m.reqNum) {
dest <- b
}
return len(b), nil
}
func (m *wsMuxConn) SetDeadline(time.Time) error { return ErrNotImplemented }
func (m *wsMuxConn) SetReadDeadline(time.Time) error { return ErrNotImplemented }
func (m *wsMuxConn) SetWriteDeadline(time.Time) error { return ErrNotImplemented }