]> git.lizzy.rs Git - mt.git/blob - rudp/rudp.go
rudp: partial rewrite with new API supporting io.Readers
[mt.git] / rudp / rudp.go
1 /*
2 Package rudp implements the low-level Minetest protocol described at
3 https://dev.minetest.net/Network_Protocol#Low-level_protocol.
4 */
5 package rudp
6
7 import (
8         "encoding/binary"
9         "errors"
10         "io"
11         "time"
12 )
13
14 var be = binary.BigEndian
15
16 /*
17 UDP packet format:
18
19         protoID
20         src PeerID
21         channel uint8
22         rawType...
23 */
24
25 var ErrTimedOut = errors.New("timed out")
26
27 const (
28         ConnTimeout = 30 * time.Second
29         PingTimeout = 5 * time.Second
30 )
31
32 const (
33         MaxRelPktSize   = 32439825
34         MaxUnrelPktSize = 32636430
35 )
36
37 // protoID must be at the start of every UDP packet.
38 const protoID uint32 = 0x4f457403
39
40 // PeerIDs aren't actually used to identify peers, IP addresses and ports are,
41 // these just exist for backward compatibility.
42 type PeerID uint16
43
44 const (
45         // Used by clients before the server sets their ID.
46         PeerIDNil PeerID = iota
47
48         // The server always has this ID.
49         PeerIDSrv
50
51         // Lowest ID the server can assign to a client.
52         PeerIDCltMin
53 )
54
55 type rawType uint8
56
57 const (
58         rawCtl rawType = iota
59         // ctlType...
60
61         rawOrig
62         // data...
63
64         rawSplit
65         // seqnum
66         // n, i uint16
67         // data...
68
69         rawRel
70         // seqnum
71         // rawType...
72 )
73
74 type ctlType uint8
75
76 const (
77         ctlAck ctlType = iota
78         // seqnum
79
80         ctlSetPeerID
81         // PeerID
82
83         ctlPing // Sent to prevent timeout.
84
85         ctlDisco
86 )
87
88 type Pkt struct {
89         io.Reader
90         PktInfo
91 }
92
93 // Reliable packets in a channel are be received in the order they are sent in.
94 // A Channel must be less than ChannelCount.
95 type Channel uint8
96
97 const ChannelCount Channel = 3
98
99 type PktInfo struct {
100         Channel
101
102         // Unrel (unreliable) packets may be dropped, duplicated or reordered.
103         Unrel bool
104 }
105
106 // seqnums are sequence numbers used to maintain reliable packet order
107 // and identify split packets.
108 type seqnum uint16
109
110 const initSeqnum seqnum = 65500