Пример #1
0
        public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
        {
            if (!(requestMessage.Type == Message.Message.MessageType.RequestFf1 && requestMessage.Command == Rpc.Commands.Quit.GetNr()))
            {
                throw new ArgumentException("Message content is wrong for this handler.");
            }
            Logger.Debug("Received QUIT message {0}.", requestMessage);

            lock (PeerBean.PeerStatusListeners)
            {
                foreach (var listener in PeerBean.PeerStatusListeners)
                {
                    listener.PeerFailed(requestMessage.Sender,
                                        new PeerException(PeerException.AbortCauseEnum.Shutdown, "shutdown"));
                }
            }
            if (requestMessage.IsUdp)
            {
                responder.ResponseFireAndForget();
            }
            else
            {
                responder.Response(CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok));
            }
        }
Пример #2
0
 public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
 {
     if (!(requestMessage.Type == Message.Message.MessageType.RequestFf1 &&
           requestMessage.Command == Rpc.Commands.Broadcast.GetNr()))
     {
         throw new ArgumentException("Message content is wrong for this handler.");
     }
     Logger.Debug("Received BROADCAST message: {0}.", requestMessage);
     BroadcastHandler.Receive(requestMessage);
     if (requestMessage.IsUdp)
     {
         responder.ResponseFireAndForget();
     }
     else
     {
         responder.Response(CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok));
     }
 }
Пример #3
0
 public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
 {
     if (!(requestMessage.Type == Message.Message.MessageType.RequestFf1
         && requestMessage.Command == Rpc.Commands.Broadcast.GetNr()))
     {
         throw new ArgumentException("Message content is wrong for this handler.");   
     }
     Logger.Debug("Received BROADCAST message: {0}.", requestMessage);
     BroadcastHandler.Receive(requestMessage);
     if (requestMessage.IsUdp)
     {
         responder.ResponseFireAndForget();
     }
     else
     {
         responder.Response(CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok));    
     }
 }
Пример #4
0
        public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
        {
            if (!(requestMessage.Type == Message.Message.MessageType.RequestFf1 && requestMessage.Command == Rpc.Commands.Quit.GetNr()))
            {
                throw new ArgumentException("Message content is wrong for this handler.");
            }
            Logger.Debug("Received QUIT message {0}.", requestMessage);

            lock (PeerBean.PeerStatusListeners)
            {
                foreach (var listener in PeerBean.PeerStatusListeners)
                {
                    listener.PeerFailed(requestMessage.Sender,
                        new PeerException(PeerException.AbortCauseEnum.Shutdown, "shutdown"));
                }
            }
            if (requestMessage.IsUdp)
            {
                responder.ResponseFireAndForget();
            }
            else
            {
                responder.Response(CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok));
            }
        }
