-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathNetConstants.cs
More file actions
124 lines (114 loc) · 5.12 KB
/
NetConstants.cs
File metadata and controls
124 lines (114 loc) · 5.12 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
namespace LiteNetLib
{
/// <summary>
/// Sending method type
/// </summary>
public enum DeliveryMethod : byte
{
/// <summary>
/// Unreliable. Packets can be dropped, can be duplicated, can arrive without order.
/// </summary>
Unreliable = 4,
/// <summary>
/// Reliable. Packets won't be dropped, won't be duplicated, can arrive without order.
/// </summary>
ReliableUnordered = 0,
/// <summary>
/// Unreliable. Packets can be dropped, won't be duplicated, will arrive in order.
/// </summary>
Sequenced = 1,
/// <summary>
/// Reliable and ordered. Packets won't be dropped, won't be duplicated, will arrive in order.
/// </summary>
ReliableOrdered = 2,
/// <summary>
/// Reliable only last packet. Packets can be dropped (except the last one), won't be duplicated, will arrive in order.
/// Cannot be fragmented
/// </summary>
ReliableSequenced = 3
}
/// <summary>
/// Network constants. Can be tuned from sources for your purposes.
/// </summary>
public static class NetConstants
{
/// <summary>
/// Default window size for reliable channels (number of packets).
/// </summary>
public const int DefaultWindowSize = 64;
/// <summary>
/// Size of the underlying UDP socket receive and send buffers in bytes. <br/>
/// Default is 1MB.
/// </summary>
public const int SocketBufferSize = 1024 * 1024;
/// <summary>
/// Time To Live (TTL) for the UDP packets.
/// </summary>
public const int SocketTTL = 255;
/// <summary>
/// Size of the base packet header (PacketProperty) in <see cref="byte"/>s.
/// </summary>
public const int HeaderSize = 1;
/// <summary>
/// Size of the header for sequenced or reliable messages in <see cref="byte"/>s. <br/>
/// Includes <see cref="HeaderSize"/>, Sequence, and ChannelId.
/// </summary>
public const int ChanneledHeaderSize = 4;
/// <summary>
/// Additional header size required for fragmented packets in <see cref="byte"/>s. <br/>
/// Includes FragmentId, FragmentPart, and FragmentsTotal.
/// </summary>
public const int FragmentHeaderSize = 6;
/// <summary>
/// Total header size for a fragmented channeled packet in <see cref="byte"/>s. <br/>
/// Combines <see cref="ChanneledHeaderSize"/> and <see cref="FragmentHeaderSize"/>.
/// </summary>
public const int FragmentedHeaderTotalSize = ChanneledHeaderSize + FragmentHeaderSize;
/// <summary>
/// Maximum possible sequence number before wrapping back to zero.
/// </summary>
public const ushort MaxSequence = 32768;
/// <summary>
/// Half of the <see cref="MaxSequence"/>, used for sequence comparison and wrap-around logic.
/// </summary>
public const ushort HalfMaxSequence = MaxSequence / 2;
//protocol
internal const int ProtocolId = 13;
internal const int MaxUdpHeaderSize = 68;
internal const int ChannelTypeCount = 4;
internal const int FragmentedChannelsCount = 2;
internal const int MaxFragmentsInWindow = DefaultWindowSize / 2;
internal static readonly int[] PossibleMtu =
{
//576 - MaxUdpHeaderSize minimal (RFC 1191)
1024, //most games standard
1232 - MaxUdpHeaderSize,
1460 - MaxUdpHeaderSize, //google cloud
1472 - MaxUdpHeaderSize, //VPN
1492 - MaxUdpHeaderSize, //Ethernet with LLC and SNAP, PPPoE (RFC 1042)
1500 - MaxUdpHeaderSize //Ethernet II (RFC 1191)
};
/// <summary>
/// The starting Maximum Transmission Unit (MTU) used for new connections before path MTU discovery.
/// </summary>
public static readonly int InitialMtu = PossibleMtu[0];
/// <summary>
/// Maximum possible packet size allowed by the library based on the largest supported MTU.
/// </summary>
public static readonly int MaxPacketSize = PossibleMtu[PossibleMtu.Length - 1];
/// <summary>
/// Maximum payload size for a single unreliable packet in <see cref="byte"/>s. <br/>
/// Calculated as <see cref="MaxPacketSize"/> - <see cref="HeaderSize"/>.
/// </summary>
public static readonly int MaxUnreliableDataSize = MaxPacketSize - HeaderSize;
/// <summary>
/// Maximum possible value for <see cref="NetPacket.ConnectionNumber"/>.
/// </summary>
/// <remarks>
/// This value is used to distinguish between different connection instances from the same <see cref="System.Net.IPEndPoint"/>. <br/>
/// It allows the receiver to identify and discard packets belonging to previous connection attempts that may arrive
/// late due to network jitter, even if they originate from the same address and port. <br/>
/// </remarks>
public const byte MaxConnectionNumber = 4;
}
}