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 void AckHandler(UdpContext context) { var id = context.GetData <Guid>(); if (server.ackTasks.TryGetValue(id, out var src)) { src.TrySetResult(true); } }
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 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(); } } }