-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
udp.go
148 lines (124 loc) · 2.5 KB
/
udp.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
package meta
import (
"errors"
"net"
"strconv"
"sync"
"ergo.services/ergo/gen"
)
//
// UDP Server meta process
//
const (
defaultUDPBufferSize int = 65000
)
func CreateUDP(options UDPOptions) (gen.MetaBehavior, error) {
hp := net.JoinHostPort(options.Host, strconv.Itoa(int(options.Port)))
pc, err := net.ListenPacket("udp", hp)
if err != nil {
return nil, err
}
mb := &udp{
pc: pc,
}
if options.BufferSize < 1 {
options.BufferSize = defaultUDPBufferSize
}
mb.bufferSize = options.BufferSize
// check sync.Pool
if options.BufferPool != nil {
b := options.BufferPool.Get()
if _, ok := b.([]byte); ok == false {
return nil, errors.New("options.BufferPool must be pool of []byte values")
}
// get it back to the pool
options.BufferPool.Put(b)
}
mb.bufpool = options.BufferPool
mb.process = options.Process
return mb, nil
}
type UDPOptions struct {
Host string
Port uint16
Process gen.Atom
BufferSize int
BufferPool *sync.Pool
}
type MessageUDP struct {
ID gen.Alias
Addr net.Addr
Data []byte
}
type udp struct {
gen.MetaProcess
pc net.PacketConn
bufferSize int
process gen.Atom
bufpool *sync.Pool
// TODO add to UDPOptions
stopnoproc bool
}
func (u *udp) Init(process gen.MetaProcess) error {
u.MetaProcess = process
return nil
}
func (u *udp) Start() error {
var buf []byte
var to any
if u.process == "" {
to = u.Parent()
} else {
to = u.process
}
id := u.ID()
for {
if u.bufpool == nil {
buf = make([]byte, u.bufferSize)
} else {
b := u.bufpool.Get()
buf = b.([]byte)
}
n, addr, err := u.pc.ReadFrom(buf)
if n > 0 {
packet := MessageUDP{
ID: id,
Data: buf[:n],
Addr: addr,
}
if err := u.Send(to, packet); err != nil {
u.Log().Error("unable to send MessageUDP to %s: %s", to, err)
if u.stopnoproc {
return err
}
}
}
if err != nil {
return err
}
}
}
func (u *udp) HandleMessage(from gen.PID, message any) error {
switch m := message.(type) {
case MessageUDP:
_, err := u.pc.WriteTo(m.Data, m.Addr)
if u.bufpool != nil {
u.bufpool.Put(m.Data)
}
return err
default:
u.Log().Error("unsupported message from %s. ignored", from)
}
return nil
}
func (u *udp) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error) {
return nil, nil
}
func (u *udp) Terminate(reason error) {
u.pc.Close()
}
func (u *udp) HandleInspect(from gen.PID, item ...string) map[string]string {
return map[string]string{
"listener": u.pc.LocalAddr().String(),
}
}