Example #1
0
        /// <summary>
        /// Sends a message that indicates this peer is about to quit. This is an RPC.
        /// </summary>
        /// <param name="remotePeer">The remote peer to send this request.</param>
        /// <param name="shutdownBuilder">Used for the sign and force TCP flag. Set if the message should be signed.</param>
        /// <param name="channelCreator">The channel creator that creates connections.</param>
        /// <returns>The future response message.</returns>
        public Task<Message.Message> QuitAsync(PeerAddress remotePeer, ShutdownBuilder shutdownBuilder,
            ChannelCreator channelCreator)
        {
            var message = CreateRequestMessage(remotePeer, Rpc.Commands.Quit.GetNr(),
                Message.Message.MessageType.RequestFf1);
            if (shutdownBuilder.IsSign)
            {
                message.SetPublicKeyAndSign(shutdownBuilder.KeyPair);
            }

            var tcsResponse = new TaskCompletionSource<Message.Message>(message);
            var requestHandler = new RequestHandler(tcsResponse, PeerBean, ConnectionBean, shutdownBuilder);
            Logger.Debug("Send QUIT message {0}.", message);
            if (!shutdownBuilder.IsForceTcp)
            {
                return requestHandler.FireAndForgetUdpAsync(channelCreator);
            }
            return requestHandler.SendTcpAsync(channelCreator);
        }
Example #2
0
 public Task<Message.Message> SendAsync(PeerAddress remotePeer, BroadcastBuilder broadcastBuilder,
     ChannelCreator channelCreator, IConnectionConfiguration configuration)
 {
     var message = CreateRequestMessage(remotePeer, Rpc.Commands.Broadcast.GetNr(),
         Message.Message.MessageType.RequestFf1);
     message.SetIntValue(broadcastBuilder.HopCounter);
     message.SetKey(broadcastBuilder.MessageKey);
     if (broadcastBuilder.DataMap != null)
     {
         message.SetDataMap(new DataMap(broadcastBuilder.DataMap));
     }
     var tcsResponse = new TaskCompletionSource<Message.Message>(message);
     var requestHandler = new RequestHandler(tcsResponse, PeerBean, ConnectionBean, configuration);
     if (!broadcastBuilder.IsUdp)
     {
         return requestHandler.SendTcpAsync(channelCreator);
     }
     else
     {
         return requestHandler.FireAndForgetUdpAsync(channelCreator);
     }
 }
Example #3
0
        private TaskCompletionSource<Message.Message> Send(Message.Message message, IConnectionConfiguration configuration,
            ChannelCreator channelCreator)
        {
            var tcsResponse = new TaskCompletionSource<Message.Message>(message);
            tcsResponse.Task.ContinueWith(taskResponse =>
            {
                if (!taskResponse.IsFaulted)
                {
                    var response = taskResponse.Result;
                    if (response != null)
                    {
                        var neighborSet = response.NeighborsSet(0);
                        if (neighborSet != null)
                        {
                            foreach (var neighbor in neighborSet.Neighbors)
                            {
                                lock (PeerBean.PeerStatusListeners)
                                {
                                    foreach (var listener in PeerBean.PeerStatusListeners)
                                    {
                                        listener.PeerFound(neighbor, response.Sender, null);
                                    }
                                }
                            }
                        }
                    }
                }
            });

            var requestHandler = new RequestHandler(tcsResponse, PeerBean, ConnectionBean, configuration);

            if (!configuration.IsForceTcp)
            {
                requestHandler.SendUdpAsync(channelCreator);
            }
            else
            {
                requestHandler.SendTcpAsync(channelCreator);
            }
            // .NET-specific: Return TCS instead of Task. It's actually the same TCS that is provided with
            // the RequestHandler c'tor
            return tcsResponse;
        }