-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackethandler.go
122 lines (107 loc) · 3.55 KB
/
packethandler.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
package hacket
import (
"net"
"sync"
"time"
)
// PacketWriter interface used in handlers
type PacketWriter interface {
WriteTo(msg PacketMessage, addr net.Addr) (int, error)
}
// hacketPacketWriter is a Wrapper around a packetConn. PacketWriter specifically
// wraps the Write side of a PacketConn only enforcing the PacketMessage param to
// writeTo. This is so we can force the user into using our PacketMessage and
// PacketMessageBuilder
type hacketPacketWriter struct {
conn net.PacketConn
options *packetOptions
}
// WriteTo wraps the internal PacketConn WriteTo. PacketMessage is the payload of the network
// packet, Addr is the address of the remote peer we are sending to
func (hpw *hacketPacketWriter) WriteTo(msg PacketMessage, addr net.Addr) (int, error) {
if hpw.options.WriteDeadline > 0 {
deadline := time.Now().Add(hpw.options.WriteDeadline)
if err := hpw.conn.SetWriteDeadline(deadline); err != nil {
return 0, err
}
}
return hpw.conn.WriteTo(msg, addr)
}
// PacketHandler defines a function to handle Packets
type PacketHandler interface {
HandlePacket(packet Packet, pw PacketWriter)
}
// PacketHandlerFunc defines a function definition to handle packets
type PacketHandlerFunc func(packet Packet, pw PacketWriter)
// HandlePacket statifies the PacketHandler interface
func (s PacketHandlerFunc) HandlePacket(packet Packet, pw PacketWriter) {
s(packet, pw)
}
type packetMuxEntry struct {
packetHandler PacketHandler
pktType PacketType
}
//PacketMux allows PacketHandlers to be registered.
type PacketMux struct {
mu sync.RWMutex
m map[PacketType]packetMuxEntry
}
// NewPacketMux initializes a PacketMux
func NewPacketMux() *PacketMux {
return new(PacketMux)
}
// PacketHandler registers a PacketHandler with key PacketType
// PacketType acts as the route and PacketHandler is the function to be called
func (pmux *PacketMux) PacketHandler(pktType PacketType, packetHandler PacketHandler) error {
pmux.mu.Lock()
if packetHandler == nil {
return ErrNilPacketHander
}
if pmux.m == nil {
pmux.m = make(map[PacketType]packetMuxEntry)
}
if _, ok := pmux.m[pktType]; ok {
return ErrPacketHandlerAlreadyExists
}
pmux.m[pktType] = packetMuxEntry{packetHandler: packetHandler, pktType: pktType}
pmux.mu.Unlock()
return nil
}
// PacketHandlerFunc registers a PacketHandlerFunc with key PacketType
// PacketType acts as the route and PacketHandler is the function to be called
func (pmux *PacketMux) PacketHandlerFunc(pktType PacketType, packetHandler func(packet Packet, pw PacketWriter)) error {
pmux.mu.Lock()
if packetHandler == nil {
return ErrNilPacketHander
}
if pmux.m == nil {
pmux.m = make(map[PacketType]packetMuxEntry)
}
if _, ok := pmux.m[pktType]; ok {
return ErrPacketHandlerAlreadyExists
}
pmux.m[pktType] = packetMuxEntry{packetHandler: PacketHandlerFunc(packetHandler), pktType: pktType}
pmux.mu.Unlock()
return nil
}
// HandlePacket statifies the PacketHandler interface. A PacketType is expected to be
// set by the caller. The PacketType is used to find the PacketHandler to call
func (pmux *PacketMux) HandlePacket(packet Packet, pw PacketWriter) {
pktType, msg, _ := decode(packet.Msg())
// update packet msg with remove pktType
packet.SetMsg(msg)
handler := pmux.findPacketHandler(pktType)
if handler != nil {
handler.HandlePacket(packet, pw)
}
}
// findPacketHandler returns a PacketHandler if the pktType is found
func (pmux *PacketMux) findPacketHandler(pktType PacketType) PacketHandler {
pmux.mu.RLock()
entry, ok := pmux.m[pktType]
if !ok {
return nil
}
pmux.mu.RUnlock()
return entry.packetHandler
}