Exemple #1
0
        internal void Complete(MqttCommand command)
        {
            System.Diagnostics.Debug.WriteLine("FINISHED: {0} {1}", command.CommandMessage, command.MessageId);

            switch (command.CommandMessage)
            {
                case CommandMessage.SUBSCRIBE:
                    ActiveSubscriptions.Current.Add(ClientId, (command as Subscribe).Subscriptions);
                    break;
                case CommandMessage.PUBLISH:
                    foreach (string client in ActiveSubscriptions.Current.Publish(ClientId, (command as Publish).Topic, (command as Publish).Message))
                    {
                        Manager.Send(client, new Publish((command as Publish).Topic, (command as Publish).Message));
                    }
                    break;
                case CommandMessage.UNSUBSCRIBE:
                    ActiveSubscriptions.Current.Remove(ClientId, (command as Unsubscribe).Topics);
                    break;
                case CommandMessage.PINGREQ:
                    Send(new PingReq());
                    break;
                case CommandMessage.DISCONNECT:
                    ActiveSubscriptions.Current.Remove(ClientId);
                    Connection.Disconnect();
                    Manager.Disconnect(this);
                    break;
            }
        }
Exemple #2
0
        public override Task Start(MqttCommand msg, Action<MqttCommand> release)
        {
            if (release == null)
            {
                release = p => { };
            }

            switch (msg.Header.QualityOfService)
            {
                case QualityOfService.AtMostOnce:
                    return Task.Factory.StartNew(() => release(msg), TaskCreationOptions.LongRunning);
                case QualityOfService.AtLeastOnce:
                    return Send(new PubAck(msg.MessageId))
                         .ContinueWith(task =>
                            release(msg),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
                case QualityOfService.ExactlyOnce:
                    return Send(new PubRec(msg.MessageId))
                        .ContinueWith(task =>
                            WaitFor(CommandMessage.PUBREL, msg.MessageId, TimeSpan.FromSeconds(60)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            Send(new PubComp(msg.MessageId)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            release(msg),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
                default:
                    throw new InvalidOperationException("Unknown QoS");
            }
        }
Exemple #3
0
        void ICommandWriter.Send(NetworkConnection connection, MqttCommand command)
        {
            byte[] bytes = command.ToByteArray();

            System.Diagnostics.Debug.WriteLine("SEND {0} => {1}", command, BitConverter.ToString(bytes).Replace("-", " "));

            connection.Stream.Write(bytes, 0, bytes.Length);
        }
Exemple #4
0
        public override Task Start(MqttCommand command, Action<MqttCommand> onSuccess)
        {
            if (onSuccess == null)
            {
                onSuccess = (MqttCommand c) => { };
            }

            return Send(new ConnAck())
                .ContinueWith((task) =>
                    onSuccess(command),
                    TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
        }
Exemple #5
0
        private Task ProcessSubscription(MqttCommand msg, Action<MqttCommand> release)
        {
            SubAck ack = new SubAck(msg.MessageId);
            Subscribe subCmd = msg as Subscribe;
            foreach (Subscription sub in subCmd.Subscriptions)
            {
                ack.Grants.Add(QualityOfService.AtMostOnce);
            }

            return Send(ack)
                .ContinueWith((task) =>
                    Task.Factory.StartNew(() => release(msg)),
                    TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
        }
Exemple #6
0
        public override Task Start(MqttCommand command, Action<MqttCommand> onSuccess)
        {
            if (onSuccess == null)
            {
                onSuccess = c => { };
            }

            return Send(command)
                .ContinueWith(task =>
                    WaitFor(CommandMessage.CONNACK, MessageId.Any, TimeSpan.FromSeconds(30)),
                    TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                .ContinueWith(task =>
                    onSuccess(command),
                    TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
        }
Exemple #7
0
        public Task Send(MqttCommand command)
        {
            var tcs = new TaskCompletionSource<object>();
            try
            {
                _writer.Send(_connection, command);
                tcs.SetResult(null);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }

            return tcs.Task;
        }
Exemple #8
0
        public Task Send(MqttCommand command)
        {
            var tcs = new TaskCompletionSource<object>();
            try
            {
                EnqueueResponse(command);
                tcs.SetResult(null);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }

            return tcs.Task;
        }
Exemple #9
0
        public void Deliver(MqttCommand command)
        {
            lock (_desireLock)
            {
                Desire desire;

                if (_desireCache.TryGetAndRemove(command.CommandMessage, command.MessageId, out desire))
                {
                    desire.Fulfilled(command);
                }
                else
                {
                    _unlovedCommands.Add(command);
                }
            }
        }
Exemple #10
0
        private Task ProcessSubscription(MqttCommand msg, Action<MqttCommand> release)
        {
            var ack = new SubAck(msg.MessageId);
            var subCmd = msg as Subscribe;
            if (subCmd == null)
            {
                throw new InvalidOperationException("Subscribe operationg expects Subscribe command");
            }

            foreach (Subscription sub in subCmd.Subscriptions)
            {
                ack.Grants.Add(QualityOfService.AtMostOnce);
            }

            return Send(ack)
                .ContinueWith(task =>
                    Task.Factory.StartNew(() => release(msg)),
                    TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
        }
Exemple #11
0
        public override Task Start(MqttCommand msg, Action<MqttCommand> release)
        {
            if (release == null)
            {
                release = (MqttCommand p) => { };
            }

            switch (msg.Header.QualityOfService)
            {
                case QualityOfService.AtLeastOnce:
                    return ProcessSubscription(msg, release);
                case QualityOfService.AtMostOnce:
                case QualityOfService.ExactlyOnce:
                    var tcs = new TaskCompletionSource<MqttCommand>();
                    tcs.SetException(new ProtocolException(msg.CommandMessage));
                    return tcs.Task;
                default:
                    throw new InvalidOperationException("Unknown QoS");
            }
        }
Exemple #12
0
 internal void Complete(MqttCommand command)
 {
     switch (command.CommandMessage)
     {
         case CommandMessage.SUBSCRIBE:
             var sub = command as Subscribe;
             if (sub == null)
             {
                 throw new ArgumentException("Message declared itself as Subscribe but was not of type Subscribe", "command");
             }
             ActiveSubscriptions.Current.Add(ClientId, sub.Subscriptions);
             break;
         case CommandMessage.PUBLISH:
             var pub = command as Publish;
             if (pub == null)
             {
                 throw new ArgumentException("Message declared itself as Publish but was not of type Publish", "command");
             }
             foreach (var client in ActiveSubscriptions.Current.Publish(ClientId, pub.Topic, pub.Message))
             {
                 Manager.Send(client, new Publish(pub.Topic, pub.Message));
             }
             break;
         case CommandMessage.UNSUBSCRIBE:
             var unsub = command as Unsubscribe;
             if (unsub == null)
             {
                 throw new ArgumentException("Message declared itself as Unsubscribe but was not of type Unsubscribe", "command");
             }
             ActiveSubscriptions.Current.Remove(ClientId, unsub.Topics);
             break;
         case CommandMessage.PINGREQ:
             Send(new PingReq());
             break;
         case CommandMessage.DISCONNECT:
             ActiveSubscriptions.Current.Remove(ClientId);
             Connection.Disconnect();
             Manager.Disconnect(this);
             break;
     }
 }
Exemple #13
0
        public override Task Start(MqttCommand command, Action<MqttCommand> onSuccess)
        {
            if (onSuccess == null)
            {
                onSuccess = p => { };
            }

            switch (command.Header.QualityOfService)
            {
                case QualityOfService.AtMostOnce:
                    return Send(command)
                        .ContinueWith(task =>
                            onSuccess(command),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
                case QualityOfService.AtLeastOnce:
                    return Send(command)
                        .ContinueWith(task =>
                            WaitFor(CommandMessage.PUBACK, command.MessageId, TimeSpan.FromSeconds(60)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            onSuccess(command),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
                case QualityOfService.ExactlyOnce:
                    return Send(command)
                        .ContinueWith(task =>
                            WaitFor(CommandMessage.PUBREC, command.MessageId, TimeSpan.FromSeconds(60)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            Send(new PubRel(command.MessageId)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            WaitFor(CommandMessage.PUBCOMP, command.MessageId, TimeSpan.FromSeconds(60)),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning)
                        .ContinueWith(task =>
                            onSuccess(command),
                            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
                default:
                    throw new InvalidOperationException("Unknown QoS");
            }
        }
Exemple #14
0
 public Task StartNew(MqttCommand command, Action<MqttCommand> onSuccess)
 {
     switch (command.CommandMessage)
     {
         case CommandMessage.CONNECT:
             var connFlow = new ConnectReceiveFlow(this);
             return connFlow.Start(command, onSuccess);
         case CommandMessage.PUBLISH:
             var pubFlow = new PublishReceiveFlow(this);
             return pubFlow.Start(command, onSuccess);
         case CommandMessage.SUBSCRIBE:
             var subFlow = new SubscribeFlow(this);
             return subFlow.Start(command, onSuccess);
         case CommandMessage.PINGREQ:
             var pingFlow = new PingSendFlow(this);
             return pingFlow.Start(command, onSuccess);
         default:
             var tcs = new TaskCompletionSource<object>();
             tcs.SetException(new InvalidOperationException("Unhandled command type"));
             return tcs.Task;
     }
 }
Exemple #15
0
 public override Task Start(MqttCommand msg, Action<MqttCommand> release)
 {
     if (release == null)
     {
         release = p => { };
     }
     switch (msg.Header.QualityOfService)
     {
         case QualityOfService.AtLeastOnce:
             return Send(msg)
                 .ContinueWith(task =>
                     WaitFor(CommandMessage.SUBACK, msg.MessageId, TimeSpan.FromSeconds(30)))
                 .ContinueWith(task =>
                     release(msg),
                     TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.LongRunning);
         case QualityOfService.AtMostOnce:
         case QualityOfService.ExactlyOnce:
             var tcs = new TaskCompletionSource<MqttCommand>();
             tcs.SetException(new ProtocolException(msg.CommandMessage));
             return tcs.Task;
         default:
             throw new InvalidOperationException("Unknown QoS");
     }
 }
Exemple #16
0
 private void Notify(MqttCommand command)
 {
     UnsolicitedMessageCallback callback = OnUnsolicitedMessage;
     if (callback != null)
     {
         callback(this, new ClientCommandEventArgs(command));
     }
 }
Exemple #17
0
 protected Task Send(MqttCommand message)
 {
     return _manager.Send(message);
 }
Exemple #18
0
 public abstract Task Start(MqttCommand command, Action<MqttCommand> onSuccess);
Exemple #19
0
 internal void Send(MqttCommand command)
 {
     ICommandWriter writer = new CommandWriter();
     writer.Send(Connection, command);
 }
Exemple #20
0
 public PingReceived(MqttCommand cmd, NamedConnection connection)
 {
     _command = cmd;
     _connection = connection;
 }
Exemple #21
0
        private void EnqueueResponse(MqttCommand command)
        {
            MessageReceivedCallback recv = OnMessageReceived;

            switch (command.CommandMessage)
            {
                case Types.CommandMessage.CONNECT:
                    if (recv != null)
                    {
                        recv(this, new ClientCommandEventArgs(new ConnAck()));
                    }
                    break;
                case Types.CommandMessage.DISCONNECT:
                    break;
                default:
                    throw new NotImplementedException();
            }
        }
Exemple #22
0
 public PublishReceive(MqttCommand cmd, NamedConnection connection)
 {
     _command = cmd;
     _connection = connection;
 }
 public CommandRead(MqttCommand command, NamedConnection connection)
 {
     Command = command;
     Connection = connection;
 }
Exemple #24
0
 public Task Send(MqttCommand command)
 {
     return _network.Send(command);
 }
Exemple #25
0
 public SubscribeReceive(MqttCommand cmd, NamedConnection connection)
 {
     _command = cmd;
     _connection = connection;
 }
Exemple #26
0
 public void Fulfilled(MqttCommand command)
 {
     _fulfilled(command);
 }
Exemple #27
0
 internal void Send(MqttCommand command)
 {
     ICommandWriter writer = BrokerFactory.Get<ICommandWriter>();
     writer.Send(Connection, command);
 }
Exemple #28
0
 internal Task Send(MqttCommand message)
 {
     return _broker.Send(message);
 }
 public MessageReceivedEventArgs(string clientId, MqttCommand command)
 {
     ClientId = clientId;
     Command = command;
 }
 public ClientCommandEventArgs(MqttCommand command)
 {
     Command = command;
 }