Ejemplo n.º 1
0
        private void OnPeerDisconnected(PeerDisconnected data)
        {
            Notifications.Enqueue(new PeerDisconnectedNotification(data.Peer));

            DataMap?.Handle(data);
            Sender?.Remove(data.Peer);
        }
Ejemplo n.º 2
0
        public void TriggerPeerDisconnected(DisconnectReason reason, DisconnectType disconnectType)
        {
            PeerDisconnected?.Invoke(this, new DisconnectEventArgs(reason, disconnectType));
            var task = Task.Delay(1000);

            task.Wait();
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            var disconnectedPeer = peers[peer.EndPoint];

            peers.Remove(peer.EndPoint);
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
Ejemplo n.º 4
0
 public void Handle(PeerDisconnected data)
 {
     context.Queue.Add(() =>
     {
         context.States.Handle(data);
         context.Bitfields.Handle(data);
     });
 }
Ejemplo n.º 5
0
 private void OnPeerDisconnected(PeerDisconnected @event)
 {
     // This event is catched by every ServerPeer instance, so if we have multiple endpoints listening, all of them will
     // try to remove the item from the dictionary.
     if (this.connectedPeers.Remove(@event.PeerContext.PeerId))
     {
         this.logger.LogDebug("Peer {PeerId} disconnected, removed from connectedPeers", @event.PeerContext.PeerId);
     }
 }
Ejemplo n.º 6
0
        public void Handle(PeerDisconnected data)
        {
            Bitfield bitfield;

            if (byPeer.TryGetValue(data.Peer, out bitfield))
            {
                ranking.Remove(bitfield);
            }
        }
Ejemplo n.º 7
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            var disconnectedPeer = masterPeer;

            if (masterPeer.Peer == peer)
            {
                masterPeer = null;
            }
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
Ejemplo n.º 8
0
 void OnPeerDisconnected(object sender, MultiHash peerId)
 {
     if (!otherPeers.TryGetValue(peerId.ToBase58(), out Peer peer))
     {
         peer = new Peer {
             Id = peerId
         };
     }
     PeerDisconnected?.Invoke(this, peer);
 }
Ejemplo n.º 9
0
Archivo: Peer.cs Proyecto: wyk125/AElf
        private void MessageReaderOnStreamClosed(object sender, EventArgs eventArgs)
        {
            Dispose();

            _logger?.Warn($"Peer connection has been terminated : {DistantNodeData}.");

            PeerDisconnected?.Invoke(this, new PeerDisconnectedArgs {
                Peer = this, Reason = DisconnectReason.StreamClosed
            });
        }
 /// <summary>
 /// Removes the specified peer from the list of connected peer.
 /// </summary>
 /// <param name="event">The event.</param>
 private void RemoveConnectedPeer(PeerDisconnected @event)
 {
     if (!_connectedPeers.Remove(@event.PeerContext.PeerId))
     {
         _logger.LogWarning("Cannot remove peer {PeerId}, peer not found", @event.PeerContext.PeerId);
     }
     else
     {
         _logger.LogDebug("Peer {PeerId} disconnected from PeerBehaviorManager.", @event.PeerContext.PeerId);
     }
 }
Ejemplo n.º 11
0
        private void Disconnected(IPeer peer)
        {
            // Remove listener to messages
            peer.MessageReceived -= OnMessageReceived;

            // Remove the peer
            _connectedPeers.Remove(peer.Id);

            // Invoke the event
            PeerDisconnected?.Invoke(peer);
        }
Ejemplo n.º 12
0
        protected virtual void RemovePeer(NetPeer peer)
        {
            if (!ConnectedClients.ContainsKey(peer.UniqueIdentifier))
            {
                return;
            }

            PeerDisconnected?.Invoke(this, ConnectedClients[peer.UniqueIdentifier]);
            lock (Locker)
                ConnectedClients.Remove(peer.UniqueIdentifier);
        }
Ejemplo n.º 13
0
 public void SendMessage(string message)
 {
     if (connection != null)
     {
         try {
             connection.Send(Encoding.ASCII.GetBytes(message + "\r\n"));
             Console.WriteLine("SENT MESSAGE:   " + message);
         } catch (Exception e) {
             Console.WriteLine(e);
             PeerDisconnected?.Invoke();
         }
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Removes the specified peer from the list of connected peer.
        /// </summary>
        /// <param name="event">The event.</param>
        private void OnPeerDisconnected(PeerDisconnected @event)
        {
            ConcurrentDictionary <string, IPeerContext> container = @event.PeerContext.Direction == PeerConnectionDirection.Inbound ? inboundPeers : outboundPeers;

            if (!container.TryRemove(@event.PeerContext.PeerId, out _))
            {
                _logger.LogWarning("Cannot remove peer {PeerId}, peer not found", @event.PeerContext.PeerId);
            }
            else
            {
                _logger.LogDebug("Peer {PeerId} disconnected.", @event.PeerContext.PeerId);
            }
        }
Ejemplo n.º 15
0
 public async void SendDataAsync(byte[] data)
 {
     await Task.Run(() =>
     {
         try
         {
             OpenStream();
             netStream.Write(data, 0, data.Length);
         }
         catch (Exception ex)
         {
             PeerDisconnected?.Invoke(ex.ToString(), EventArgs.Empty);
         }
     });
 }
Ejemplo n.º 16
0
        /// <summary>
        ///   Remove and close all connection to the peer ID.
        /// </summary>
        /// <param name="id">
        ///   The ID of a <see cref="Peer"/> to remove.
        /// </param>
        /// <returns>
        ///   <b>true</b> if a connection was removed; otherwise, <b>false</b>.
        /// </returns>
        public bool Remove(MultiHash id)
        {
            if (!connections.TryRemove(Key(id), out List <PeerConnection> conns))
            {
                return(false);
            }
            foreach (var conn in conns)
            {
                conn.RemotePeer.ConnectedAddress = null;
                conn.Dispose();
            }

            PeerDisconnected?.Invoke(this, id);
            return(true);
        }
Ejemplo n.º 17
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            if (MasterPeer == null || MasterPeer.Peer != peer)
            {
                activeConnections.Remove(peer.EndPoint);
                if (activeConnections.Count == 0)
                {
                    DroppedAllConnections?.Invoke();
                }
                return;
            }
            var disconnectedPeer = MasterPeer;

            masterPeer = null;
            activeConnections.Remove(peer.EndPoint);
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
Ejemplo n.º 18
0
        protected virtual void HandleDisconnect(int connectionID)
        {
            if (NetLogFilter.logDebug)
            {
                Debug.LogFormat("Connection ID: {0} Disconnected", connectionID);
            }
            connectedPeers.TryGetValue(connectionID, out Peer peer);
            if (peer == null)
            {
                return;
            }

            peer.Dispose();

            connectedPeers.Remove(peer.connectionID);

            PeerDisconnected?.Invoke(peer);
        }
Ejemplo n.º 19
0
        private async void StartCheckPeerHearbeats()
        {
            while (!_cancel.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));

                foreach (var peerHeartbeatDeadline in _peerHeartbeatDeadlines.Where(peerHeartbeatDeadline => peerHeartbeatDeadline.Value.IsExpired).ToArray())
                {
                    Deadline tmp;
                    _peerHeartbeatDeadlines.TryRemove(peerHeartbeatDeadline.Key, out tmp);

                    foreach (var handler in PeerDisconnected?.GetInvocationList().ToArray() ?? new Delegate[0])
                    {
                        ((Action <PeerIdentity>)handler)?.Invoke(peerHeartbeatDeadline.Key);
                    }
                }
            }
        }
Ejemplo n.º 20
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            if (MasterPeer == null || !Equals(MasterPeer.PeerEndPoint, peer.EndPoint))
            {
                if (peers.TryGetValue(peer.EndPoint, out var peerManager))
                {
                    PeerDisconnected?.Invoke(peerManager);
                }
                peers.Remove(peer.EndPoint);
                if (peers.Count == 0)
                {
                    DroppedAllConnections?.Invoke();
                }
                return;
            }
            var disconnectedPeer = MasterPeer;

            masterPeer = null;
            peers.Remove(peer.EndPoint);
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
Ejemplo n.º 21
0
 public void Listen()
 {
     while (ListeningToConnection)
     {
         byte[] buffer = new byte[1024];
         try {
             string response = Encoding.ASCII.GetString(buffer, 0, connection.Receive(buffer));
             Console.WriteLine("RESPONSE: " + response);
             if (response == "")
             {
                 PeerDisconnected?.Invoke();
                 break;
             }
             response = response.Replace("\r\n", "");
             string[] sections = response.Split('|');
             NewResponse?.Invoke(sections[0], sections[1]);
         } catch {
             PeerDisconnected?.Invoke();
             break;
         }
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        ///   Remove a connection.
        /// </summary>
        /// <param name="connection">
        ///   The <see cref="PeerConnection"/> to remove.
        /// </param>
        /// <returns>
        ///   <b>true</b> if the connection was removed; otherwise, <b>false</b>.
        /// </returns>
        /// <remarks>
        ///    The <paramref name="connection"/> is removed from the list of
        ///    connections and is closed.
        /// </remarks>
        public bool Remove(PeerConnection connection)
        {
            if (connection == null)
            {
                return(false);
            }

            if (!_connections.TryGetValue(Key(connection.RemotePeer), out var originalConns))
            {
                connection.Dispose();
                return(false);
            }

            if (!originalConns.Contains(connection))
            {
                connection.Dispose();
                return(false);
            }

            var newConns = new List <PeerConnection>();

            newConns.AddRange(originalConns.Where(c => c != connection));
            _connections.TryUpdate(Key(connection.RemotePeer), newConns, originalConns);

            connection.Dispose();
            if (newConns.Count > 0)
            {
                var last = newConns.Last();
                last.RemotePeer.ConnectedAddress = last.RemoteAddress;
            }
            else
            {
                connection.RemotePeer.ConnectedAddress = null;
                PeerDisconnected?.Invoke(this, connection.RemotePeer.Id);
            }

            return(true);
        }
Ejemplo n.º 23
0
        private void receiveingTCPPacket()
        {
            while (true)
            {
                var    stream       = tcpClient.GetStream();
                byte[] buffer       = new byte[2048];
                int    byteReceived = 0;
                do
                {
                    try
                    {
                        byteReceived += stream.Read(buffer, byteReceived, buffer.Length);
                    }
                    catch
                    {
                    }
                } while (stream.DataAvailable);

                if (byteReceived <= 0)
                {
                    Dispatcher.BeginInvokeOnMainThread(() =>
                    {
                        System.Threading.Thread.Sleep(100);
                        PeerDisconnected?.Invoke();
                    });
                    break;
                }
                else
                {
                    string inStr = aesDecryptBase64(Encoding.UTF8.GetString(buffer).Trim('\0'), PSK);
                    if (inStr != "")
                    {
                        PeerDataComing?.Invoke(inStr.Trim('\0'));
                    }
                }
            }
        }
Ejemplo n.º 24
0
        public void DisconnectPeer(Peer peer, Exception ex)
        {
            if (ex != null)
            {
                logger.Debug(ex, $"Remote peer failed: {peer.RemoteEndPoint}");
            }

            PeerDisconnected?.Invoke(peer);

            if (peer.IsIncoming)
            {
                Interlocked.Decrement(ref this.incomingCount);
            }

            this.badPeers.Add(peer.RemoteEndPoint); //TODO

            this.unconnectedPeersLock.Do(() =>
                                         this.unconnectedPeers.Remove(peer.RemoteEndPoint));
            this.pendingPeers.TryRemove(peer);
            this.connectedPeers.TryRemove(peer);

            peer.OnDisconnect -= DisconnectPeer;
            peer.Dispose();
        }
Ejemplo n.º 25
0
 public void TriggerPeerDisconnected()
 {
     PeerDisconnected?.Invoke(this, new DisconnectEventArgs(DisconnectReason.TooManyPeers, DisconnectType.Local));
     //var task = Task.Delay(1000);
     //task.Wait();
 }
Ejemplo n.º 26
0
 public void PeerDisconnect()
 {
     PeerDisconnected?.Invoke();
 }
Ejemplo n.º 27
0
 public void TriggerPeerDisconnected(DisconnectReason reason, DisconnectType disconnectType)
 {
     PeerDisconnected?.Invoke(this, new DisconnectEventArgs(reason, disconnectType));
 }
Ejemplo n.º 28
0
 protected void OnPeerDisconnected(Peer peer)
 {
     PeerDisconnected?.Invoke(this, peer);
 }
Ejemplo n.º 29
0
 public void Handle(PeerDisconnected data)
 {
     byPeer.Remove(data.Peer);
 }
Ejemplo n.º 30
0
 internal void RaisePeerDisconnected(PeerDisconnectedEventArgs args)
 {
     Mode.HandlePeerDisconnected(args.Peer);
     PeerDisconnected?.InvokeAsync(this, args);
 }