private void PendingConnectionsTimeoutPulse() { while (IsActive) { for (var i = 0; i < PendingConnections.Count; i++) { if ((DateTime.Now - PendingConnections[i].ConnectionTimestamp).TotalSeconds >= PendingConnectionTimeout) { lock (_lockObj) PendingConnections.RemoveAt(i); } } Thread.Sleep(5000); } }
private void AcceptCallback(IAsyncResult res) { if (!IsActive) { return; } var newSock = ChannelSocket.EndAccept(res); Peer.Peer newPeer; if (IsMainChannel) { newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId(), SocketIdentity.GenerateGuid()), newSock, BufferSize, this); newPeer.OnPeerDisconnected += (o, e2) => { OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer)); }; newPeer.Receive(); AcceptPeer(newPeer); } else { newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId()), newSock, BufferSize, this); PendingConnections.Add(new PendingPeerConnection(newPeer.Identity.Guid, newPeer)); newPeer.OnPeerDisconnected += (o, e2) => { OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer)); }; newPeer.Receive(); newPeer.OnPeerRelocationRequest += (sender, e) => { if (PendingConnections.FirstOrDefault(pc => pc.Guid == e.PeerGuid && !pc.IsCancelled) != null) { AcceptPeer(newPeer); OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer)); lock (_lockObj) PendingConnections.Remove(PendingConnections.First(pc => pc.Guid == e.PeerGuid)); } }; } ChannelSocket.BeginAccept(AcceptCallback, null); }