blob: 421a3e73ec9254147dbfb161fb3b30d627281120 (
plain)
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
|
package rudp
import (
"errors"
"net"
"strings"
)
// TODO: Use net.ErrClosed when Go 1.16 is released.
var ErrClosed = errors.New("use of closed peer")
/*
netPkt.Data format (big endian):
ProtoID
Src PeerID
ChNo uint8 // Must be < ChannelCount.
RawPkt.Data
*/
type netPkt struct {
SrcAddr net.Addr
Data []byte
}
func readNetPkts(conn net.PacketConn, pkts chan<- netPkt, errs chan<- error) {
for {
buf := make([]byte, MaxNetPktSize)
n, addr, err := conn.ReadFrom(buf)
if err != nil {
// TODO: Change to this when Go 1.16 is released:
// if errors.Is(err, net.ErrClosed) {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
errs <- err
continue
}
pkts <- netPkt{addr, buf[:n]}
}
close(pkts)
}
|