summaryrefslogtreecommitdiff
path: root/rudp/net.go
blob: e2e728978865bd2d38f6af4a0ef0143d9e67f15d (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
package rudp

import (
	"errors"
	"net"
)

// ErrClosed is deprecated, use net.ErrClosed instead.
var ErrClosed = net.ErrClosed

/*
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 {
			if errors.Is(err, net.ErrClosed) {
				break
			}

			errs <- err
			continue
		}

		pkts <- netPkt{addr, buf[:n]}
	}

	close(pkts)
}