private Task InitiateNewUser(PeerData peer, uint subnetworkIP) { return(Task.Run(() => { // pakiet dla nowego użytkownika UdpNewPeerPacket packet = new UdpNewPeerPacket( peer.Name, subnetworkIP, true, PeersRxSupport.Values.ToArray(), CurrentSubnetwork ); byte[] data = GetSerializedBytes <UdpNewPeerPacket>(packet); ReceivedConfirmation = new ManualResetEvent(false); NewPeer = peer; int retryCounter = 0; while (retryCounter++ < ConnectionRetries && !CancelToken.IsCancellationRequested) { UdpSupport.Send(data, data.Length, peer.GetSupportEndpoint()); if (ReceivedConfirmation.WaitOne(2000)) { break; } } NewPeer = null; if (retryCounter > ConnectionRetries) { throw new NewClientNotReachedException("Czas wywołania minął!", null); } if (!CancelToken.IsCancellationRequested) { return; } // pakiet dla pozostałych packet = new UdpNewPeerPacket( null, 0, false, new PeerData[1] { peer }, null ); Broadcast <UdpNewPeerPacket>(packet); })); }
private void ProcessUdpPacket(CommPacket packet, IPEndPoint ep) { PeerData peer = null; if (packet is UdpConnectingConfirmation) { AddPeerToDictionaries(NewPeer); if (ReceivedConfirmation != null) { ReceivedConfirmation.Set(); } } else if (packet is UdpNewPeerPacket) { UdpNewPeerPacket newPeerPacket = packet as UdpNewPeerPacket; if (newPeerPacket.RecipientIsNew && AwaitingExternalConnection) { AwaitingExternalConnection = false; AddPeersToDictionaries(newPeerPacket.Peers); StartDriver(newPeerPacket.SubnetworkData, newPeerPacket.SubnetworkIP, newPeerPacket.Name); OnConnected(this, null); UdpConnectingConfirmation confirmPacket = new UdpConnectingConfirmation(); SendPacket <UdpConnectingConfirmation>(confirmPacket, ep); Thread.Sleep(100); NegotiateKey(PeersRxSupport[ep], null); } else { if (newPeerPacket.Peers != null && !newPeerPacket.RecipientIsNew) { if (newPeerPacket.Peers.Length != 0) { AddPeerToDictionaries(newPeerPacket.Peers[0]); NegotiateKey(peer, null); } } } } else if (packet is UdpKeyNegotiationPacket) { if (!PeersRxSupport.TryGetValue(ep, out peer)) { peer = NewPeer; } NegotiateKey(peer, packet as UdpKeyNegotiationPacket); } else if (packet is UdpGoodbye) { RemovePeerFromDictionaries(PeersRxSupport[ep], "goodbye"); } else if (packet is UdpHeartbeat) { if (PeersRxSupport.TryGetValue(ep, out peer)) { peer.Heartbeat(); } } }