forked from GlaireDaggers/ReliableNetcode.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReliableEndpoint.cs
More file actions
168 lines (140 loc) · 4.12 KB
/
ReliableEndpoint.cs
File metadata and controls
168 lines (140 loc) · 4.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReliableNetcode.Utils;
namespace ReliableNetcode
{
/// <summary>
/// Quality-of-service type for a message
/// </summary>
public enum QosType : byte
{
/// <summary>
/// Message is guaranteed to arrive and in order
/// </summary>
Reliable = 0,
/// <summary>
/// Message is not guaranteed delivery nor order
/// </summary>
Unreliable = 1,
/// <summary>
/// Message is not guaranteed delivery, but will be in order
/// </summary>
UnreliableOrdered = 2
}
/// <summary>
/// Main class for routing messages through QoS channels
/// </summary>
public class ReliableEndpoint
{
/// <summary>
/// Method which will be called to transmit raw datagrams over the network
/// </summary>
public Action<byte[], int> TransmitCallback;
/// <summary>
/// Method which will be called when messages are received
/// </summary>
public Action<byte[], int> ReceiveCallback;
// Index, buffer, bufferLength
public Action<uint, byte[], int> TransmitExtendedCallback;
public Action<uint, byte[], int> ReceiveExtendedCallback;
public uint Index = uint.MaxValue;
/// <summary>
/// Approximate round-trip-time
/// </summary>
public float RTT => _reliableChannel.RTT;
/// <summary>
/// Approximate packet loss
/// </summary>
public float PacketLoss => _reliableChannel.PacketLoss;
/// <summary>
/// Approximate send bandwidth
/// </summary>
public float SentBandwidthKBPS => _reliableChannel.SentBandwidthKBPS;
/// <summary>
/// Approximate received bandwidth
/// </summary>
public float ReceivedBandwidthKBPS => _reliableChannel.ReceivedBandwidthKBPS;
private MessageChannel[] messageChannels;
private double time = 0.0;
// the reliable channel
private ReliableMessageChannel _reliableChannel;
public ReliableEndpoint()
{
time = DateTime.Now.GetTotalSeconds();
_reliableChannel = new ReliableMessageChannel() { TransmitCallback = this.transmitMessage, ReceiveCallback = this.receiveMessage };
messageChannels = new MessageChannel[]
{
_reliableChannel,
new UnreliableMessageChannel() { TransmitCallback = this.transmitMessage, ReceiveCallback = this.receiveMessage },
new UnreliableOrderedMessageChannel() { TransmitCallback = this.transmitMessage, ReceiveCallback = this.receiveMessage },
};
}
public ReliableEndpoint(uint index) : this()
{
Index = index;
}
/// <summary>
/// Reset the endpoint
/// </summary>
public void Reset()
{
for (int i = 0; i < messageChannels.Length; i++)
messageChannels[i].Reset();
}
/// <summary>
/// Update the endpoint with the current time
/// </summary>
public void Update()
{
Update(DateTime.Now.GetTotalSeconds());
}
/// <summary>
/// Manually step the endpoint forward by increment in seconds
/// </summary>
public void UpdateFastForward(double increment)
{
this.time += increment;
Update(this.time);
}
/// <summary>
/// Update the endpoint with a specific time value
/// </summary>
public void Update(double time)
{
this.time = time;
for (int i = 0; i < messageChannels.Length; i++)
messageChannels[i].Update(this.time);
}
/// <summary>
/// Call this when a datagram has been received over the network
/// </summary>
public void ReceivePacket(byte[] buffer, int bufferLength)
{
int channel = buffer[1];
messageChannels[channel].ReceivePacket(buffer, bufferLength);
}
/// <summary>
/// Send a message with the given QoS level
/// </summary>
public void SendMessage(byte[] buffer, int bufferLength, QosType qos)
{
messageChannels[(int)qos].SendMessage(buffer, bufferLength);
}
protected void receiveMessage(byte[] buffer, int length)
{
if (ReceiveCallback != null)
ReceiveCallback(buffer, length);
if (ReceiveExtendedCallback != null)
ReceiveExtendedCallback(Index, buffer, length);
}
protected void transmitMessage(byte[] buffer, int length)
{
if (TransmitCallback != null)
TransmitCallback(buffer, length);
if (TransmitExtendedCallback != null)
TransmitExtendedCallback(Index, buffer, length);
}
}
}