📄 rudpstack.cs
字号:
channel = RUDPPacketChannel.BandwidthResponse01;
break;
case 0x9b:
channel = RUDPPacketChannel.Bandwidth02;
break;
case 0x9c:
channel = RUDPPacketChannel.BandwidthResponse02;
break;
default:
throw new Exception("Unsupported channel type");
}
packetId = BinaryHelper.ReadInt(payload, index);
index += 4;
advertisedWindowSize = BinaryHelper.ReadInt(payload, index);
index += 4;
int count = BinaryHelper.ReadInt(payload, index);
index += 4;
slot1 = null;
slot2 = null;
slot3 = null;
slot4 = null;
if (num2 > 0)
{
startPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
endPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
slot1 = new SACKSlot(startPacketId, endPacketId);
}
else
{
index += 8;
}
if (num2 > 1)
{
startPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
endPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
slot2 = new SACKSlot(startPacketId, endPacketId);
}
else
{
index += 8;
}
if (num2 > 2)
{
startPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
endPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
slot3 = new SACKSlot(startPacketId, endPacketId);
}
else
{
index += 8;
}
if (num2 > 3)
{
startPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
endPacketId = BinaryHelper.ReadInt(payload, index);
index += 4;
slot4 = new SACKSlot(startPacketId, endPacketId);
}
else
{
index += 8;
}
packetPayload = new byte[count];
if (count > 0)
{
Buffer.BlockCopy(payload, index, packetPayload, 0, count);
}
}
internal static void ForceFragmentsSending(int controlThreadId)
{
if (controlThreadId > -1)
{
_controlThreadInformations[controlThreadId]._protocolControlEvent.Set();
}
}
internal static PhysicalSocket GetInstance(IPEndPoint endPoint)
{
PhysicalSocket socket2;
lock (_physicals)
{
PhysicalSocket physical;
int port;
if (!endPoint.Equals(new IPEndPoint(IPAddress.Any, 0)))
{
if (_physicals.TryGetValue(endPoint, ref physical))
{
return physical;
}
physical = new PhysicalSocket();
physical.Bind(endPoint);
RegisterPhysicalSocket(physical);
_physicals.Add(endPoint, physical);
goto Label_009B;
}
physical = new PhysicalSocket();
Label_005D:
port = new Random().Next(0x7fff);
endPoint = new IPEndPoint(LocalIPAddress((ProtocolType) 4), port);
try
{
physical.Bind(endPoint);
}
catch
{
goto Label_005D;
}
RegisterPhysicalSocket(physical);
_physicals.Add(endPoint, physical);
Label_009B:
socket2 = physical;
}
return socket2;
}
private static void HandleACKs(RUDPSocket rudp, SACKSlot slot1, SACKSlot slot2, SACKSlot slot3, SACKSlot slot4)
{
if (slot1 == null)
{
return;
}
int endPacketId = slot1.EndPacketId;
if (slot4 != null)
{
endPacketId = slot4.EndPacketId;
}
else if (slot3 != null)
{
endPacketId = slot3.EndPacketId;
}
else if (slot2 != null)
{
endPacketId = slot2.EndPacketId;
}
List<RUDPOutgoingPacket> list = new List<RUDPOutgoingPacket>();
RUDPOutgoingPacket packet = null;
double maxValue = double.MaxValue;
rudp._outgoingPacketsLock.EnterReadLock();
long microSeconds = HiResTimer.MicroSeconds;
try
{
for (int i = 0; i < rudp._outgoingPackets.get_Count(); i++)
{
RUDPOutgoingPacket packet2 = rudp._outgoingPackets.get_Item(i);
if (packet2.PacketId > endPacketId)
{
goto Label_01D1;
}
if (!packet2.IsACKed)
{
if (((slot4 != null) && (packet2.PacketId >= slot4.StartPacketId)) && (packet2.PacketId <= slot4.EndPacketId))
{
if (packet2.Retransmission < 1)
{
packet = packet2;
maxValue = Math.Min(maxValue, (double) (microSeconds - packet.TSFirstSend));
}
list.Add(packet2);
}
else if (((slot3 != null) && (packet2.PacketId >= slot3.StartPacketId)) && (packet2.PacketId <= slot3.EndPacketId))
{
if (packet2.Retransmission < 1)
{
packet = packet2;
maxValue = Math.Min(maxValue, (double) (microSeconds - packet.TSFirstSend));
}
list.Add(packet2);
}
else if (((slot2 != null) && (packet2.PacketId >= slot2.StartPacketId)) && (packet2.PacketId <= slot2.EndPacketId))
{
if (packet2.Retransmission < 1)
{
packet = packet2;
maxValue = Math.Min(maxValue, (double) (microSeconds - packet.TSFirstSend));
}
list.Add(packet2);
}
else if ((packet2.PacketId >= slot1.StartPacketId) && (packet2.PacketId <= slot1.EndPacketId))
{
if (packet2.Retransmission < 1)
{
packet = packet2;
maxValue = Math.Min(maxValue, (double) (microSeconds - packet.TSFirstSend));
}
list.Add(packet2);
}
}
}
}
finally
{
rudp._outgoingPacketsLock.ExitReadLock();
}
Label_01D1:
if (packet == null)
{
maxValue = rudp.RTT;
}
if (maxValue < 1)
{
maxValue = 1;
}
for (int j = 0; j < list.get_Count(); j++)
{
RUDPOutgoingPacket packet3 = list.get_Item(j);
SetPacketACKed(rudp, packet3, maxValue);
}
}
internal static void HandleException(Exception exception, params object[] args)
{
string text = "";
foreach (object obj2 in args)
{
text = text + " - " + obj2.ToString();
}
if (text.Length > 0)
{
Console.WriteLine(string.Concat(new object[] { "CriticalError :", exception.Message, '(', text, ")\n", exception.StackTrace }));
}
else
{
Console.WriteLine(string.Concat(new object[] { "CriticalError :", exception.Message, '\n', exception.StackTrace }));
}
}
private static void HandlePacket(PhysicalSocket physical, IPEndPoint sender, byte[] connectionId, RUDPPacketChannel channel, int packetId, int advertisedWindowSize, SACKSlot slot1, SACKSlot slot2, SACKSlot slot3, SACKSlot slot4, byte[] payload)
{
RUDPSocket socket = null;
if ((channel == RUDPPacketChannel.Ping) || (channel == RUDPPacketChannel.PingRendezVous))
{
socket = HandlePing(physical, sender, packetId, channel);
if (socket == null)
{
return;
}
}
if (socket == null)
{
physical._connectedRDUPsLock.EnterReadLock();
physical._connectedRDUPs.TryGetValue(sender, ref socket);
physical._connectedRDUPsLock.ExitReadLock();
}
RUDPSocket socket2 = null;
physical._connectedIdsLock.EnterReadLock();
physical._connectedIds.TryGetValue(connectionId, ref socket2);
physical._connectedIdsLock.ExitReadLock();
if (socket2 == null)
{
if (socket == null)
{
return;
}
physical._connectedIdsLock.EnterWriteLock();
physical._connectedIds.Add(connectionId, socket);
physical._connectedIdsLock.ExitWriteLock();
}
else if (((socket == null) && (socket2 != null)) && !socket2._remoteEndPoint.Equals(sender))
{
socket._NATEndPointsHistory.Add(new RemoteMappingChangePoint(DateTime.Now, socket));
physical._connectedRDUPsLock.EnterWriteLock();
physical._connectedRDUPs.Remove(socket._remoteEndPoint);
physical._connectedRDUPs.Add(sender, socket);
socket._remoteEndPoint = sender;
physical._connectedRDUPsLock.ExitWriteLock();
}
if (channel == RUDPPacketChannel.TearDown)
{
byte[] rudpPayload = MakePacketPayload(socket, -1, RUDPPacketChannel.ACK, new SACKSlot(packetId, packetId), null, null, null, null, 0, 0);
SocketSendACK(socket, physical, sender, rudpPayload);
PayloadManager.Deallocate(RUDPPacketChannel.ACK, rudpPayload);
}
if (socket != null)
{
socket._controlWindow.OnReceiveAdvertisedWindow(advertisedWindowSize);
HandleACKs(socket, slot1, slot2, slot3, slot4);
if (channel != RUDPPacketChannel.ACK)
{
if (packetId < 0)
{
if (channel == RUDPPacketChannel.Bandwidth01)
{
PushPacketToSend(socket, false, RUDPPacketChannel.BandwidthResponse01, null, 0, 0);
}
else if (channel == RUDPPacketChannel.Bandwidth02)
{
PushPacketToSend(socket, false, RUDPPacketChannel.BandwidthResponse02, payload, 0, 8);
}
else
{
if (channel == RUDPPacketChannel.BandwidthResponse01)
{
socket._bandwidthResponse01TS = HiResTimer.MicroSeconds;
}
else if (channel == RUDPPacketChannel.BandwidthResponse02)
{
long microSeconds = HiResTimer.MicroSeconds;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -