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; } }
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"); } }
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); }
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); }
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); }
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); }
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; }
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; }
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); } } }
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); }
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"); } }
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; } }
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"); } }
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; } }
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"); } }
private void Notify(MqttCommand command) { UnsolicitedMessageCallback callback = OnUnsolicitedMessage; if (callback != null) { callback(this, new ClientCommandEventArgs(command)); } }
protected Task Send(MqttCommand message) { return _manager.Send(message); }
public abstract Task Start(MqttCommand command, Action<MqttCommand> onSuccess);
internal void Send(MqttCommand command) { ICommandWriter writer = new CommandWriter(); writer.Send(Connection, command); }
public PingReceived(MqttCommand cmd, NamedConnection connection) { _command = cmd; _connection = connection; }
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(); } }
public PublishReceive(MqttCommand cmd, NamedConnection connection) { _command = cmd; _connection = connection; }
public CommandRead(MqttCommand command, NamedConnection connection) { Command = command; Connection = connection; }
public Task Send(MqttCommand command) { return _network.Send(command); }
public SubscribeReceive(MqttCommand cmd, NamedConnection connection) { _command = cmd; _connection = connection; }
public void Fulfilled(MqttCommand command) { _fulfilled(command); }
internal void Send(MqttCommand command) { ICommandWriter writer = BrokerFactory.Get<ICommandWriter>(); writer.Send(Connection, command); }
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; }