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