2023-10-19 09:33:40 +08:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
2024-01-22 13:10:10 +08:00
|
|
|
"context"
|
2023-10-19 09:33:40 +08:00
|
|
|
"log/slog"
|
|
|
|
"net"
|
2023-10-19 14:02:20 +08:00
|
|
|
"runtime/debug"
|
2023-10-19 09:33:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UdpServer interface {
|
2024-01-24 13:25:00 +08:00
|
|
|
Listen() error
|
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 {
|
2024-01-22 13:10:10 +08:00
|
|
|
addr string
|
|
|
|
conn *net.UDPConn
|
|
|
|
handler UdpMsgHandler
|
|
|
|
cancelFn context.CancelFunc
|
2024-01-24 13:25:00 +08:00
|
|
|
done chan struct{} // 服务协程退出信号
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates a new instance of UdpServer.
|
|
|
|
func NewServer(addr string, handler UdpMsgHandler) UdpServer {
|
2024-01-24 13:25:00 +08:00
|
|
|
return &server{addr: addr, handler: handler, done: make(chan struct{})}
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 13:25:00 +08:00
|
|
|
func (s *server) Listen() error {
|
2023-10-19 09:33:40 +08:00
|
|
|
udpAddr, err := net.ResolveUDPAddr("udp", s.addr)
|
|
|
|
if err != nil {
|
2024-01-24 13:25:00 +08:00
|
|
|
return err
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", udpAddr)
|
|
|
|
if err != nil {
|
2024-01-24 13:25:00 +08:00
|
|
|
return err
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
s.conn = conn
|
2024-01-22 13:10:10 +08:00
|
|
|
ctx, cfn := context.WithCancel(context.Background())
|
2023-10-19 09:33:40 +08:00
|
|
|
// 启动监听处理
|
2024-01-22 13:10:10 +08:00
|
|
|
go s.listenAndHandle(ctx)
|
|
|
|
s.cancelFn = cfn
|
2024-01-24 13:25:00 +08:00
|
|
|
return nil
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
|
2023-10-26 15:06:26 +08:00
|
|
|
func (s *server) Close() {
|
2024-01-24 13:25:00 +08:00
|
|
|
err := s.conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("udp server close error", "error", err)
|
|
|
|
}
|
2024-01-22 13:10:10 +08:00
|
|
|
s.cancelFn()
|
2024-01-24 13:25:00 +08:00
|
|
|
<-s.done
|
2023-10-26 15:06:26 +08:00
|
|
|
}
|
|
|
|
|
2024-01-22 13:10:10 +08:00
|
|
|
func (s *server) listenAndHandle(ctx context.Context) {
|
2024-01-24 13:25:00 +08:00
|
|
|
defer close(s.done)
|
2023-10-19 09:33:40 +08:00
|
|
|
defer s.conn.Close()
|
2024-01-24 13:25:00 +08:00
|
|
|
mainLoop:
|
2023-10-19 09:33:40 +08:00
|
|
|
for {
|
2024-01-22 13:10:10 +08:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-01-24 13:25:00 +08:00
|
|
|
// slog.Info("udp server listen 关闭", "addr", s.addr)
|
|
|
|
break mainLoop
|
2024-01-22 13:10:10 +08:00
|
|
|
default:
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
2024-01-22 13:10:10 +08:00
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, _, err := s.conn.ReadFromUDP(buf)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("udp server read error", "addr", s.addr, "error", err)
|
|
|
|
continue
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
2024-01-22 13:10:10 +08:00
|
|
|
// slog.Debug("udp server handle", "addr", ap.String())
|
|
|
|
go s.handle(buf[:n])
|
2023-10-19 09:33:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|