-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux.go
52 lines (44 loc) · 1.32 KB
/
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
package gosc
import "net"
// Mux is the default multiplex handler for messages and bundles. Returned by NewMux.
type Mux struct {
bundleHandler BundleHandler
messageHandlers map[string]MessageHandler
}
func (m *Mux) HandlePackage(writer *ResponseWriter, pkg Package) {
switch x := pkg.(type) {
case *Message:
if handler, ok := m.messageHandlers[x.Address]; ok {
handler.HandleMessage(writer, x)
}
case *Bundle:
if m.bundleHandler != nil {
m.bundleHandler.HandleBundle(writer, x)
}
}
}
func (m *Mux) HandleMessage(addr string, handler MessageHandler) {
m.messageHandlers[addr] = handler
}
func (m *Mux) HandleMessageFunc(addr string, handlerFunc MessageHandlerFunc) {
m.messageHandlers[addr] = handlerFunc
}
// NewMux returns the Mux. The bundleHandler can be nil if not handling
// bundles.
func NewMux(bundleHandler BundleHandler) *Mux {
return &Mux{
bundleHandler: bundleHandler,
messageHandlers: make(map[string]MessageHandler),
}
}
// ResponseWriter is used to send responses back to the requesting client on the
// incoming connection.
type ResponseWriter struct {
src net.Addr
trans Transport
}
// Send sends a Package to the client as a response using the Transport of the
// server and the incoming connection.
func (w *ResponseWriter) Send(pkg Package) error {
return w.trans.Send(pkg, w.src)
}