public void HandleConnect(UdpContext context) { var peer = context.GetData <PeerInfo>() !; var remote = context.RemoteEndPoint; var peers = context.Peers; foreach (var item in peers) { if ((DateTime.UtcNow - item.Value.CreateTime).TotalSeconds > 10) { peers.TryRemove(item); } } peers[peer.Id] = peer; peer.OuterEP = PeerEP.ParsePeerEPFromIPEP(remote); _logger.LogInformation($"receive peer {peer.Id} from {peer.OuterEP.ToIPEP()}"); var sendbytes = MessagePackSerializer.Serialize(peers); server.msgs.Enqueue(new UdpMsg { Data = sendbytes, Ep = remote }); }
public async void ConnectHandShake(UdpContext context) { var data = context.GetData <ConnectHandshakeDto>(); var ep = data.Ep; data.Info.OuterEP = PeerEP.ParsePeerEPFromIPEP(context.RemoteEndPoint); server.connectionDic[data.Info.OuterEP] = (ep, DateTime.UtcNow); await P2PServer.SendDataReliableAsync((int)CallMethods.PeerConnectionRequest, data.Info, ep.ToIPEP(), server.ackTasks, server.msgs, server.timeoutData); _logger.LogInformation("send handshake data"); }
public void TestIllegalConnectionHandshake() { var moq = new Mock <P2PServer>(() => new P2PServer(5500)); moq.Setup(s => s.StartSendTask()).Returns(Task.CompletedTask); var obj = moq.Object; var msgs = obj.msgs; var handler = new ServerHandlers(obj, new Logger <ServerHandlers>(LoggerFactory.Create(b => b.AddConsole()))); var bytes = P2PServer.CreateUdpRequestBuffer(0, Guid.Empty, new ConnectionReplyDto { Acc = true, Ep = PeerEP.ParsePeerEPFromIPEP(new System.Net.IPEndPoint(100, 100)) }); handler.HolePunchRequest(new UdpContext(bytes.AsMemory()[20..].ToArray(), new(),
public void HolePunchRequest(UdpContext context) { var reply = context.GetData <ConnectionReplyDto>() !; var ep = reply.Ep; var rep = PeerEP.ParsePeerEPFromIPEP(context.RemoteEndPoint); _logger.LogInformation("handshake reply get"); var c = server.connectionDic.TryRemove(ep, out var t); if (c && t.Item1 == rep) { _ = P2PServer.SendDataReliableAsync((int)CallMethods.ConnectionRequestCallback, reply.Acc, ep.ToIPEP(), server.ackTasks, server.msgs, server.timeoutData).AsTask(); if (reply.Acc) { _ = P2PServer.SendDataReliableAsync((int)CallMethods.StartPunching, "", ep.ToIPEP(), server.ackTasks, server.msgs, server.timeoutData).AsTask(); _ = P2PServer.SendDataReliableAsync((int)CallMethods.StartPunching, "", rep.ToIPEP(), server.ackTasks, server.msgs, server.timeoutData).AsTask(); } } }