/// <param name="writer">Type is <seealso cref="RabbitMQ.Util.NetworkBinaryWriter"/>.</param>
        /// <param name="table">Type is <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</param>
        public static void WriteMap(NetworkBinaryWriter writer, IDictionary<string, object> table) {
            int entryCount = table.Count;
            BytesWireFormatting.WriteInt32(writer, entryCount);

            foreach (KeyValuePair<string, object> entry in table) {
                StreamWireFormatting.WriteUntypedString(writer, (string) entry.Key);
                StreamWireFormatting.WriteObject(writer, entry.Value);
            }
        }
        public SocketFrameHandler_0_9(AmqpTcpEndpoint endpoint)
        {
            m_endpoint = endpoint;
            m_socket = new TcpClient();
            m_socket.Connect(endpoint.HostName, endpoint.Port);
            // disable Nagle's algorithm, for more consistently low latency 
            m_socket.NoDelay = true;

            Stream netstream = m_socket.GetStream();
            m_reader = new NetworkBinaryReader(netstream);
            m_writer = new NetworkBinaryWriter(netstream);
        }
Пример #3
0
        public MethodArgumentWriter(NetworkBinaryWriter writer)
        {
            m_writer = writer;
            if (!m_writer.BaseStream.CanSeek)
            {
                //FIXME: Consider throwing System.IO.IOException
                // with message indicating that the specified writer does not support Seeking

                // Only really a problem if we try to write a table,
                // but complain anyway. See WireFormatting.WriteTable
                throw new NotSupportedException("Cannot write method arguments to non-positionable stream");
            }
            ResetBitAccumulator();
        }
Пример #4
0
        public static void CheckEmptyFrameSize() {
            Frame f = new Frame(CommonFraming.Constants.FrameBody, 0, m_emptyByteArray);
            MemoryStream stream = new MemoryStream();
            NetworkBinaryWriter writer = new NetworkBinaryWriter(stream);
            f.WriteTo(writer);
            long actualLength = stream.Length;

            if (EmptyFrameSize != actualLength) {
                string message =
                    string.Format("EmptyFrameSize is incorrect - defined as {0} where the computed value is in fact {1}.",
                                  EmptyFrameSize,
                                  actualLength);
                throw new ProtocolViolationException(message);
            }
        }
 public void Check(NetworkBinaryWriter w, byte[] expected)
 {
     byte[] actual = Contents(w);
     try
     {
         Assert.AreEqual(expected, actual);
     }
     catch
     {
         Console.WriteLine();
         Console.WriteLine("EXPECTED ==================================================");
         DebugUtil.Dump(expected);
         Console.WriteLine("ACTUAL ====================================================");
         DebugUtil.Dump(actual);
         Console.WriteLine("===========================================================");
         throw;
     }
 }
 public static void WriteLongstr(NetworkBinaryWriter writer, byte[] val)
 {
     WriteLong(writer, (uint)val.Length);
     writer.Write(val);
 }
 public static void WriteOctet(NetworkBinaryWriter writer, byte val)
 {
     writer.Write((byte)val);
 }
 public static void WriteBytes(NetworkBinaryWriter writer, byte[] value) {
     WriteBytes(writer, value, 0, value.Length);
 }
 public static void WriteLonglong(NetworkBinaryWriter writer, ulong val)
 {
     writer.Write((ulong)val);
 }
 public void Check(NetworkBinaryWriter w, byte[] bytes)
 {
     Assert.AreEqual(bytes, Contents(w));
 }
 public static byte[] Contents(NetworkBinaryWriter w)
 {
     return ((MemoryStream)w.BaseStream).ToArray();
 }
 public static void WriteDouble(NetworkBinaryWriter writer, double value) {
     writer.Write((byte) StreamWireFormattingTag.Double);
     writer.Write(value);
 }
        ///<summary>Writes an AMQP "table" to the writer.</summary>
        ///<remarks>
        ///<para>
        /// In this method, we assume that the stream that backs our
        /// NetworkBinaryWriter is a positionable stream - which it is
        /// currently (see Frame.m_accumulator, Frame.GetWriter and
        /// Command.Transmit).
        ///</para>
        ///<para>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t
        /// x and V types and the AMQP 0-9-1 A type.
        ///</para>
        ///</remarks>
        public static void WriteTable(NetworkBinaryWriter writer, IDictionary val)
        {
            if (val == null)
            {
                writer.Write((uint)0);
            }
            else
            {
                Stream backingStream = writer.BaseStream;
                long patchPosition = backingStream.Position;
                writer.Write((uint)0); // length of table - will be backpatched

                foreach (DictionaryEntry entry in val)
                {
                    WriteShortstr(writer, (string)entry.Key);
                    object value = entry.Value;
                    WriteFieldValue(writer, value);
                }

                // Now, backpatch the table length.
                long savedPosition = backingStream.Position;
                long tableLength = savedPosition - patchPosition - 4; // offset for length word
                backingStream.Seek(patchPosition, SeekOrigin.Begin);
                writer.Write((uint)tableLength);
                backingStream.Seek(savedPosition, SeekOrigin.Begin);
            }
        }
 public ContentHeaderPropertyWriter(NetworkBinaryWriter writer)
 {
     m_writer = writer;
     m_flagWord = 0;
     m_bitCount = 0;
 }
        public SocketFrameHandler(AmqpTcpEndpoint endpoint,
                                  ConnectionFactory.ObtainSocket socketFactory,
                                  int timeout)
        {
            m_endpoint = endpoint;
            m_socket = null;
            if (Socket.OSSupportsIPv6)
            {
                try
                {
                    m_socket = socketFactory(AddressFamily.InterNetworkV6);
                    Connect(m_socket, endpoint, timeout);
                }
                catch (ConnectFailureException) // could not connect using IPv6
                {
                    m_socket = null;
                }
                catch (SocketException) // Mono might raise a SocketException when using IPv4 addresses on an OS that supports IPv6
                {
                    m_socket = null;
                }
            }
            if (m_socket == null)
            {
                m_socket = socketFactory(AddressFamily.InterNetwork);
                Connect(m_socket, endpoint, timeout);
            }

            Stream netstream = m_socket.GetStream();
            if (endpoint.Ssl.Enabled)
            {
                try
                {
                    netstream = SslHelper.TcpUpgrade(netstream, endpoint.Ssl, timeout);
                }
                catch (Exception)
                {
                    Close();
                    throw;
                }
            }
            m_reader = new NetworkBinaryReader(new BufferedStream(netstream));
            m_writer = new NetworkBinaryWriter(new BufferedStream(netstream));
        }
 public static void WriteUntypedString(NetworkBinaryWriter writer, string value) {
     writer.Write(Encoding.UTF8.GetBytes(value));
     writer.Write((byte) 0);
 }
        ///<exception cref="ProtocolViolationException"/>
        public static void WriteObject(NetworkBinaryWriter writer, object value) {
            if (value is bool) { WriteBool(writer, (bool) value); }
            else if (value is int) { WriteInt32(writer, (int) value); }
            else if (value is short) { WriteInt16(writer, (short) value); }
            else if (value is byte) { WriteByte(writer, (byte) value); }
            else if (value is char) { WriteChar(writer, (char) value); }
            else if (value is long) { WriteInt64(writer, (long) value); }
            else if (value is float) { WriteSingle(writer, (float) value); }
            else if (value is double) { WriteDouble(writer, (double) value); }
            else if (value is byte[]) { WriteBytes(writer, (byte[]) value); }
	    else if (value is BinaryTableValue) { WriteBytes(writer,
							     ((BinaryTableValue) value).Bytes); }
            else if (value is string) { WriteString(writer, (string) value); }
            else {
                string message = string.Format("Invalid object in StreamMessage.WriteObject: {0}",
                                               value);
                throw new ProtocolViolationException(message);
            }
        }
 public static void WriteString(NetworkBinaryWriter writer, string value) {
     writer.Write((byte) StreamWireFormattingTag.String);
     WriteUntypedString(writer, value);
 }
 public static void WriteShort(NetworkBinaryWriter writer, ushort val)
 {
     writer.Write((ushort)val);
 }
 public static void WriteArray(NetworkBinaryWriter writer, IList val)
 {
     if (val == null)
     {
         writer.Write((uint)0);
     }
     else
     {
         Stream backingStream = writer.BaseStream;
         long patchPosition = backingStream.Position;
         writer.Write((uint)0); // length of table - will be backpatched
         foreach (object entry in val)
         {
             WriteFieldValue(writer, entry);
         }
         long savedPosition = backingStream.Position;
         long tableLength = savedPosition - patchPosition - 4; // offset for length word
         backingStream.Seek(patchPosition, SeekOrigin.Begin);
         writer.Write((uint)tableLength);
         backingStream.Seek(savedPosition, SeekOrigin.Begin);
     }
 }
 public static void WriteShortstr(NetworkBinaryWriter writer, string val)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(val);
     int len = bytes.Length;
     if (len > 255)
     {
         throw new WireFormattingException("Short string too long; " +
                                           "UTF-8 encoded length=" + len + ", max=255");
     }
     writer.Write((byte)len);
     writer.Write(bytes);
 }
 public static void WriteDecimal(NetworkBinaryWriter writer, decimal value)
 {
     byte scale;
     int mantissa;
     DecimalToAmqp(value, out scale, out mantissa);
     WriteOctet(writer, scale);
     WriteLong(writer, (uint)mantissa);
 }
 public static void WriteTimestamp(NetworkBinaryWriter writer, AmqpTimestamp val)
 {
     // 0-9 is afaict silent on the signedness of the timestamp.
     // See also MethodArgumentReader.ReadTimestamp and AmqpTimestamp itself
     WriteLonglong(writer, (ulong)val.UnixTime);
 }
