/// <summary> /// Receive messages from specified azure iothub on specified partition. The MessageManager parses the received message and displays it accordingly /// </summary> /// <param name="partition"></param> /// <param name="offset"></param> /// <param name="msgman"></param> /// <param name="hubData"></param> /// <returns></returns> public static async Task ReceiveMessages(string partition, DateTime offset, MessageManager msgman, IoTAccountData hubData) { string port = hubData.EventHubInfo.EventHubPort.Replace("sb://", ""); port = port.Replace("/", ""); Address address = new Address(port, 5671, hubData.SharedAccessPolicy, hubData.PrimaryKey, "/", "amqps"); Connection connection = await Connection.Factory.CreateAsync(address); Session session = new Session(connection); string totalMilliseconds = ((long)(offset - new DateTime(StartOfEpoch, DateTimeKind.Utc)).TotalMilliseconds).ToString(); Map filters = new Map(); filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"), new DescribedValue( new Amqp.Types.Symbol("apache.org:selector-filter:string"), "amqp.annotation.x-opt-enqueuedtimeutc > " + totalMilliseconds + "")); ReceiverLink receiver = new ReceiverLink(session, "my-receiver", new global::Amqp.Framing.Source() { Address = hubData.EventHubInfo.EventHubEntity + "/ConsumerGroups/$Default/Partitions/" + partition, FilterSet = filters }, null); Amqp.Types.Symbol deviceIdKey = new Amqp.Types.Symbol("iothub-connection-device-id"); string deviceId = hubData.DeviceName; while (true) { Amqp.Message m = await receiver.ReceiveAsync(10000); if (m != null) { var id = m.MessageAnnotations.Map[deviceIdKey].ToString(); if (id == deviceId) { Data data = (Data)m.BodySection; string msg = System.Text.Encoding.UTF8.GetString(data.Binary, 0, data.Binary.Length); bool isValid = msgman.parseMessage(msg); if (isValid) { receiver.Accept(m); } else { receiver.Release(m); } } } } }
/// <summary> /// Writes a symbol value to a buffer. /// </summary> /// <param name="buffer">The buffer to write.</param> /// <param name="value">The symbol value.</param> /// <param name="smallEncoding">if true, try using small encoding if possible.</param> public static void WriteSymbol(ByteBuffer buffer, Symbol value, bool smallEncoding) { if (value == null) { AmqpBitConverter.WriteUByte(buffer, FormatCode.Null); } else { byte[] data = Encoding.UTF8.GetBytes(value); if (smallEncoding && data.Length <= byte.MaxValue) { AmqpBitConverter.WriteUByte(buffer, FormatCode.Symbol8); AmqpBitConverter.WriteUByte(buffer, (byte)data.Length); AmqpBitConverter.WriteBytes(buffer, data, 0, data.Length); } else { AmqpBitConverter.WriteUByte(buffer, FormatCode.Symbol32); AmqpBitConverter.WriteUInt(buffer, (uint)data.Length); AmqpBitConverter.WriteBytes(buffer, data, 0, data.Length); } } }
bool AreEqual(ulong? code1, Symbol symbol1, ulong? code2, Symbol symbol2) { if (code1 != null && code2 != null) { return code1.Value == code2.Value; } if (symbol1 != null && symbol2 != null) { return string.Equals((string)symbol1, (string)symbol2, StringComparison.Ordinal); } return false; }
public static Symbol[] GetSymbolMultiple(object[] fields, int index) { if (fields[index] == null) { return null; } Symbol[] symbols = fields[index] as Symbol[]; if (symbols != null) { return symbols; } Symbol symbol = fields[index] as Symbol; if (symbol != null) { symbols = new Symbol[] { symbol }; fields[index] = symbols; return symbols; } throw new AmqpException(ErrorCode.InvalidField, Fx.Format("{0} {1}", index, fields[index].GetType().Name)); }
internal bool TryGetMechanism(Symbol name, out SaslMechanism mechanism) { return this.mechanisms.TryGetValue(name, out mechanism); }
void RaiseErrorEvent(Link link, Symbol error) { var onError = this.OnError; if (onError != null) { onError.Invoke(this, link, error); } }
static void client_OnError(Client client, Link link, Symbol error) { Microsoft.SPOT.Debug.Print((link != null ? "Link" : "Client") + " was closed due to error " + (error ?? "[unknown]")); }