-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.go
152 lines (134 loc) · 3.57 KB
/
socket.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
package midware
import (
"net/http"
"reflect"
"github.com/adamcolton/luce/ds/bus/iobus"
"github.com/adamcolton/luce/lerr"
"github.com/adamcolton/luce/lhttp"
"github.com/adamcolton/luce/util/filter"
"github.com/adamcolton/luce/util/linject"
"github.com/adamcolton/luce/util/reflector"
"github.com/gorilla/websocket"
)
// WebSocket can perform two different midware operations. It can attach a
// *websocket.Conn directly or it can attach a set of []byte channels connected
// to a websocket.
type WebSocket struct {
Upgrader websocket.Upgrader
ToBuf, FromBuf int
lhttp.ErrHandler
}
// // NewWebSocket with default buffer sizes.
func NewWebSocket() WebSocket {
return WebSocket{
Upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
}
}
// // ChannelInitilizer creates an Initilizer that will abstract the websocket to
// // a pair of []byte channels.
func (ws WebSocket) Initilizer(to, from, conn string) Initilizer {
return webSocketInitilizer{
WebSocket: ws,
to: to,
from: from,
conn: conn,
}
}
type webSocketInitilizer struct {
WebSocket
to, from, conn string
}
var (
inChanType = reflector.Type[chan<- []byte]()
checkSend = filter.IsType(inChanType).
Check(filter.TypeErr("Invalid Websocket 'To' type: %s"))
outChanType = reflector.Type[<-chan []byte]()
checkRecv = filter.IsType(outChanType).
Check(filter.TypeErr("Invalid Websocket 'From' type: %s"))
connType = reflector.Type[*websocket.Conn]()
checkConn = filter.IsType(connType).
Check(filter.TypeErr("Invalid Websocket 'Conn' type: %s"))
)
// Initilize checks the "to" and "from" fields to validate the names and types.
// It will panic if they are not.
func (i webSocketInitilizer) Initilize(fieldType reflect.Type) Injector {
var hasToField, hasFromField, hasConnField bool
var toField, fromField, connField reflect.StructField
if linject.FieldName(i.to) {
toField, hasToField = fieldType.FieldByName(i.to)
}
if linject.FieldName(i.from) {
fromField, hasFromField = fieldType.FieldByName(i.from)
}
if linject.FieldName(i.conn) {
connField, hasConnField = fieldType.FieldByName(i.conn)
}
if !hasToField && !hasFromField && !hasConnField {
return nil
}
di := webSocketDataInserter{
WebSocket: i.WebSocket,
}
if hasToField {
lerr.Must(checkSend(toField.Type))
di.to = toField.Index
}
if hasFromField {
lerr.Must(checkRecv(fromField.Type))
di.from = fromField.Index
}
if hasConnField {
lerr.Must(checkConn(connField.Type))
di.conn = connField.Index
}
return di
}
type webSocketDataInserter struct {
WebSocket
to, from, conn []int
}
// Insert sets the "to" and "from" channels on the data field.
func (di webSocketDataInserter) Inject(w http.ResponseWriter, r *http.Request, data reflect.Value) (func([]reflect.Value), error) {
fbi := data.Elem().FieldByIndex
set := func(idx []int, v any) {
fbi(idx).Set(reflect.ValueOf(v))
}
conn, err := di.Upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, err
}
if di.conn != nil {
set(di.conn, conn)
}
var to chan []byte
if di.to != nil || di.from != nil {
sw := lhttp.NewSocket(conn)
if di.to != nil {
to = make(chan []byte, di.ToBuf)
set(di.to, to)
go iobus.Writer(sw, to, nil)
}
if di.from != nil {
from := make(chan []byte, di.FromBuf)
set(di.from, from)
stop := false
go (iobus.Config{
CloseOnEOF: true,
}).Reader(sw, from, nil, &stop)
}
}
callbackClose := func(rets []reflect.Value) {
conn.Close()
defer func() {
// ignore double close error
recover()
}()
if to != nil {
close(to)
}
}
return callbackClose, nil
}