Пример #5
0
        public override void HandleResponse(Message.Message requestMessage, PeerConnection peerConnection, bool sign, IResponder responder)
        {
            // server-side:
            // comes from DispatchHandler
            // IResponder now responds the result...

            if (!((requestMessage.Type == Message.Message.MessageType.RequestFf1 ||
                   requestMessage.Type == Message.Message.MessageType.Request1 ||
                   requestMessage.Type == Message.Message.MessageType.Request2 ||
                   requestMessage.Type == Message.Message.MessageType.Request3 ||
                   requestMessage.Type == Message.Message.MessageType.Request3) &&
                  requestMessage.Command == Rpc.Commands.Ping.GetNr()))
            {
                throw new ArgumentException("Request message type or command is wrong for this handler.");
            }
            Message.Message responseMessage;

            // probe
            if (requestMessage.Type == Message.Message.MessageType.Request3)
            {
                Logger.Debug("Respond to probing. Firing message to {0}.", requestMessage.Sender);
                responseMessage = CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok);

                if (requestMessage.IsUdp)
                {
                    ConnectionBean.Reservation.CreateAsync(1, 0).ContinueWith(t =>
                    {
                        if (!t.IsFaulted)
                        {
                            Logger.Debug("Fire UDP to {0}.", requestMessage.Sender);
                            var taskResponse = FireUdpAsync(requestMessage.Sender, t.Result,
                                                            ConnectionBean.ChannelServer.ChannelServerConfiguration);
                            Utils.Utils.AddReleaseListener(t.Result, taskResponse);
                        }
                        else
                        {
                            Utils.Utils.AddReleaseListener(t.Result);
                            Logger.Warn("Handling response for Request3 failed. (UDP) {0}", t.Exception);
                        }
                    });
                }
                else
                {
                    ConnectionBean.Reservation.CreateAsync(0, 1).ContinueWith(t =>
                    {
                        if (!t.IsFaulted)
                        {
                            Logger.Debug("Fire TCP to {0}.", requestMessage.Sender);
                            var taskResponse = FireTcpAsync(requestMessage.Sender, t.Result,
                                                            ConnectionBean.ChannelServer.ChannelServerConfiguration);
                            Utils.Utils.AddReleaseListener(t.Result, taskResponse);
                        }
                        else
                        {
                            Utils.Utils.AddReleaseListener(t.Result);
                            Logger.Warn("Handling response for Request3 failed. (TCP) {0}", t.Exception);
                        }
                    });
                }
            }
            // discover
            else if (requestMessage.Type == Message.Message.MessageType.Request2)
            {
                Logger.Debug("Respond to discovering. Found {0}.", requestMessage.Sender);
                responseMessage = CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok);
                responseMessage.SetNeighborSet(CreateNeighborSet(requestMessage.Sender));
            }
            // regular ping
            else if (requestMessage.Type == Message.Message.MessageType.Request1 ||
                     requestMessage.Type == Message.Message.MessageType.Request4)
            {
                Logger.Debug("Respond to regular ping from {0}.", requestMessage.Sender);
                // Test, if this is a broadcast message to ourselves.
                // If it is, do not reply.
                if (requestMessage.IsUdp &&
                    requestMessage.Sender.PeerId.Equals(PeerBean.ServerPeerAddress.PeerId) &&
                    requestMessage.Recipient.PeerId.Equals(Number160.Zero))
                {
                    Logger.Warn("Don't respond. We are on the same peer, you should make this call.");
                    responder.ResponseFireAndForget();
                }
                if (_enable)
                {
                    responseMessage = CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok);
                    if (_wait)
                    {
                        Thread.Sleep(WaitTime);
                    }
                }
                else
                {
                    Logger.Debug("Don't respond.");
                    // used for debugging
                    if (_wait)
                    {
                        Thread.Sleep(WaitTime);
                    }
                    return;
                }
                if (requestMessage.Type == Message.Message.MessageType.Request4)
                {
                    lock (_receivedBroadcastPingListeners)
                    {
                        foreach (IPeerReceivedBroadcastPing listener in _receivedBroadcastPingListeners)
                        {
                            listener.BroadcastPingReceived(requestMessage.Sender);
                        }
                    }
                }
            }
            else
            {
                // fire-and-forget if requestMessage.Type == MessageType.RequestFf1
                // we received a fire-and forget ping
                // this means we are reachable from the outside
                PeerAddress serverAddress = PeerBean.ServerPeerAddress;
                if (requestMessage.IsUdp)
                {
                    // UDP
                    PeerAddress newServerAddress = serverAddress.ChangeIsFirewalledUdp(false);
                    PeerBean.SetServerPeerAddress(newServerAddress);
                    lock (_reachableListeners)
                    {
                        foreach (IPeerReachable listener in _reachableListeners)
                        {
                            listener.PeerWellConnected(newServerAddress, requestMessage.Sender, false);
                        }
                    }
                    responseMessage = requestMessage;
                }
                else
                {
                    // TCP
                    PeerAddress newServerAddress = serverAddress.ChangeIsFirewalledTcp(false);
                    PeerBean.SetServerPeerAddress(newServerAddress);
                    lock (_reachableListeners)
                    {
                        foreach (IPeerReachable listener in _reachableListeners)
                        {
                            listener.PeerWellConnected(newServerAddress, requestMessage.Sender, true);
                        }
                    }
                    responseMessage = CreateResponseMessage(requestMessage, Message.Message.MessageType.Ok);
                }
            }
            responder.Response(responseMessage);
        }