]> git.lizzy.rs Git - mt.git/blob - rudp/rudp.go
rudp: optimize and refactor
[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 All exported functions and methods in this package are safe for concurrent use
6 by multiple goroutines.
7 */
8 package rudp
9
10 import "encoding/binary"
11
12 var be = binary.BigEndian
13
14 // protoID must be at the start of every network packet.
15 const protoID uint32 = 0x4f457403
16
17 // PeerIDs aren't actually used to identify peers, network addresses are,
18 // these just exist for backward compatability.
19 type PeerID uint16
20
21 const (
22         // Used by clients before the server sets their ID.
23         PeerIDNil PeerID = iota
24
25         // The server always has this ID.
26         PeerIDSrv
27
28         // Lowest ID the server can assign to a client.
29         PeerIDCltMin
30 )
31
32 // ChannelCount is the maximum channel number + 1.
33 const ChannelCount = 3
34
35 /*
36 rawPkt.Data format (big endian):
37
38         rawType
39         switch rawType {
40         case rawTypeCtl:
41                 ctlType
42                 switch ctlType {
43                 case ctlAck:
44                         // Tells peer you received a rawTypeRel
45                         // and it doesn't need to resend it.
46                         seqnum
47                 case ctlSetPeerId:
48                         // Tells peer to send packets with this Src PeerID.
49                         PeerId
50                 case ctlPing:
51                         // Sent to prevent timeout.
52                 case ctlDisco:
53                         // Tells peer that you disconnected.
54                 }
55         case rawTypeOrig:
56                 Pkt.(Data)
57         case rawTypeSplit:
58                 // Packet larger than MaxNetPktSize split into smaller packets.
59                 // Packets with I >= Count should be ignored.
60                 // Once all Count chunks are recieved, they are sorted by I and
61                 // concatenated to make a Pkt.(Data).
62                 seqnum // Identifies split packet.
63                 Count, I uint16
64                 Chunk...
65         case rawTypeRel:
66                 // Resent until a ctlAck with same seqnum is recieved.
67                 // seqnums are sequencial and start at seqnumInit,
68                 // These should be processed in seqnum order.
69                 seqnum
70                 rawPkt.Data
71         }
72 */
73 type rawPkt struct {
74         Data  []byte
75         ChNo  uint8
76         Unrel bool
77 }
78
79 type rawType uint8
80
81 const (
82         rawTypeCtl rawType = iota
83         rawTypeOrig
84         rawTypeSplit
85         rawTypeRel
86 )
87
88 type ctlType uint8
89
90 const (
91         ctlAck ctlType = iota
92         ctlSetPeerID
93         ctlPing
94         ctlDisco
95 )
96
97 type Pkt struct {
98         Data  []byte
99         ChNo  uint8
100         Unrel bool
101 }
102
103 // seqnums are sequence numbers used to maintain reliable packet order
104 // and to identify split packets.
105 type seqnum uint16
106
107 const seqnumInit seqnum = 65500