-
Notifications
You must be signed in to change notification settings - Fork 22
/
server_windows.go
91 lines (71 loc) · 1.53 KB
/
server_windows.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
package fastudp
import (
"fmt"
"net"
"sync"
"sync/atomic"
"github.com/shaoyuan1943/fastudp/netudp"
)
type Server struct {
wg sync.WaitGroup
handler EventHandler
conn *net.UDPConn
err error
closed atomic.Value
}
func NewUDPServer(network, addr string, mtu int, handler EventHandler) (*Server, error) {
if !netudp.IsUDP(network) {
return nil, fmt.Errorf("unknown network: %v", network)
}
svr := &Server{
handler: handler,
}
if err := svr.start(network, addr, mtu); err != nil {
return nil, err
}
svr.closed.Store(false)
return svr, nil
}
func (svr *Server) start(network, addr string, mtu int) error {
udpAddr, err := net.ResolveUDPAddr(network, addr)
if err != nil {
return fmt.Errorf("resolve addr: %v", err)
}
conn, err := net.ListenUDP(network, udpAddr)
if err != nil {
return fmt.Errorf("listen udp: %v", err)
}
svr.conn = conn
svr.wg.Add(1)
go func() {
defer svr.wg.Done()
buffer := make([]byte, mtu)
for {
n, remoteAddr, err := conn.ReadFrom(buffer)
if err != nil {
return
}
if n > 0 {
svr.handler.OnReaded(buffer[:n], remoteAddr.(*net.UDPAddr))
}
}
}()
return nil
}
func (svr *Server) Shutdown(err error) {
if svr.IsClosed() {
return
}
svr.conn.Close()
svr.wg.Wait()
svr.closed.Store(true)
}
func (svr *Server) IsClosed() bool {
return svr.closed.Load().(bool)
}
func (svr *Server) WriteTo(addr *net.UDPAddr, data []byte) (int, error) {
if svr.closed.Load().(bool) {
return 0, fmt.Errorf("server is closed")
}
return svr.conn.WriteTo(data, addr)
}