📄 conenathandler.cs
字号:
namespace NCindy.Protocol.RUDP.NATTraversal
{
using System;
using System.Collections.Generic;
using System.Net;
public sealed class ConeNATHandler : NATHandler
{
private List<IPEndPoint> AddRemoteEndPoint(Dictionary<int, List<IPEndPoint>> dictionary, int port, IPEndPoint endPoint)
{
List<IPEndPoint> list = dictionary.get_Item(port);
if (list == null)
{
list = new List<IPEndPoint>();
}
if (!list.Contains(endPoint))
{
list.Add(endPoint);
}
dictionary.set_Item(port, list);
return list;
}
public override bool CanHandleNAT(List<NATHistoryPoint> h)
{
bool flag = false;
int num = 0;
Dictionary<int, List<IPEndPoint>> dictionary = new Dictionary<int, List<IPEndPoint>>();
List<NATHistoryPoint> list = new List<NATHistoryPoint>();
List<NATHistoryPoint>.Enumerator enumerator = h.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
NATHistoryPoint point = enumerator.get_Current();
list.Add(point);
}
}
finally
{
enumerator.Dispose();
}
list.Reverse();
Dictionary<int, NATHistoryPoint> dictionary2 = new Dictionary<int, NATHistoryPoint>();
bool flag2 = true;
List<NATHistoryPoint>.Enumerator enumerator2 = list.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
NATHistoryPoint point2 = enumerator2.get_Current();
IPEndPoint peerViewOfLocalEndPoint = point2.PeerViewOfLocalEndPoint;
if (peerViewOfLocalEndPoint != null)
{
int port = peerViewOfLocalEndPoint.Port;
NATHistoryPoint point4 = dictionary2.get_Item(point2.SocketHandle);
if (point2 is NewSocketPoint)
{
List<IPEndPoint> list2 = this.AddRemoteEndPoint(dictionary, port, point2.RemoteEndPoint);
flag |= list2.get_Count() > 1;
num = Math.Max(num, dictionary.get_Count());
}
else if (point2 is SocketClosePoint)
{
this.RemoveRemoteEndPoint(dictionary, port, point2.RemoteEndPoint);
}
else if (point2 is LocalMappingChangePoint)
{
if (point4 != null)
{
int num3 = point4.PeerViewOfLocalEndPoint.Port;
this.RemoveRemoteEndPoint(dictionary, num3, point4.RemoteEndPoint);
}
List<IPEndPoint> list3 = this.AddRemoteEndPoint(dictionary, port, point2.RemoteEndPoint);
flag |= list3.get_Count() > 1;
num = Math.Max(num, dictionary.get_Count());
}
else if (point2 is RemoteMappingChangePoint)
{
if (point4 != null)
{
this.RemoveRemoteEndPoint(dictionary, port, point4.RemoteEndPoint);
}
List<IPEndPoint> list4 = this.AddRemoteEndPoint(dictionary, port, point2.RemoteEndPoint);
flag |= list4.get_Count() > 1;
num = Math.Max(num, dictionary.get_Count());
}
dictionary2.set_Item(point2.SocketHandle, point2);
flag2 = num < 3;
if (!flag2)
{
return flag2;
}
}
}
}
finally
{
enumerator2.Dispose();
}
return flag2;
}
private void RemoveRemoteEndPoint(Dictionary<int, List<IPEndPoint>> dictionary, int port, IPEndPoint endPoint)
{
List<IPEndPoint> list = dictionary.get_Item(port);
list.Remove(endPoint);
if (list.get_Count() == 0)
{
dictionary.Remove(port);
}
}
public override List<IPEndPoint> TargetEndPoints(List<NATHistoryPoint> history)
{
List<IPEndPoint> list = new List<IPEndPoint>();
List<NATHistoryPoint>.Enumerator enumerator = history.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
IPEndPoint peerViewOfLocalEndPoint = enumerator.get_Current().PeerViewOfLocalEndPoint;
if (peerViewOfLocalEndPoint != null)
{
if ((list.get_Count() == 0) || !peerViewOfLocalEndPoint.Equals(list.get_Item(0)))
{
list.Add(peerViewOfLocalEndPoint);
}
if (list.get_Count() == 2)
{
return list;
}
}
}
}
finally
{
enumerator.Dispose();
}
return list;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -