public void MesssageHeadersCanBeRead() { var msg1 = new MqttMessageBase(MessageType.Connect) { Duplicate = false, QualityOfService = QualityOfService.AtMostOnce, Retain = false }; Assert.AreEqual(MessageType.Connect, msg1.MessageType); Assert.AreEqual(QualityOfService.AtMostOnce, msg1.QualityOfService); Assert.AreEqual(false, msg1.Duplicate); Assert.AreEqual(false, msg1.Retain); var msg2 = new MqttMessageBase(MessageType.ConnAck) { Duplicate = true, QualityOfService = QualityOfService.ExactlyOnce, Retain = true }; Assert.AreEqual(MessageType.ConnAck, msg2.MessageType); Assert.AreEqual(QualityOfService.ExactlyOnce, msg2.QualityOfService); Assert.AreEqual(true, msg2.Duplicate); Assert.AreEqual(true, msg2.Retain); }
public void CannotCreateFromIncompleteFrame() { var msgBuilder = new MqttReceivedMessageBuilder(); msgBuilder.Append(0x28); msgBuilder.Append(0x02); msgBuilder.Append(0xFF); Assert.IsFalse(msgBuilder.FrameComplete); MqttMessageBase msg = null; try { msg = msgBuilder.GetMessage(); } catch (MessageFrameException) { } catch (Exception) { Assert.Fail("Invalid Exception type."); } Assert.IsNull(msg, "GetMessage succeeded but should have failed."); }
/// <summary> /// Saves a message into the store and begins the response timer. If the message is not pulled from the store /// before the response timer ends, the MessageTimeout event is fired. /// </summary> /// <param name="message">The message sent to the remote endpoint. I.e. Publish, Subscribe, etc.</param> /// <param name="eventData">Client defined data associated with the message.</param> /// <param name="clientUid">The socket connection context.</param> public void Add(IMqttMessage message, object eventData, string clientUid) { var storeData = new MessageStoreData { Message = message, MessageId = MqttMessageBase.GetMessageIdOrDefault(message), EventData = eventData, ResponseTimer = new TimeoutTimer(MqttProtocolInformation.Settings.NetworkTimeout), ClientUid = clientUid }; lock (_lock) { _pendingMessageData.Add(storeData); } storeData.ResponseTimer.TimeOutData = storeData; storeData.ResponseTimer.Timeout += ResponseTimerOnTimeout; storeData.ResponseTimer.Start(); }
/// <summary> /// Finds the original message for the given response, pulls it out of the store, stops the response timeout /// and returns the client defined event data /// </summary> /// <param name="responseMessage">The expected response message for the stored message. I.e. PubAck, SubAck, etc.</param> /// <param name="clientUid">The connection context.</param> /// <returns></returns> public object Remove(IMqttMessage responseMessage, string clientUid) { return(Remove(responseMessage.MessageType, MqttMessageBase.GetMessageIdOrDefault(responseMessage), clientUid)); }
public void SendMessageAsync(IMqttMessage msg, object eventData, string clientUid) { Logger.LogMessage("Protocol", LogLevel.Verbose, "SendMessageAsync(" + msg.MessageType + ")"); var args = new SocketEventArgs { MessageToSend = msg, ClientUid = clientUid }; // If we expect a response, push the event data on our stack and retrieve it with the response if (args.MessageToSend.ExpectedResponse != MessageType.None) { _messageStore.Add(args.MessageToSend, eventData, clientUid); } args.OnOperationComplete((eventArgs) => { MessageType messageType = eventArgs.MessageToSend.MessageType; string exceptionText = eventArgs.SocketException == null ? "Success." : "Error: " + eventArgs.SocketException.ToString(); Logger.LogMessage("Protocol", LogLevel.Verbose, "SendMessageAsync(" + messageType + ") completed callback. " + exceptionText); if (eventArgs.SocketException != null) { // Clean up pending message queue _messageStore.Remove(args.MessageToSend.ExpectedResponse, MqttMessageBase.GetMessageIdOrDefault(args.MessageToSend), clientUid); } OnSendMessageAsyncCompleted(clientUid, eventArgs.MessageToSend, eventArgs.SocketException); if (messageType == MessageType.Connect && eventArgs.SocketException != null) { FireConnectComplete(new MqttNetEventArgs { Message = args.MessageToSend, Exception = eventArgs.SocketException, AdditionalErrorInfo = eventArgs.AdditionalErrorInfo, EventData = eventData, ClientUid = clientUid }); } else if (messageType == MessageType.Disconnect) { CloseConnection(clientUid); FireSendMessageComplete(new MqttNetEventArgs { Message = args.MessageToSend, Exception = eventArgs.SocketException, AdditionalErrorInfo = eventArgs.AdditionalErrorInfo, EventData = eventData, ClientUid = clientUid }); } else if (messageType == MessageType.Subscribe && eventArgs.SocketException != null) { FireSubscribeMessageComplete(new MqttNetEventArgs { Message = args.MessageToSend, Exception = eventArgs.SocketException, AdditionalErrorInfo = eventArgs.AdditionalErrorInfo, EventData = eventData, ClientUid = clientUid }); } else if (args.MessageToSend.ExpectedResponse == MessageType.None || eventArgs.SocketException != null) { FireSendMessageComplete(new MqttNetEventArgs { Message = args.MessageToSend, Exception = eventArgs.SocketException, AdditionalErrorInfo = eventArgs.AdditionalErrorInfo, EventData = eventData, ClientUid = clientUid }); } }); Socket.WriteAsync(args); }