From a865d2bce1aa097273fdb9d0d02d9cfa8460aefd Mon Sep 17 00:00:00 2001 From: anon5 Date: Sat, 7 Nov 2020 18:01:24 +0000 Subject: Initial public release --- rudp/net.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 rudp/net.go (limited to 'rudp/net.go') diff --git a/rudp/net.go b/rudp/net.go new file mode 100644 index 0000000..421a3e7 --- /dev/null +++ b/rudp/net.go @@ -0,0 +1,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) +} -- cgit v1.2.3