Пример #24
0
 public void WriteTo(NetworkBinaryWriter writer)
 {
     FinishWriting();
     writer.Write((byte) m_type);
     writer.Write((ushort) m_channel);
     writer.Write((uint) m_payload.Length);
     writer.Write((byte[]) m_payload);
     writer.Write((byte) CommonFraming.Constants.FrameEnd);
 }
 public void SetUp()
 {
     m_w = Writer();
 }
 public static void WriteSingle(NetworkBinaryWriter writer, float value) {
     writer.Write((byte) StreamWireFormattingTag.Single);
     writer.Write(value);
 }
 public static void WriteLong(NetworkBinaryWriter writer, uint val)
 {
     writer.Write((uint)val);
 }
 public static void WriteBytes(NetworkBinaryWriter writer,
                               byte[] value,
                               int offset,
                               int length)
 {
     writer.Write((byte) StreamWireFormattingTag.Bytes);
     writer.Write(length);
     writer.Write(value, offset, length);
 }
 public static void WriteFieldValue(NetworkBinaryWriter writer, object value)
 {
     if (value == null)
     {
         WriteOctet(writer, (byte)'V');
     }
     else if (value is string)
     {
         WriteOctet(writer, (byte)'S');
         WriteLongstr(writer, Encoding.UTF8.GetBytes((string)value));
     }
     else if (value is byte[])
     {
         WriteOctet(writer, (byte)'S');
         WriteLongstr(writer, (byte[])value);
     }
     else if (value is int)
     {
         WriteOctet(writer, (byte)'I');
         writer.Write((int)value);
     }
     else if (value is decimal)
     {
         WriteOctet(writer, (byte)'D');
         WriteDecimal(writer, (decimal)value);
     }
     else if (value is AmqpTimestamp)
     {
         WriteOctet(writer, (byte)'T');
         WriteTimestamp(writer, (AmqpTimestamp)value);
     }
     else if (value is IDictionary)
     {
         WriteOctet(writer, (byte)'F');
         WriteTable(writer, (IDictionary)value);
     }
     else if (value is IList)
     {
         WriteOctet(writer, (byte)'A');
         WriteArray(writer, (IList)value);
     }
     else if (value is byte)
     {
         WriteOctet(writer, (byte)'b');
         WriteOctet(writer, (byte)value);
     }
     else if (value is double)
     {
         WriteOctet(writer, (byte)'d');
         writer.Write((double)value);
     }
     else if (value is float)
     {
         WriteOctet(writer, (byte)'f');
         writer.Write((float)value);
     }
     else if (value is long)
     {
         WriteOctet(writer, (byte)'l');
         writer.Write((long)value);
     }
     else if (value is short)
     {
         WriteOctet(writer, (byte)'s');
         writer.Write((short)value);
     }
     else if (value is bool)
     {
         WriteOctet(writer, (byte)'t');
         WriteOctet(writer, (byte)(((bool)value) ? 1 : 0));
     }
     else if (value is BinaryTableValue)
     {
         WriteOctet(writer, (byte)'x');
         WriteLongstr(writer, ((BinaryTableValue)value).Bytes);
     }
     else
     {
         throw new WireFormattingException("Value cannot appear as table value",
                                           value);
     }
 }
 public static void WriteInt64(NetworkBinaryWriter writer, long value) {
     writer.Write((byte) StreamWireFormattingTag.Int64);
     writer.Write(value);
 }