You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

626 lines
23 KiB

  1. using MQTTnet.Exceptions;
  2. using MQTTnet.Packets;
  3. using MQTTnet.Protocol;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using MQTTnet.Adapter;
  8. namespace MQTTnet.Serializer
  9. {
  10. public class MqttPacketSerializer : IMqttPacketSerializer
  11. {
  12. private const int FixedHeaderSize = 1;
  13. public MqttProtocolVersion ProtocolVersion { get; set; } = MqttProtocolVersion.V311;
  14. public ArraySegment<byte> Serialize(MqttBasePacket packet)
  15. {
  16. if (packet == null) throw new ArgumentNullException(nameof(packet));
  17. using (var stream = new MemoryStream(128))
  18. {
  19. // Leave enough head space for max header size (fixed + 4 variable remaining length = 5 bytes)
  20. stream.Seek(5, SeekOrigin.Begin);
  21. var fixedHeader = SerializePacket(packet, stream);
  22. var remainingLength = (int)stream.Length - 5;
  23. var remainingLengthBuffer = MqttPacketWriter.EncodeRemainingLength(remainingLength);
  24. var headerSize = FixedHeaderSize + remainingLengthBuffer.Count;
  25. var headerOffset = 5 - headerSize;
  26. // Position cursor on correct offset on beginining of array (has leading 0x0)
  27. stream.Seek(headerOffset, SeekOrigin.Begin);
  28. stream.WriteByte(fixedHeader);
  29. stream.Write(remainingLengthBuffer.Array, remainingLengthBuffer.Offset, remainingLengthBuffer.Count);
  30. #if NET461 || NET452 || NETSTANDARD2_0
  31. var buffer = stream.GetBuffer();
  32. return new ArraySegment<byte>(buffer, headerOffset, (int)stream.Length - headerOffset);
  33. #else
  34. if (stream.TryGetBuffer(out var segment))
  35. {
  36. return new ArraySegment<byte>(segment.Array, headerOffset, segment.Count - headerOffset);
  37. }
  38. var buffer = stream.ToArray();
  39. return new ArraySegment<byte>(buffer, headerOffset, buffer.Length - headerOffset);
  40. #endif
  41. }
  42. }
  43. private byte SerializePacket(MqttBasePacket packet, Stream stream)
  44. {
  45. switch (packet)
  46. {
  47. case MqttConnectPacket connectPacket: return Serialize(connectPacket, stream);
  48. case MqttConnAckPacket connAckPacket: return Serialize(connAckPacket, stream);
  49. case MqttDisconnectPacket _: return SerializeEmptyPacket(MqttControlPacketType.Disconnect);
  50. case MqttPingReqPacket _: return SerializeEmptyPacket(MqttControlPacketType.PingReq);
  51. case MqttPingRespPacket _: return SerializeEmptyPacket(MqttControlPacketType.PingResp);
  52. case MqttPublishPacket publishPacket: return Serialize(publishPacket, stream);
  53. case MqttPubAckPacket pubAckPacket: return Serialize(pubAckPacket, stream);
  54. case MqttPubRecPacket pubRecPacket: return Serialize(pubRecPacket, stream);
  55. case MqttPubRelPacket pubRelPacket: return Serialize(pubRelPacket, stream);
  56. case MqttPubCompPacket pubCompPacket: return Serialize(pubCompPacket, stream);
  57. case MqttSubscribePacket subscribePacket: return Serialize(subscribePacket, stream);
  58. case MqttSubAckPacket subAckPacket: return Serialize(subAckPacket, stream);
  59. case MqttUnsubscribePacket unsubscribePacket: return Serialize(unsubscribePacket, stream);
  60. case MqttUnsubAckPacket unsubAckPacket: return Serialize(unsubAckPacket, stream);
  61. default: throw new MqttProtocolViolationException("Packet type invalid.");
  62. }
  63. }
  64. public MqttBasePacket Deserialize(ReceivedMqttPacket receivedMqttPacket)
  65. {
  66. if (receivedMqttPacket == null) throw new ArgumentNullException(nameof(receivedMqttPacket));
  67. var controlPacketType = receivedMqttPacket.FixedHeader >> 4;
  68. if (controlPacketType < 1 || controlPacketType > 14)
  69. {
  70. throw new MqttProtocolViolationException($"The packet type is invalid ({controlPacketType}).");
  71. }
  72. switch ((MqttControlPacketType)controlPacketType)
  73. {
  74. case MqttControlPacketType.Connect: return DeserializeConnect(receivedMqttPacket.Body);
  75. case MqttControlPacketType.ConnAck: return DeserializeConnAck(receivedMqttPacket.Body);
  76. case MqttControlPacketType.Disconnect: return new MqttDisconnectPacket();
  77. case MqttControlPacketType.Publish: return DeserializePublish(receivedMqttPacket);
  78. case MqttControlPacketType.PubAck: return DeserializePubAck(receivedMqttPacket.Body);
  79. case MqttControlPacketType.PubRec: return DeserializePubRec(receivedMqttPacket.Body);
  80. case MqttControlPacketType.PubRel: return DeserializePubRel(receivedMqttPacket.Body);
  81. case MqttControlPacketType.PubComp: return DeserializePubComp(receivedMqttPacket.Body);
  82. case MqttControlPacketType.PingReq: return new MqttPingReqPacket();
  83. case MqttControlPacketType.PingResp: return new MqttPingRespPacket();
  84. case MqttControlPacketType.Subscribe: return DeserializeSubscribe(receivedMqttPacket.Body);
  85. case MqttControlPacketType.SubAck: return DeserializeSubAck(receivedMqttPacket.Body);
  86. case MqttControlPacketType.Unsubscibe: return DeserializeUnsubscribe(receivedMqttPacket.Body);
  87. case MqttControlPacketType.UnsubAck: return DeserializeUnsubAck(receivedMqttPacket.Body);
  88. default: throw new MqttProtocolViolationException($"Packet type ({controlPacketType}) not supported.");
  89. }
  90. }
  91. private static MqttBasePacket DeserializeUnsubAck(MqttPacketBodyReader body)
  92. {
  93. ThrowIfBodyIsEmpty(body);
  94. return new MqttUnsubAckPacket
  95. {
  96. PacketIdentifier = body.ReadUInt16()
  97. };
  98. }
  99. private static MqttBasePacket DeserializePubComp(MqttPacketBodyReader body)
  100. {
  101. ThrowIfBodyIsEmpty(body);
  102. return new MqttPubCompPacket
  103. {
  104. PacketIdentifier = body.ReadUInt16()
  105. };
  106. }
  107. private static MqttBasePacket DeserializePubRel(MqttPacketBodyReader body)
  108. {
  109. ThrowIfBodyIsEmpty(body);
  110. return new MqttPubRelPacket
  111. {
  112. PacketIdentifier = body.ReadUInt16()
  113. };
  114. }
  115. private static MqttBasePacket DeserializePubRec(MqttPacketBodyReader body)
  116. {
  117. ThrowIfBodyIsEmpty(body);
  118. return new MqttPubRecPacket
  119. {
  120. PacketIdentifier = body.ReadUInt16()
  121. };
  122. }
  123. private static MqttBasePacket DeserializePubAck(MqttPacketBodyReader body)
  124. {
  125. ThrowIfBodyIsEmpty(body);
  126. return new MqttPubAckPacket
  127. {
  128. PacketIdentifier = body.ReadUInt16()
  129. };
  130. }
  131. private static MqttBasePacket DeserializeUnsubscribe(MqttPacketBodyReader body)
  132. {
  133. ThrowIfBodyIsEmpty(body);
  134. var packet = new MqttUnsubscribePacket
  135. {
  136. PacketIdentifier = body.ReadUInt16(),
  137. };
  138. while (!body.EndOfStream)
  139. {
  140. packet.TopicFilters.Add(body.ReadStringWithLengthPrefix());
  141. }
  142. return packet;
  143. }
  144. private static MqttBasePacket DeserializeSubscribe(MqttPacketBodyReader body)
  145. {
  146. ThrowIfBodyIsEmpty(body);
  147. var packet = new MqttSubscribePacket
  148. {
  149. PacketIdentifier = body.ReadUInt16()
  150. };
  151. while (!body.EndOfStream)
  152. {
  153. packet.TopicFilters.Add(new TopicFilter(
  154. body.ReadStringWithLengthPrefix(),
  155. (MqttQualityOfServiceLevel)body.ReadByte()));
  156. }
  157. return packet;
  158. }
  159. private static MqttBasePacket DeserializePublish(ReceivedMqttPacket receivedMqttPacket)
  160. {
  161. var body = receivedMqttPacket.Body;
  162. ThrowIfBodyIsEmpty(body);
  163. var fixedHeader = new ByteReader(receivedMqttPacket.FixedHeader);
  164. var retain = fixedHeader.Read();
  165. var qualityOfServiceLevel = (MqttQualityOfServiceLevel)fixedHeader.Read(2);
  166. var dup = fixedHeader.Read();
  167. var topic = body.ReadStringWithLengthPrefix();
  168. ushort? packetIdentifier = null;
  169. if (qualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
  170. {
  171. packetIdentifier = body.ReadUInt16();
  172. }
  173. var packet = new MqttPublishPacket
  174. {
  175. PacketIdentifier = packetIdentifier,
  176. Retain = retain,
  177. Topic = topic,
  178. Payload = body.ReadRemainingData().ToArray(),
  179. QualityOfServiceLevel = qualityOfServiceLevel,
  180. Dup = dup
  181. };
  182. return packet;
  183. }
  184. private static MqttBasePacket DeserializeConnect(MqttPacketBodyReader body)
  185. {
  186. ThrowIfBodyIsEmpty(body);
  187. var protocolName = body.ReadStringWithLengthPrefix();
  188. MqttProtocolVersion protocolVersion;
  189. if (protocolName == "MQTT")
  190. {
  191. var protocolLevel = body.ReadByte();
  192. if (protocolLevel != 4)
  193. {
  194. throw new MqttProtocolViolationException($"Protocol level ({protocolLevel}) not supported for MQTT 3.1.1.");
  195. }
  196. protocolVersion = MqttProtocolVersion.V311;
  197. }
  198. else if (protocolName == "MQIsdp")
  199. {
  200. var protocolLevel = body.ReadByte();
  201. if (protocolLevel != 3)
  202. {
  203. throw new MqttProtocolViolationException($"Protocol level ({protocolLevel}) not supported for MQTT 3.1.");
  204. }
  205. protocolVersion = MqttProtocolVersion.V310;
  206. }
  207. else
  208. {
  209. throw new MqttProtocolViolationException($"Protocol name ({protocolName}) is not supported.");
  210. }
  211. var connectFlags = new ByteReader(body.ReadByte());
  212. if (connectFlags.Read())
  213. {
  214. throw new MqttProtocolViolationException("The first bit of the Connect Flags must be set to 0.");
  215. }
  216. var packet = new MqttConnectPacket
  217. {
  218. ProtocolVersion = protocolVersion,
  219. CleanSession = connectFlags.Read()
  220. };
  221. var willFlag = connectFlags.Read();
  222. var willQoS = connectFlags.Read(2);
  223. var willRetain = connectFlags.Read();
  224. var passwordFlag = connectFlags.Read();
  225. var usernameFlag = connectFlags.Read();
  226. packet.KeepAlivePeriod = body.ReadUInt16();
  227. packet.ClientId = body.ReadStringWithLengthPrefix();
  228. if (willFlag)
  229. {
  230. packet.WillMessage = new MqttApplicationMessage
  231. {
  232. Topic = body.ReadStringWithLengthPrefix(),
  233. Payload = body.ReadWithLengthPrefix().ToArray(),
  234. QualityOfServiceLevel = (MqttQualityOfServiceLevel)willQoS,
  235. Retain = willRetain
  236. };
  237. }
  238. if (usernameFlag)
  239. {
  240. packet.Username = body.ReadStringWithLengthPrefix();
  241. }
  242. if (passwordFlag)
  243. {
  244. packet.Password = body.ReadStringWithLengthPrefix();
  245. }
  246. ValidateConnectPacket(packet);
  247. return packet;
  248. }
  249. private static MqttBasePacket DeserializeSubAck(MqttPacketBodyReader body)
  250. {
  251. ThrowIfBodyIsEmpty(body);
  252. var packet = new MqttSubAckPacket
  253. {
  254. PacketIdentifier = body.ReadUInt16()
  255. };
  256. while (!body.EndOfStream)
  257. {
  258. packet.SubscribeReturnCodes.Add((MqttSubscribeReturnCode)body.ReadByte());
  259. }
  260. return packet;
  261. }
  262. private MqttBasePacket DeserializeConnAck(MqttPacketBodyReader body)
  263. {
  264. ThrowIfBodyIsEmpty(body);
  265. var packet = new MqttConnAckPacket();
  266. var firstByteReader = new ByteReader(body.ReadByte());
  267. if (ProtocolVersion == MqttProtocolVersion.V311)
  268. {
  269. packet.IsSessionPresent = firstByteReader.Read();
  270. }
  271. packet.ConnectReturnCode = (MqttConnectReturnCode)body.ReadByte();
  272. return packet;
  273. }
  274. private static void ValidateConnectPacket(MqttConnectPacket packet)
  275. {
  276. if (packet == null) throw new ArgumentNullException(nameof(packet));
  277. if (string.IsNullOrEmpty(packet.ClientId) && !packet.CleanSession)
  278. {
  279. throw new MqttProtocolViolationException("CleanSession must be set if ClientId is empty [MQTT-3.1.3-7].");
  280. }
  281. }
  282. private static void ValidatePublishPacket(MqttPublishPacket packet)
  283. {
  284. if (packet == null) throw new ArgumentNullException(nameof(packet));
  285. if (packet.QualityOfServiceLevel == 0 && packet.Dup)
  286. {
  287. throw new MqttProtocolViolationException("Dup flag must be false for QoS 0 packets [MQTT-3.3.1-2].");
  288. }
  289. }
  290. private byte Serialize(MqttConnectPacket packet, Stream stream)
  291. {
  292. ValidateConnectPacket(packet);
  293. // Write variable header
  294. if (ProtocolVersion == MqttProtocolVersion.V311)
  295. {
  296. stream.WriteWithLengthPrefix("MQTT");
  297. stream.WriteByte(4); // 3.1.2.2 Protocol Level 4
  298. }
  299. else
  300. {
  301. stream.WriteWithLengthPrefix("MQIsdp");
  302. stream.WriteByte(3); // Protocol Level 3
  303. }
  304. var connectFlags = new ByteWriter(); // 3.1.2.3 Connect Flags
  305. connectFlags.Write(false); // Reserved
  306. connectFlags.Write(packet.CleanSession);
  307. connectFlags.Write(packet.WillMessage != null);
  308. if (packet.WillMessage != null)
  309. {
  310. connectFlags.Write((int)packet.WillMessage.QualityOfServiceLevel, 2);
  311. connectFlags.Write(packet.WillMessage.Retain);
  312. }
  313. else
  314. {
  315. connectFlags.Write(0, 2);
  316. connectFlags.Write(false);
  317. }
  318. if (packet.Password != null && packet.Username == null)
  319. {
  320. throw new MqttProtocolViolationException("If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].");
  321. }
  322. connectFlags.Write(packet.Password != null);
  323. connectFlags.Write(packet.Username != null);
  324. stream.Write(connectFlags);
  325. stream.Write(packet.KeepAlivePeriod);
  326. stream.WriteWithLengthPrefix(packet.ClientId);
  327. if (packet.WillMessage != null)
  328. {
  329. stream.WriteWithLengthPrefix(packet.WillMessage.Topic);
  330. stream.WriteWithLengthPrefix(packet.WillMessage.Payload);
  331. }
  332. if (packet.Username != null)
  333. {
  334. stream.WriteWithLengthPrefix(packet.Username);
  335. }
  336. if (packet.Password != null)
  337. {
  338. stream.WriteWithLengthPrefix(packet.Password);
  339. }
  340. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Connect);
  341. }
  342. private byte Serialize(MqttConnAckPacket packet, Stream stream)
  343. {
  344. if (ProtocolVersion == MqttProtocolVersion.V310)
  345. {
  346. stream.WriteByte(0);
  347. }
  348. else if (ProtocolVersion == MqttProtocolVersion.V311)
  349. {
  350. var connectAcknowledgeFlags = new ByteWriter();
  351. connectAcknowledgeFlags.Write(packet.IsSessionPresent);
  352. stream.Write(connectAcknowledgeFlags);
  353. }
  354. else
  355. {
  356. throw new MqttProtocolViolationException("Protocol version not supported.");
  357. }
  358. stream.WriteByte((byte)packet.ConnectReturnCode);
  359. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.ConnAck);
  360. }
  361. private static byte Serialize(MqttPubRelPacket packet, Stream stream)
  362. {
  363. if (!packet.PacketIdentifier.HasValue)
  364. {
  365. throw new MqttProtocolViolationException("PubRel packet has no packet identifier.");
  366. }
  367. stream.Write(packet.PacketIdentifier.Value);
  368. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRel, 0x02);
  369. }
  370. private static byte Serialize(MqttPublishPacket packet, Stream stream)
  371. {
  372. ValidatePublishPacket(packet);
  373. stream.WriteWithLengthPrefix(packet.Topic);
  374. if (packet.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
  375. {
  376. if (!packet.PacketIdentifier.HasValue)
  377. {
  378. throw new MqttProtocolViolationException("Publish packet has no packet identifier.");
  379. }
  380. stream.Write(packet.PacketIdentifier.Value);
  381. }
  382. else
  383. {
  384. if (packet.PacketIdentifier > 0)
  385. {
  386. throw new MqttProtocolViolationException("Packet identifier must be empty if QoS == 0 [MQTT-2.3.1-5].");
  387. }
  388. }
  389. if (packet.Payload?.Length > 0)
  390. {
  391. stream.Write(packet.Payload, 0, packet.Payload.Length);
  392. }
  393. byte fixedHeader = 0;
  394. if (packet.Retain)
  395. {
  396. fixedHeader |= 0x01;
  397. }
  398. fixedHeader |= (byte)((byte)packet.QualityOfServiceLevel << 1);
  399. if (packet.Dup)
  400. {
  401. fixedHeader |= 0x08;
  402. }
  403. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Publish, fixedHeader);
  404. }
  405. private static byte Serialize(MqttPubAckPacket packet, Stream stream)
  406. {
  407. if (!packet.PacketIdentifier.HasValue)
  408. {
  409. throw new MqttProtocolViolationException("PubAck packet has no packet identifier.");
  410. }
  411. stream.Write(packet.PacketIdentifier.Value);
  412. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubAck);
  413. }
  414. private static byte Serialize(MqttPubRecPacket packet, Stream stream)
  415. {
  416. if (!packet.PacketIdentifier.HasValue)
  417. {
  418. throw new MqttProtocolViolationException("PubRec packet has no packet identifier.");
  419. }
  420. stream.Write(packet.PacketIdentifier.Value);
  421. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubRec);
  422. }
  423. private static byte Serialize(MqttPubCompPacket packet, Stream stream)
  424. {
  425. if (!packet.PacketIdentifier.HasValue)
  426. {
  427. throw new MqttProtocolViolationException("PubComp packet has no packet identifier.");
  428. }
  429. stream.Write(packet.PacketIdentifier.Value);
  430. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.PubComp);
  431. }
  432. private static byte Serialize(MqttSubscribePacket packet, Stream stream)
  433. {
  434. if (!packet.TopicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.8.3-3].");
  435. if (!packet.PacketIdentifier.HasValue)
  436. {
  437. throw new MqttProtocolViolationException("Subscribe packet has no packet identifier.");
  438. }
  439. stream.Write(packet.PacketIdentifier.Value);
  440. if (packet.TopicFilters?.Count > 0)
  441. {
  442. foreach (var topicFilter in packet.TopicFilters)
  443. {
  444. stream.WriteWithLengthPrefix(topicFilter.Topic);
  445. stream.WriteByte((byte)topicFilter.QualityOfServiceLevel);
  446. }
  447. }
  448. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Subscribe, 0x02);
  449. }
  450. private static byte Serialize(MqttSubAckPacket packet, Stream stream)
  451. {
  452. if (!packet.PacketIdentifier.HasValue)
  453. {
  454. throw new MqttProtocolViolationException("SubAck packet has no packet identifier.");
  455. }
  456. stream.Write(packet.PacketIdentifier.Value);
  457. if (packet.SubscribeReturnCodes?.Any() == true)
  458. {
  459. foreach (var packetSubscribeReturnCode in packet.SubscribeReturnCodes)
  460. {
  461. stream.WriteByte((byte)packetSubscribeReturnCode);
  462. }
  463. }
  464. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.SubAck);
  465. }
  466. private static byte Serialize(MqttUnsubscribePacket packet, Stream stream)
  467. {
  468. if (!packet.TopicFilters.Any()) throw new MqttProtocolViolationException("At least one topic filter must be set [MQTT-3.10.3-2].");
  469. if (!packet.PacketIdentifier.HasValue)
  470. {
  471. throw new MqttProtocolViolationException("Unsubscribe packet has no packet identifier.");
  472. }
  473. stream.Write(packet.PacketIdentifier.Value);
  474. if (packet.TopicFilters?.Any() == true)
  475. {
  476. foreach (var topicFilter in packet.TopicFilters)
  477. {
  478. stream.WriteWithLengthPrefix(topicFilter);
  479. }
  480. }
  481. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.Unsubscibe, 0x02);
  482. }
  483. private static byte Serialize(MqttUnsubAckPacket packet, Stream stream)
  484. {
  485. if (!packet.PacketIdentifier.HasValue)
  486. {
  487. throw new MqttProtocolViolationException("UnsubAck packet has no packet identifier.");
  488. }
  489. stream.Write(packet.PacketIdentifier.Value);
  490. return MqttPacketWriter.BuildFixedHeader(MqttControlPacketType.UnsubAck);
  491. }
  492. private static byte SerializeEmptyPacket(MqttControlPacketType type)
  493. {
  494. return MqttPacketWriter.BuildFixedHeader(type);
  495. }
  496. private static void ThrowIfBodyIsEmpty(MqttPacketBodyReader body)
  497. {
  498. if (body == null || body.Length == 0)
  499. {
  500. throw new MqttProtocolViolationException("Data from the body is required but not present.");
  501. }
  502. }
  503. }
  504. }