summaryrefslogtreecommitdiff
path: root/rudp/rudp.go
blob: faf67d97bf7174f77ec9d238c42b72259bf3e2fe (plain) (blame)
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
/*
Package rudp implements the low-level Minetest protocol described at
https://dev.minetest.net/Network_Protocol#Low-level_protocol.
*/
package rudp

import (
	"encoding/binary"
	"errors"
	"io"
	"time"
)

var be = binary.BigEndian

/*
UDP packet format:

	protoID
	src PeerID
	channel uint8
	rawType...
*/

var ErrTimedOut = errors.New("timed out")

const (
	ConnTimeout = 30 * time.Second
	PingTimeout = 5 * time.Second
)

const (
	MaxRelPktSize   = 32439825
	MaxUnrelPktSize = 32636430
)

// protoID must be at the start of every UDP packet.
const protoID uint32 = 0x4f457403

// PeerIDs aren't actually used to identify peers, IP addresses and ports are,
// these just exist for backward compatibility.
type PeerID uint16

const (
	// Used by clients before the server sets their ID.
	PeerIDNil PeerID = iota

	// The server always has this ID.
	PeerIDSrv

	// Lowest ID the server can assign to a client.
	PeerIDCltMin
)

type rawType uint8

const (
	rawCtl rawType = iota
	// ctlType...

	rawOrig
	// data...

	rawSplit
	// seqnum
	// n, i uint16
	// data...

	rawRel
	// seqnum
	// rawType...
)

type ctlType uint8

const (
	ctlAck ctlType = iota
	// seqnum

	ctlSetPeerID
	// PeerID

	ctlPing // Sent to prevent timeout.

	ctlDisco
)

type Pkt struct {
	io.Reader
	PktInfo
}

// Reliable packets in a channel are be received in the order they are sent in.
// A Channel must be less than ChannelCount.
type Channel uint8

const ChannelCount Channel = 3

type PktInfo struct {
	Channel

	// Unrel (unreliable) packets may be dropped, duplicated or reordered.
	Unrel bool
}

// seqnums are sequence numbers used to maintain reliable packet order
// and identify split packets.
type seqnum uint16

const initSeqnum seqnum = 65500