public void TestQpidJmsTypes() { NetworkBinaryWriter w = Writer(); Hashtable t = new Hashtable(); t["B"] = (byte)255; t["b"] = (sbyte)-128; t["d"] = (double)123; t["f"] = (float)123; t["l"] = (long)123; t["s"] = (short)123; t["t"] = true; byte[] xbytes = { 0xaa, 0x55 }; t["x"] = new BinaryTableValue(xbytes); t["V"] = null; WireFormatting.WriteTable(w, t); IDictionary nt = (IDictionary)WireFormatting.ReadTable(Reader(Contents(w))); Assert.AreEqual(typeof(byte), nt["B"].GetType()); Assert.AreEqual((byte)255, nt["B"]); Assert.AreEqual(typeof(sbyte), nt["b"].GetType()); Assert.AreEqual((sbyte)-128, nt["b"]); Assert.AreEqual(typeof(double), nt["d"].GetType()); Assert.AreEqual((double)123, nt["d"]); Assert.AreEqual(typeof(float), nt["f"].GetType()); Assert.AreEqual((float)123, nt["f"]); Assert.AreEqual(typeof(long), nt["l"].GetType()); Assert.AreEqual((long)123, nt["l"]); Assert.AreEqual(typeof(short), nt["s"].GetType()); Assert.AreEqual((short)123, nt["s"]); Assert.AreEqual(true, nt["t"]); Assert.AreEqual(xbytes, ((BinaryTableValue)nt["x"]).Bytes); Assert.AreEqual(null, nt["V"]); }
public void TestQpidJmsTypes() { Hashtable t = new Hashtable // 4 { ["B"] = (byte)255, // 2+2 ["b"] = (sbyte)-128, // 2+2 ["d"] = (double)123, // 2+9 ["f"] = (float)123, // 2+5 ["l"] = (long)123, // 2+9 ["s"] = (short)123, // 2+2 ["t"] = true // 2+2 }; byte[] xbytes = { 0xaa, 0x55 }; t["x"] = new BinaryTableValue(xbytes); // 2+5+2 t["V"] = null; // 2+1 int bytesNeeded = WireFormatting.GetTableByteCount(t); byte[] bytes = new byte[bytesNeeded]; WireFormatting.WriteTable(ref bytes.GetStart(), t); int bytesRead = WireFormatting.ReadDictionary(bytes, out var nt); Assert.Equal(bytesNeeded, bytesRead); Assert.Equal(typeof(byte), nt["B"].GetType()); Assert.Equal((byte)255, nt["B"]); Assert.Equal(typeof(sbyte), nt["b"].GetType()); Assert.Equal((sbyte)-128, nt["b"]); Assert.Equal(typeof(double), nt["d"].GetType()); Assert.Equal((double)123, nt["d"]); Assert.Equal(typeof(float), nt["f"].GetType()); Assert.Equal((float)123, nt["f"]); Assert.Equal(typeof(long), nt["l"].GetType()); Assert.Equal((long)123, nt["l"]); Assert.Equal(typeof(short), nt["s"].GetType()); Assert.Equal((short)123, nt["s"]); Assert.Equal(true, nt["t"]); Assert.Equal(xbytes, ((BinaryTableValue)nt["x"]).Bytes); Assert.Null(nt["V"]); }
public void TestQpidJmsTypes() { IDictionary <string, object> t = new Dictionary <string, object> { ["B"] = (byte)255, ["b"] = (sbyte)-128, ["d"] = (double)123, ["f"] = (float)123, ["l"] = (long)123, ["s"] = (short)123, ["t"] = true }; byte[] xbytes = new byte[] { 0xaa, 0x55 }; t["x"] = new BinaryTableValue(xbytes); t["V"] = null; int bytesNeeded = WireFormatting.GetTableByteCount(t); byte[] bytes = new byte[bytesNeeded]; WireFormatting.WriteTable(bytes, t); IDictionary nt = (IDictionary)WireFormatting.ReadTable(bytes, out int bytesRead); Assert.AreEqual(bytesNeeded, bytesRead); Assert.AreEqual(typeof(byte), nt["B"].GetType()); Assert.AreEqual((byte)255, nt["B"]); Assert.AreEqual(typeof(sbyte), nt["b"].GetType()); Assert.AreEqual((sbyte)-128, nt["b"]); Assert.AreEqual(typeof(double), nt["d"].GetType()); Assert.AreEqual((double)123, nt["d"]); Assert.AreEqual(typeof(float), nt["f"].GetType()); Assert.AreEqual((float)123, nt["f"]); Assert.AreEqual(typeof(long), nt["l"].GetType()); Assert.AreEqual((long)123, nt["l"]); Assert.AreEqual(typeof(short), nt["s"].GetType()); Assert.AreEqual((short)123, nt["s"]); Assert.AreEqual(true, nt["t"]); Assert.AreEqual(xbytes, ((BinaryTableValue)nt["x"]).Bytes); Assert.AreEqual(null, nt["V"]); }
public void TestTableEncoding_x() { NetworkBinaryWriter w = Writer(); Hashtable t = new Hashtable(); t["a"] = new BinaryTableValue(new byte[] { 0xaa, 0x55 }); WireFormatting.WriteTable(w, t); Check(w, new byte[] { 0, 0, 0, 9, // table length 1, (byte)'a', // key (byte)'x', // type 0, 0, 0, 2, 0xaa, 0x55 // value }); }
public void TestQpidJmsTypes() { NetworkBinaryWriter w = Writer(); Hashtable t = new Hashtable(); t["b"] = (byte)123; t["d"] = (double)123; t["f"] = (float)123; t["l"] = (long)123; t["s"] = (short)123; t["t"] = true; byte[] xbytes = new byte[] { 0xaa, 0x55 }; t["x"] = new BinaryTableValue(xbytes); t["V"] = null; WireFormatting.WriteTable(w, t); IDictionary nt = WireFormatting.ReadTable(Reader(Contents(w))); Assert.AreEqual(typeof(byte), nt["b"].GetType()); Assert.AreEqual((byte)123, nt["b"]); Assert.AreEqual(typeof(double), nt["d"].GetType()); Assert.AreEqual((double)123, nt["d"]); Assert.AreEqual(typeof(float), nt["f"].GetType()); Assert.AreEqual((float)123, nt["f"]); Assert.AreEqual(typeof(long), nt["l"].GetType()); Assert.AreEqual((long)123, nt["b"]); Assert.AreEqual(typeof(short), nt["s"].GetType()); Assert.AreEqual((short)123, nt["b"]); Assert.AreEqual(true, nt["t"]); Assert.AreEqual(xbytes, ((BinaryTableValue)nt["x"]).Bytes); Assert.AreEqual(null, nt["V"]); }
public static int Main(string[] args) { bool persistMode = false; int optionIndex = 0; while (optionIndex < args.Length) { if (args[optionIndex] == "/persist") { persistMode = true; } else { break; } optionIndex++; } if (((args.Length - optionIndex) < 1) || (((args.Length - optionIndex - 1) % 2) == 1)) { Console.Error.WriteLine("Usage: SendMap [<option> ...] <exchange-uri> [[<key> <value>] ...]"); Console.Error.WriteLine("RabbitMQ .NET client version " + typeof(IModel).Assembly.GetName().Version.ToString()); Console.Error.WriteLine("Exchange-URI: amqp://host[:port]/exchange[/routingkey][?type=exchangetype]"); Console.Error.WriteLine("Keys must start with '+' or with '-'. Those starting with '+' are placed in"); Console.Error.WriteLine("the body of the message, and those starting with '-' are placed in the headers."); Console.Error.WriteLine("Values must start with a single character typecode and a colon."); Console.Error.WriteLine("Supported typecodes are:"); Console.Error.WriteLine(" S - string/byte array"); Console.Error.WriteLine(" x - byte array (base-64)"); Console.Error.WriteLine(" t - boolean"); Console.Error.WriteLine(" i - 32-bit integer"); Console.Error.WriteLine(" d - double-precision float"); Console.Error.WriteLine(" D - fixed-point decimal"); Console.Error.WriteLine("Note that some types are valid only in the body of a message, and some are"); Console.Error.WriteLine("valid only in the headers."); Console.Error.WriteLine("The exchange \"amq.default\" is an alias for the default (\"\") AMQP exchange,"); Console.Error.WriteLine("introduced so that the default exchange can be addressed via URI syntax."); Console.Error.WriteLine("Available options:"); Console.Error.WriteLine(" /persist send message in 'persistent' mode"); return(2); } Uri uri = new Uri(args[optionIndex++]); string exchange = uri.Segments[1].TrimEnd(new char[] { '/' }); string exchangeType = uri.Query.StartsWith("?type=") ? uri.Query.Substring(6) : null; string routingKey = uri.Segments.Length > 2 ? uri.Segments[2] : ""; if (exchange == "amq.default") { exchange = ""; } ConnectionFactory cf = new ConnectionFactory(); cf.Endpoint = new AmqpTcpEndpoint(uri); using (IConnection conn = cf.CreateConnection()) { using (IModel ch = conn.CreateModel()) { if (exchangeType != null) { ch.ExchangeDeclare(exchange, exchangeType); } IMapMessageBuilder b = new MapMessageBuilder(ch); while ((optionIndex + 1) < args.Length) { string keyAndDiscriminator = args[optionIndex++]; string valueAndType = args[optionIndex++]; if (keyAndDiscriminator.Length < 1) { Console.Error.WriteLine("Invalid key: '{0}'", keyAndDiscriminator); return(2); } string key = keyAndDiscriminator.Substring(1); char discriminator = keyAndDiscriminator[0]; IDictionary <string, object> target; switch (discriminator) { case '-': target = b.Headers; break; case '+': target = b.Body; break; default: Console.Error.WriteLine("Invalid key: '{0}'", keyAndDiscriminator); return(2); } if (valueAndType.Length < 2 || valueAndType[1] != ':') { Console.Error.WriteLine("Invalid value: '{0}'", valueAndType); return(2); } string valueStr = valueAndType.Substring(2); char typeCode = valueAndType[0]; object value; switch (typeCode) { case 'S': value = valueStr; break; case 'x': value = new BinaryTableValue(Convert.FromBase64String(valueStr)); break; case 't': value = (valueStr.ToLower() == "true" || valueStr.ToLower() == "yes" || valueStr.ToLower() == "on" || valueStr == "1"); break; case 'i': value = int.Parse(valueStr); break; case 'd': value = double.Parse(valueStr); break; case 'D': value = decimal.Parse(valueStr); break; default: Console.Error.WriteLine("Invalid type code: '{0}'", typeCode); return(2); } target[key] = value; } if (persistMode) { ((IBasicProperties)b.GetContentHeader()).DeliveryMode = 2; } ch.BasicPublish(exchange, routingKey, (IBasicProperties)b.GetContentHeader(), b.GetContentBody()); return(0); } } }
public void TestTableEncoding_x() { NetworkBinaryWriter w = Writer(); Hashtable t = new Hashtable(); t["a"] = new BinaryTableValue(new byte[] { 0xaa, 0x55 }); WireFormatting.WriteTable(w, t); Check(w, new byte[] { 0,0,0,9, // table length 1,(byte)'a', // key (byte)'x', // type 0,0,0,2,0xaa,0x55 // value }); }
public static object ReadFieldValue(NetworkBinaryReader reader) { object value = null; byte discriminator = reader.ReadByte(); switch ((char)discriminator) { case 'S': value = ReadLongstr(reader); break; case 'I': value = reader.ReadInt32(); break; case 'D': value = ReadDecimal(reader); break; case 'T': value = ReadTimestamp(reader); break; case 'F': value = ReadTable(reader); break; case 'A': value = ReadArray(reader); break; case 'b': value = reader.ReadSByte(); break; case 'd': value = reader.ReadDouble(); break; case 'f': value = reader.ReadSingle(); break; case 'l': value = reader.ReadInt64(); break; case 's': value = reader.ReadInt16(); break; case 't': value = (ReadOctet(reader) != 0); break; case 'x': value = new BinaryTableValue(ReadLongstr(reader)); break; case 'V': value = null; break; default: throw new SyntaxError("Unrecognised type in table: " + (char)discriminator); } return value; }
private object ConvertHeaderValueIfNecessary(object valueArg) { var value = valueArg; if (value == null) { return(value); } var valid = (value is string) || (value is byte[]) || (value is bool) || (value is byte) || (value is sbyte) || (value is uint) || (value is int) || (value is long) || (value is float) || (value is double) || (value is decimal) || (value is short) || (value is AmqpTimestamp) || (value is IList) || (value is IDictionary) || (value is BinaryTableValue) || (value is object[]) || (value is Type); if (!valid) { value = value.ToString(); } else if (value is object[] array) { var writableList = new object[array.Length]; for (var i = 0; i < array.Length; i++) { writableList[i] = ConvertHeaderValueIfNecessary(array[i]); } value = writableList; } else if (value is IList) { var writableList = new List <object>(); foreach (var listValue in (IList)value) { writableList.Add(ConvertHeaderValueIfNecessary(listValue)); } value = writableList; } else if (value is IDictionary originalMap) { var writableMap = new Dictionary <object, object>(); foreach (DictionaryEntry entry in originalMap) { writableMap[entry.Key] = ConvertHeaderValueIfNecessary(entry.Value); } value = writableMap; } else if (value is Type) { value = ((Type)value).ToString(); } else if (value is byte[]) { value = new BinaryTableValue((byte[])value); } return(value); }