-
Notifications
You must be signed in to change notification settings - Fork 2
/
netx.go
156 lines (140 loc) · 3.85 KB
/
netx.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
149
150
151
152
153
154
155
156
package main
import (
"log"
"net"
"sync"
"time"
)
type UDPServer struct {
pc net.PacketConn
clients map[string]net.Conn // *UDPConn2
clientsmu sync.Mutex
}
func ListenUDP(address string) (net.Listener, error) {
lsner := &UDPServer{}
pc, err := net.ListenPacket("udp", address)
if err != nil {
return nil, err
}
lsner.pc = pc
lsner.clients = make(map[string]net.Conn)
return lsner, nil
}
// should block
func (this *UDPServer) Accept() (net.Conn, error) {
// zrbuf := make([]byte, 0)
for {
kbuf := make([]byte, 1024) // TODO what size
rn, addr, err := this.pc.ReadFrom(kbuf)
log.Println(rn, addr, err, string(kbuf[:rn]))
if err != nil {
log.Println(err)
return nil, err
}
hasnew := false
var newc net.Conn
this.clientsmu.Lock()
if newc2, ok := this.clients[addr.String()]; ok {
newc = newc2
} else {
// think as new connection
newc = NewUDPConn2(this.pc, addr)
this.clients[addr.String()] = newc
hasnew = true
}
this.clientsmu.Unlock()
// check chan close and don't send data
func() {
defer func() {
if err := recover(); err != nil {
log.Println(err, hasnew)
if err.(error).Error() == "send on closed channel" {
this.clientsmu.Lock()
delete(this.clients, addr.String())
newc = NewUDPConn2(this.pc, addr)
this.clients[addr.String()] = newc
hasnew = true
this.clientsmu.Unlock()
}
}
}()
newc.(*UDPConn2).InChan <- kbuf[:rn]
}()
if !hasnew {
// time.Sleep(1 * time.Microsecond)
} else {
return newc, nil
}
}
}
func (this *UDPServer) Close() error { return this.pc.Close() }
func (this *UDPServer) Addr() net.Addr { return this.pc.LocalAddr() }
/////
type dummyConn struct{}
func (this *dummyConn) Close() error { return nil }
func (this *dummyConn) SetDeadline(t time.Time) error { return nil }
func (this *dummyConn) SetReadDeadline(t time.Time) error { return nil }
func (this *dummyConn) SetWriteDeadline(t time.Time) error { return nil }
/*
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() Addr
RemoteAddr() Addr
SetDeadline(t time.Time) error
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
*/
type UDPConn2 struct {
// *dummyConn
Conn net.PacketConn
Raddr *net.UDPAddr
InChan chan []byte
}
func NewUDPConn2(c net.PacketConn, raddr net.Addr) net.Conn {
this := &UDPConn2{}
// this.dummyConn = &dummyConn{}
this.Conn = c
this.Raddr = raddr.(*net.UDPAddr)
this.InChan = make(chan []byte, 1)
return this
}
func (this *UDPConn2) Read(b []byte) (n int, err error) {
nb := <-this.InChan
rn := copy(b, nb)
if rn < len(nb) {
panic("buffer too small")
}
return rn, nil
}
func (this *UDPConn2) Write(b []byte) (n int, err error) {
return this.Conn.WriteTo(b, this.Raddr)
}
func (this *UDPConn2) Close() (err error) {
log.Println(123)
close(this.InChan)
return
}
func (this *UDPConn2) LocalAddr() net.Addr { return this.Conn.LocalAddr() }
func (this *UDPConn2) RemoteAddr() net.Addr { return this.Raddr }
func (this *UDPConn2) SetDeadline(t time.Time) error { return nil }
func (this *UDPConn2) SetReadDeadline(t time.Time) error { return nil }
func (this *UDPConn2) SetWriteDeadline(t time.Time) error { return nil }
/////
type UDPConn struct {
net.PacketConn
Raddr *net.UDPAddr
}
func (this *UDPConn) Close() error { return nil }
func (this *UDPConn) RemoteAddr() net.Addr { return this.Raddr }
func (this *UDPConn) Write(b []byte) (int, error) { return this.PacketConn.WriteTo(b, this.Raddr) }
func (this *UDPConn) Read(b []byte) (int, error) {
n, addr, err := this.ReadFrom(b)
if addr.String() != this.Raddr.String() {
}
return n, err
}
func NewUDPConnOnServer(srv net.PacketConn, raddr net.Addr) net.Conn {
c := &UDPConn{PacketConn: srv, Raddr: raddr.(*net.UDPAddr)}
return c
}