-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
78 lines (65 loc) · 1.32 KB
/
packet.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
package pio
import (
"github.com/kmcsr/go-pio/encoding"
)
type (
PacketBase interface{
PktId()(uint32)
ParseFrom(encoding.Reader)(error)
WriteTo(encoding.Writer)(error)
}
Packet interface{
PacketBase
Trigger()(error)
}
PacketAsk interface{
PacketBase
Ask()(PacketBase, error)
}
)
type PacketNewer func()(PacketBase)
type (
EmptyPkt struct{
Id uint32
}
EmptyPktTrigger struct{
EmptyPkt
OnTrigger func()(error)
}
EmptyPktAsk struct{
EmptyPkt
OnAsk func()(PacketBase, error)
}
)
var _ PacketBase = EmptyPkt{}
var _ Packet = EmptyPktTrigger{}
var _ PacketAsk = EmptyPktAsk{}
func NewPkt(id uint32)(PacketBase){
return EmptyPkt{
Id: id,
}
}
func NewPktTrigger(id uint32, ontrigger func()(error))(Packet){
return EmptyPktTrigger{
EmptyPkt: EmptyPkt{id},
OnTrigger: ontrigger,
}
}
func NewPktAsk(id uint32, onask func()(PacketBase, error))(PacketAsk){
return EmptyPktAsk{
EmptyPkt: EmptyPkt{id},
OnAsk: onask,
}
}
func (pkt EmptyPkt)PktId()(uint32){ return pkt.Id }
func (EmptyPkt)ParseFrom(encoding.Reader)(error){ return nil }
func (EmptyPkt)WriteTo(encoding.Writer)(error){ return nil }
func (pkt EmptyPktTrigger)Trigger()(error){
return pkt.OnTrigger()
}
func (pkt EmptyPktAsk)Ask()(PacketBase, error){
if pkt.OnAsk == nil {
panic("pkt.OnAsk == nil")
}
return pkt.OnAsk()
}