-
Notifications
You must be signed in to change notification settings - Fork 4
/
nexus.go
192 lines (164 loc) · 3.78 KB
/
nexus.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
package nexus
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync"
"github.com/pkg/errors"
"strings"
"strconv"
"github.com/gorilla/websocket"
)
type PacketFormat int
const (
PacketFormatJSON = iota
PacketFormatDelimited
)
type Nexus struct {
Verbose bool
all *Pool
handlersMu sync.Mutex
handlers map[string]Handler
PacketFormat PacketFormat
}
func NewNexus() *Nexus {
n := Nexus{
all: NewPool(),
handlers: map[string]Handler{},
PacketFormat: PacketFormatJSON,
}
return &n
}
func (n *Nexus) debugf(format string, args ...interface{}) {
if n.Verbose {
fmt.Printf(format, args...)
}
}
func (n *Nexus) debug(args ...interface{}) {
if n.Verbose {
fmt.Println(args...)
}
}
func (n *Nexus) errorf(format string, args ...interface{}) {
fmt.Printf(format, args...)
}
func (n *Nexus) All() *Pool {
return n.all
}
func (n *Nexus) Handle(t string, handler Handler) {
n.handlersMu.Lock()
defer n.handlersMu.Unlock()
n.handlers[t] = handler
}
var Upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func (n *Nexus) ServeHTTP(w http.ResponseWriter, r *http.Request) {
n.Handler(w, r)
}
func (n *Nexus) Handler(w http.ResponseWriter, r *http.Request) {
ws, err := Upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
client := newClient(ws)
defer client.close()
n.all.Add(client)
defer n.all.Remove(client)
// write loop
go func() {
for {
select {
case msg := <-client.messageChan:
if msg == nil {
continue
}
n.debugf("sending message %v to %v", msg, client)
switch n.PacketFormat {
case PacketFormatJSON:
err := ws.WriteJSON(msg)
if err != nil {
n.errorf("error writing to websocket %v", msg)
}
case PacketFormatDelimited:
err := ws.WriteMessage(websocket.TextMessage, marshalDelimitedPacket(*msg))
if err != nil {
n.errorf("error writing to websocket %v", msg)
}
}
case <-client.Context.Done():
n.debugf("stopping websocket write loop due to context closed")
return
}
}
}()
// read loop
for {
select {
case <-client.Context.Done():
n.debugf("stopping websocket read loop due to context closed")
return
default:
p := &Packet{}
_, data, err := ws.ReadMessage()
if err != nil {
if err == io.EOF {
// remove connection
n.errorf("got eof error on read %s", err.Error())
return
}
n.errorf("got websocket error on receive %s", err.Error())
return
}
switch n.PacketFormat {
case PacketFormatJSON:
err = json.Unmarshal(data, p)
if err != nil {
n.errorf("received malformed json %s %v", err.Error(), p)
continue
}
case PacketFormatDelimited:
p, err = unmarshalDelimitedPacket(data)
if err != nil {
n.errorf("received malformed delimited message %s %v", err.Error(), p)
continue
}
}
n.debugf("received message %v from %v", p, client)
handler, ok := n.handlers[p.Type]
if !ok {
n.debugf("handler %s does not exist", p.Type, client.name)
continue
} else {
go handler(client, p)
}
}
}
}
func unmarshalDelimitedPacket(bytes []byte) (*Packet, error) {
str := string(bytes)
fmt.Println(str)
peices := strings.SplitN(str, ":", 2)
if len(peices) != 2 {
return nil, errors.New("delimiter not found in message bytes")
}
n, err := strconv.Atoi(peices[0])
if err != nil {
return nil, errors.Errorf("first field should be an integer, but was %s", peices[0])
}
if len(peices[1]) < n {
return nil, errors.Errorf("message ended unexpectedly, message length was not the expected size")
}
p := Packet{
Type: peices[1][:n],
Data: peices[1][n:],
}
return &p, nil
}
func marshalDelimitedPacket(p Packet) []byte {
return []byte(fmt.Sprintf("%d:%s%s", len(p.Type), p.Type, p.Data))
}