/// <summary> /// Parses a buffer that hold a packet data and updates the packet structure with data from the buffer. /// </summary> /// <param name="obj"></param> /// <param name="buffer"></param> /// <param name="offset"></param> /// <returns>New offset after parsing the buffer</returns> /// 01-01-05-00-03-00-DC-65-C3-3B-02-00-10-00-00-00-00-00-EB-6B private static int Parse_Fields(PacketBase obj, byte[] buffer, int offset, int size) { int offsetLimit = offset + size; if (offsetLimit > buffer.Length) throw new rfidInvalidPacketException(rfidErrorCode.PacketDataTooSmall, buffer, 0); foreach (System.Reflection.FieldInfo info in obj.GetType().GetFields()) { #region Parse Tracing code #if false if (info.FieldType.IsArray) { System.Diagnostics.Debug.WriteLine(String.Format("ParseMessage:: {0}: {1}", info.FieldType.Name, info.Name), "TRACE"); } else { System.Diagnostics.Debug.WriteLine(String.Format("ParseMessage:: {0} {1}: {2} = {3}", info.IsInitOnly ? "RO" : "", info.FieldType.Name, info.Name, info.GetValue(obj).ToString()), "TRACE"); } #endif #endregion switch (info.FieldType.Name) { case "Byte[]": int arraySize = 0; if (info.GetValue(obj) != null) // This is for fixed length byte arrays (none right now) { arraySize = ((byte[])info.GetValue(obj)).Length; if (offset + arraySize > offsetLimit) return -1; byte[] newArray = new byte[arraySize]; for (int i = 0; i < arraySize; i++) newArray[i] = buffer[offset+i]; // Reverse the array if it has tag data if (info.Name == "inventory_data") Array.Reverse(newArray); info.SetValue(obj, newArray); } else { // Variable length array (assume the rest of the packet is the data) arraySize = buffer.Length - offset; byte[] newArray = new byte[arraySize]; for (int i = 0; i < arraySize; i++) newArray[i] = buffer[offset+i]; info.SetValue(obj, newArray); } offset += arraySize; // binaryWriter.Write((byte[])info.GetValue(obj), 0, arraySize); break; case "Byte": if (offset + 1 > offsetLimit) return -1; if (info.IsInitOnly) { if ((byte)info.GetValue(obj) != (byte)buffer[offset]) throw new rfidInvalidPacketFieldException(info.Name, info.GetValue(obj).ToString(), buffer[offset].ToString()); } else { info.SetValue(obj, buffer[offset]); } offset += 1; break; case "UInt16": if (offset + 2 > offsetLimit) return -1; if (info.IsInitOnly) { if ((UInt16)info.GetValue(obj) != Parse_UInt16(buffer, offset)) throw new rfidInvalidPacketFieldException(info.Name, info.GetValue(obj).ToString(), Parse_UInt16(buffer, offset).ToString()); } else { info.SetValue(obj, Parse_UInt16(buffer, offset)); } offset += 2; break; case "UInt32": if (offset + 4 > offsetLimit) return -1; if (info.IsInitOnly) { if ((UInt32)info.GetValue(obj) != Parse_UInt32(buffer, offset)) throw new rfidInvalidPacketFieldException(info.Name, info.GetValue(obj).ToString(), Parse_UInt32(buffer, offset).ToString()); } else { info.SetValue(obj, Parse_UInt32(buffer, offset)); } offset += 4; break; case "String": int strlen = Find_String_Length(obj, info); if (offset + 12 > offsetLimit) return -1; info.SetValue(obj, Parse_String(buffer, offset, strlen)); offset += strlen; break; // case "Char[]": // binaryWriter.Write((char[])info.GetValue(obj)); // break; default: throw new ApplicationException("Parse_Fields Error " + info.Name + "(" + info.FieldType.Name + ") is an unsupported type."); } } return offset; }
private void ReceiveConnack() { PacketBase packet = Transport.Read(); ConnackPacket connack = packet as ConnackPacket; if (packet == null) { WriteToLog(Localization.UseRussian ? String.Format("Первый принимаемый пакет должен быть Connack, но получили {0}", packet.GetType().Name) : String.Format("First received message should be Connack, but {0} received instead", packet.GetType().Name)); throw new MqttProtocolException(String.Format("First received message should be Connack, but {0} received instead", packet.GetType().Name)); } if (connack.ReturnCode != ConnackReturnCode.Accepted) { WriteToLog(Localization.UseRussian ? "Соединение не разрешено брокером" : "The connection was not accepted"); throw new MqttConnectException("The connection was not accepted", connack.ReturnCode); } this.IsSessionPresent = connack.SessionPresent; }
void HandleReceivedPacket(PacketBase packet) { switch (packet.PacketType) { case PublishPacket.PacketTypeCode: OnPublishReceived(packet as PublishPacket); break; case PubackPacket.PacketTypeCode: OnPubackReceived(packet as PubackPacket); break; case PubrecPacket.PacketTypeCode: OnPubrecReceived(packet as PubrecPacket); break; case PubrelPacket.PacketTypeCode: OnPubrelReceived(packet as PubrelPacket); break; case PubcompPacket.PacketTypeCode: OnPubcompReceived(packet as PubcompPacket); break; case SubackPacket.PacketTypeCode: OnSubackReceived(packet as SubackPacket); break; case UnsubackPacket.PacketTypeCode: OnUnsubackReceived(packet as UnsubackPacket); break; case PingrespPacket.PacketTypeCode: break; default: WriteToLog(Localization.UseRussian ? String.Format("Не возможно принять пакет типа {0}", packet.GetType().Name) : String.Format("Cannot receive message of type {0}", packet.GetType().Name)); throw new MqttProtocolException(String.Format("Cannot receive message of type {0}", packet.GetType().Name)); } }
public FileConfigManager(PacketBase packet) : this(packet.GetType().Name.ToLower()) { }