Beispiel #1
0
#pragma warning disable VSTHRD100 // Avoid async void methods
        /// <summary>
        ///   Raised when a connection is established to a remote peer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="connection"></param>
        /// <remarks>
        ///   Sends the hello message to the remote peer.  The message contains
        ///   all topics that are of interest to the local peer.
        /// </remarks>
        private async void Swarm_ConnectionEstablished(object sender, PeerConnection connection)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            if (_localTopics.Count == 0)
            {
                return;
            }

            try
            {
                var hello = new PubSubMessage
                {
                    Subscriptions = _localTopics.Values
                                    .Select(topic => new Subscription
                    {
                        Subscribe = true,
                        Topic     = topic
                    })
                                    .ToArray()
                };
                await SendAsync(hello, new[] { connection.RemotePeer }, CancellationToken.None)
                .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _log.Warn("Sending hello message failed", e);
            }
        }
Beispiel #2
0
        /// <inheritdoc />
        public async Task LeaveTopicAsync(string topic, CancellationToken cancel)
        {
            _localTopics.TryRemove(topic, out _);
            var msg = new PubSubMessage
            {
                Subscriptions = new[]
                {
                    new Subscription
                    {
                        Topic     = topic,
                        Subscribe = false
                    }
                }
            };

            try
            {
                var peers = SwarmService.KnownPeers.Where(p => p.ConnectedAddress != null);
                await SendAsync(msg, peers, cancel).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _log.Warn("Leave topic failed.", e);
            }
        }
Beispiel #3
0
        private Task SendAsync(PubSubMessage msg, IEnumerable <Peer> peers, CancellationToken cancel)
        {
            // Get binary representation
            byte[] bin;
            using (var ms = new MemoryStream())
            {
                Serializer.SerializeWithLengthPrefix(ms, msg, PrefixStyle.Base128);
                bin = ms.ToArray();
            }

            return(Task.WhenAll(peers.Select(p => SendAsync(bin, p, cancel))));
        }
Beispiel #4
0
        /// <inheritdoc />
        public Task PublishAsync(PublishedMessage message, CancellationToken cancel)
        {
            if (_tracker.RecentlySeen(message.MessageId))
            {
                return(Task.CompletedTask);
            }

            // Find a set of peers that are interested in the topic(s).
            // Exclude author and sender
            var peers = message.Topics
                        .SelectMany(topic => RemoteTopics.GetPeers(topic))
                        .Where(peer => peer != message.Sender)
                        .Where(peer => peer != message.Forwarder);

            // Forward the message.
            var forward = new PubSubMessage
            {
                PublishedMessages = new[] { message }
            };

            return(SendAsync(forward, peers, cancel));
        }