@@ -64,13 +64,13 @@ func (t *TCPListener) Addr() net.Addr {
64
64
return t .ln .Addr ()
65
65
}
66
66
67
+ type OnCloseFunc func () error
68
+
67
69
type acceptResponse struct {
68
70
conn transport.StreamConn
69
71
err error
70
72
}
71
73
72
- type OnCloseFunc func () error
73
-
74
74
type virtualStreamListener struct {
75
75
mu sync.Mutex // Mutex to protect access to the channels
76
76
addr net.Addr
@@ -119,14 +119,52 @@ func (sl *virtualStreamListener) Addr() net.Addr {
119
119
return sl .addr
120
120
}
121
121
122
+ type packetResponse struct {
123
+ n int
124
+ addr net.Addr
125
+ err error
126
+ data []byte
127
+ }
128
+
122
129
type virtualPacketConn struct {
123
130
net.PacketConn
131
+ mu sync.Mutex // Mutex to protect access to the channels
132
+ readCh <- chan packetResponse
133
+ closeCh chan struct {}
134
+ closed bool
124
135
onCloseFunc OnCloseFunc
125
136
}
126
137
127
- func (spc * virtualPacketConn ) Close () error {
128
- if spc .onCloseFunc != nil {
129
- return spc .onCloseFunc ()
138
+ func (pc * virtualPacketConn ) ReadFrom (p []byte ) (n int , addr net.Addr , err error ) {
139
+ pc .mu .Lock ()
140
+ readCh := pc .readCh
141
+ pc .mu .Unlock ()
142
+
143
+ select {
144
+ case packetResponse , ok := <- readCh :
145
+ if ! ok {
146
+ return 0 , nil , net .ErrClosed
147
+ }
148
+ copy (p , packetResponse .data )
149
+ return packetResponse .n , packetResponse .addr , packetResponse .err
150
+ case <- pc .closeCh :
151
+ return 0 , nil , net .ErrClosed
152
+ }
153
+ }
154
+
155
+ func (pc * virtualPacketConn ) Close () error {
156
+ pc .mu .Lock ()
157
+ if pc .closed {
158
+ pc .mu .Unlock ()
159
+ return nil
160
+ }
161
+ pc .closed = true
162
+ pc .readCh = nil
163
+ close (pc .closeCh )
164
+ pc .mu .Unlock ()
165
+
166
+ if pc .onCloseFunc != nil {
167
+ return pc .onCloseFunc ()
130
168
}
131
169
return nil
132
170
}
@@ -204,6 +242,7 @@ type multiPacketListener struct {
204
242
mu sync.Mutex
205
243
addr string
206
244
pc RefCount [net.PacketConn ]
245
+ readCh chan packetResponse
207
246
onCloseFunc OnCloseFunc
208
247
}
209
248
@@ -226,6 +265,18 @@ func (m *multiPacketListener) Acquire() (net.PacketConn, error) {
226
265
return nil , err
227
266
}
228
267
m .pc = NewRefCount (pc , m .onCloseFunc )
268
+ m .readCh = make (chan packetResponse )
269
+ go func () {
270
+ for {
271
+ buffer := make ([]byte , serverUDPBufferSize )
272
+ n , addr , err := pc .ReadFrom (buffer )
273
+ if err != nil {
274
+ close (m .readCh )
275
+ return
276
+ }
277
+ m .readCh <- packetResponse {n : n , addr : addr , err : err , data : buffer [:n ]}
278
+ }
279
+ }()
229
280
}
230
281
return m .pc , nil
231
282
}()
@@ -236,6 +287,8 @@ func (m *multiPacketListener) Acquire() (net.PacketConn, error) {
236
287
pc := refCount .Acquire ()
237
288
return & virtualPacketConn {
238
289
PacketConn : pc ,
290
+ readCh : m .readCh ,
291
+ closeCh : make (chan struct {}),
239
292
onCloseFunc : refCount .Close ,
240
293
}, nil
241
294
}
0 commit comments