📄 p2psessionfinder.cs
字号:
enumerator.Dispose();
}
list.Clear();
lock (this.allRemoteEndpoints)
{
list.AddRange(this.allRemoteEndpoints);
}
}
}
public PunchingResult SendPunchingRequests(ShareContent.OtherSideOption option)
{
List<IPEndPoint> list;
AllEndPoints endPorints = option.EndPorints;
bool flag = ShareContentType.UDP == (Configuration.FixedClientSetting.ShareContentType & ShareContentType.UDP);
if (ShareContentType.TCP == (Configuration.FixedClientSetting.ShareContentType & ShareContentType.TCP))
{
Guid guid = new Guid(option.Id);
PunchingResult result = P2PManager.Instace.TryTcpConnect(this.RemotePeer, guid.ToByteArray());
if (result != null)
{
return result;
}
}
if (!flag)
{
return new PunchingResult(null, null, false, TransportMode.Block);
}
PeerTokenCodec codec = new PeerTokenCodec(this);
this.innerSession.PacketDecoder = codec;
this.innerSession.PacketEncoder = codec;
this.innerSession.ObjectReceived += new EventHandler<SessionEventArgs>(this, (IntPtr) this.OnObjectReceived);
this.allRemoteEndpoints.Clear();
this.allRemoteEndpoints.AddRange(endPorints.InnerEndPoints);
if ((endPorints.OuterEndPoint != null) && this.allRemoteEndpoints.Contains(endPorints.OuterEndPoint))
{
this.allRemoteEndpoints.Add(endPorints.OuterEndPoint);
}
AsyncUdpSession innerSession = this.innerSession;
lock (this.allRemoteEndpoints)
{
list = new List<IPEndPoint>(this.allRemoteEndpoints);
}
int num = (int) Math.Ceiling(40 / ((double) list.get_Count()));
for (int i = 0; i < num; i++)
{
List<IPEndPoint>.Enumerator enumerator = list.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
IPEndPoint point = enumerator.get_Current();
if (!this.isAckRecevied)
{
lock (this.innerSession)
{
innerSession.RemoteEndPoint = point;
innerSession.Send("P2PV3:" + this.localPeer.PeerToken);
innerSession.Send(this.localPeer);
}
if (log.IsInfoEnabled)
{
log.Info(string.Format("Send to {0}, content: {1}", point, this.localPeer.PeerToken));
}
}
if (this.CanStop)
{
goto Label_0204;
}
Thread.Sleep(100);
}
}
finally
{
enumerator.Dispose();
}
Label_0204:
list.Clear();
lock (this.allRemoteEndpoints)
{
list.AddRange(this.allRemoteEndpoints);
}
}
if (!this.CanStop)
{
return new PunchingResult(null, null, false, TransportMode.Block);
}
if (!this.isSender)
{
this.SendAckResponse();
}
IPEndPoint iep = innerSession.LocalEndPoint;
innerSession.Close();
this.localPeer.AddUsableEndPoint(iep);
return new PunchingResult(this.localPeer, this.remotePeer, true, (TransportMode) Math.Min((int) this.remotePeer.Mode, (int) this.localPeer.Mode));
}
internal bool CanStop
{
get
{
return this.canStop;
}
set
{
this.canStop = value;
}
}
public Peer LocalPeer
{
get
{
return this.localPeer;
}
}
public Peer RemotePeer
{
get
{
return this.remotePeer;
}
}
private class PeerTokenCodec : IPacketDecoder, IPacketEncoder
{
private readonly P2PSessionFinder parent;
private readonly Peer rawLocalPeer;
private readonly Peer remotePeer;
public PeerTokenCodec(P2PSessionFinder parent)
{
this.parent = parent;
this.remotePeer = parent.RemotePeer;
this.rawLocalPeer = parent.LocalPeer;
}
public object Decode(ISession session, IPacket packet)
{
if (packet.Content.Remaining >= 0x20)
{
string text = Encoding.UTF8.GetString(packet.Content.GetInnerByteArray(), packet.Content.Position, packet.Content.Remaining);
if (string.IsNullOrEmpty(text))
{
return null;
}
packet.Content.Position = packet.Content.Limit;
if (P2PSessionFinder.log.IsInfoEnabled)
{
P2PSessionFinder.log.Info(string.Format("Receive Message: {0}", text));
}
if (text.Length > 0x20)
{
try
{
string[] textArray = text.Split(new char[] { ':' });
if (textArray.Length == 2)
{
this.remotePeer.Mode = (TransportMode) Enum.Parse(typeof(TransportMode), textArray[0]);
}
else
{
return null;
}
}
catch (Exception)
{
return null;
}
}
lock (this.remotePeer)
{
if (text.IndexOf(this.remotePeer.PeerToken) >= 0)
{
this.remotePeer.AddUsableEndPoint(packet.EndPoint);
if (!this.parent.allRemoteEndpoints.Contains(packet.EndPoint))
{
lock (this.parent.allRemoteEndpoints)
{
if (!this.parent.allRemoteEndpoints.Contains(packet.EndPoint))
{
this.parent.allRemoteEndpoints.Add(packet.EndPoint);
}
}
}
return this.remotePeer;
}
if (text.IndexOf(this.rawLocalPeer.PeerToken) >= 0)
{
return this.rawLocalPeer;
}
if (P2PSessionFinder.log.IsDebugEnabled)
{
P2PSessionFinder.log.Debug("P2P Packet received,but MD5 not match.");
}
}
}
return null;
}
public IPacket Encode(ISession session, object obj)
{
if (obj is Peer)
{
Peer peer = (Peer) obj;
return new DefaultPacket(Encoding.UTF8.GetBytes(peer.PeerToken));
}
if (obj is string)
{
return new DefaultPacket(Encoding.UTF8.GetBytes((string) obj));
}
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -