2023-10-19 09:33:40 +08:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log/slog"
|
|
|
|
"net"
|
2023-10-19 14:02:20 +08:00
|
|
|
"runtime/debug"
|
2023-10-19 09:33:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UdpServer interface {
|
|
|
|
Listen()
|
2023-10-26 15:06:26 +08:00
|
|
|
Close()
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type UdpMsgHandler func(b []byte)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
addr string
|
|
|
|
conn *net.UDPConn
|
|
|
|
handler UdpMsgHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new instance of UdpServer.
|
|
|
|
func NewServer(addr string, handler UdpMsgHandler) UdpServer {
|
|
|
|
return &server{addr: addr, handler: handler}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Listen() {
|
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", s.addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", udpAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s.conn = conn
|
|
|
|
// 启动监听处理
|
|
|
|
go s.listenAndHandle()
|
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (s *server) Close() {
|
|
|
|
err := s.conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("udp server close error", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 09:33:40 +08:00
|
|
|
func (s *server) listenAndHandle() {
|
|
|
|
defer s.conn.Close()
|
|
|
|
for {
|
|
|
|
b := make([]byte, 1024)
|
|
|
|
n, err := s.conn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("udp server read error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
go s.handle(b[0:n])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) handle(b []byte) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2023-10-19 14:02:20 +08:00
|
|
|
slog.Error("udp server handle error", "error", err, "stack", string(debug.Stack()))
|
|
|
|
debug.PrintStack()
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
// slog.Info("udp server handle", "msg", string(b))
|
|
|
|
s.handler(b)
|
|
|
|
}
|