/// <summary> /// Parse bytes for a CONNACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>CONNACK message instance</returns> public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; MqttMsgConnack msg = new MqttMsgConnack(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] ... set session present flag ... msg.sessionPresent = (buffer[CONN_ACK_FLAGS_BYTE_OFFSET] & SESSION_PRESENT_FLAG_MASK) != 0x00; } // ...and set return code from broker msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET]; return(msg); }
/// <summary> /// Parse bytes for a CONNACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="socket">Socket connected to the broker</param> /// <returns>CONNACK message instance</returns> public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, Socket socket) { byte[] buffer; MqttMsgConnack msg = new MqttMsgConnack(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(socket); buffer = new byte[remainingLength]; // read bytes from socket... socket.Receive(buffer); // ...and set return code from broker msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET]; return msg; }
/// <summary> /// Parse bytes for a CONNACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>CONNACK message instance</returns> public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, IMqttNetworkChannel channel) { byte[] buffer; MqttMsgConnack msg = new MqttMsgConnack(); // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); // ...and set return code from broker msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET]; return(msg); }
public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { MqttMsgConnack mqttMsgConnack = new MqttMsgConnack(); if (protocolVersion == (byte)4 && ((uint)fixedHeaderFirstByte & 15U) > 0U) { throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } byte[] buffer = new byte[MqttMsgBase.decodeRemainingLength(channel)]; channel.Receive(buffer); if (protocolVersion == (byte)4) { mqttMsgConnack.sessionPresent = ((uint)buffer[0] & 1U) > 0U; } mqttMsgConnack.returnCode = buffer[1]; return(mqttMsgConnack); }
/// <summary> /// Send CONNACK message to the client (connection accepted or not) /// </summary> /// <param name="returnCode">Return code for CONNACK message</param> /// <param name="connect">CONNECT message with all client information</param> public void Connack(byte returnCode, MqttMsgConnect connect) { this.lastCommTime = 0; // create CONNACK message and ... MqttMsgConnack connack = new MqttMsgConnack(); connack.ReturnCode = returnCode; // ... send it to the client this.Send(connack); // connection accepted, start keep alive thread checking if (connack.ReturnCode == MqttMsgConnack.CONN_ACCEPTED) { this.ClientId = connect.ClientId; this.CleanSession = connect.CleanSession; this.WillFlag = connect.WillFlag; this.WillTopic = connect.WillTopic; this.WillMessage = connect.WillMessage; this.WillQosLevel = connect.WillQosLevel; this.keepAlivePeriod = connect.KeepAlivePeriod * 1000; // convert in ms // broker has a tolerance of 1.5 specified keep alive period this.keepAlivePeriod += (this.keepAlivePeriod / 2); // start thread for checking keep alive period timeout this.keepAliveThread = new Thread(this.KeepAliveThread); this.keepAliveThread.Name = KEEP_ALIVE_THREAD; this.keepAliveThread.Start(); this.IsConnected = true; } // connection refused, close TCP/IP channel else { this.Close(); } }
/// <summary> /// Parse bytes for a CONNACK message /// </summary> /// <param name="fixedHeaderFirstByte">First fixed header byte</param> /// <param name="protocolVersion">Protocol Version</param> /// <param name="channel">Channel connected to the broker</param> /// <returns>CONNACK message instance</returns> public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel) { byte[] buffer; MqttMsgConnack msg = new MqttMsgConnack(); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] check flag bits if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS) throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits); } // get remaining length and allocate buffer int remainingLength = MqttMsgBase.decodeRemainingLength(channel); buffer = new byte[remainingLength]; // read bytes from socket... channel.Receive(buffer); if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1) { // [v3.1.1] ... set session present flag ... msg.sessionPresent = (buffer[CONN_ACK_FLAGS_BYTE_OFFSET] & SESSION_PRESENT_FLAG_MASK) != 0x00; } // ...and set return code from broker msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET]; return msg; }