///<summary>Fill this instance from the given byte buffer stream.
 ///</summary>
 public ulong ReadFrom(NetworkBinaryReader reader)
 {
     reader.ReadUInt16(); // weight - not currently used
     ulong bodySize = reader.ReadUInt64();
     ReadPropertiesFrom(new ContentHeaderPropertyReader(reader));
     return bodySize;
 }
Ejemplo n.º 2
0
 public static byte[] ReadLongstr(NetworkBinaryReader reader)
 {
     uint byteCount = reader.ReadUInt32();
     if (byteCount > int.MaxValue)
     {
         throw new SyntaxError("Long string too long; " +
                               "byte length=" + byteCount + ", max=" + int.MaxValue);
     }
     return reader.ReadBytes((int)byteCount);
 }
 ///<summary>Fill this instance from the given byte buffer
 ///stream. Throws BodyTooLongException, which is the reason
 ///for the channelNumber parameter.</summary>
 ///<remarks>
 ///<para>
 /// It might be better to do the body length check in our
 /// caller, currently CommandAssembler, which would avoid
 /// passing in the otherwise unrequired channelNumber
 /// parameter.
 ///</para>
 ///</remarks>
 public ulong ReadFrom(int channelNumber, NetworkBinaryReader reader)
 {
     reader.ReadUInt16(); // weight - not currently used
     ulong bodySize = reader.ReadUInt64();
     if (bodySize > MaximumPermittedReceivableBodySize)
     {
         throw new BodyTooLongException(channelNumber, bodySize);
     }
     ReadPropertiesFrom(new ContentHeaderPropertyReader(reader));
     return bodySize;
 }
 public static bool ReadBool(NetworkBinaryReader reader) {
     object value = ReadNonnullObject("bool", reader);
     if (value is bool) {
         return (bool) value;
     }
     if (value is string) {
         return PrimitiveParser.ParseBool((string) value);
     }
     PrimitiveParser.InvalidConversion("bool", value);
     return false;
 }
 public static int ReadInt32(NetworkBinaryReader reader) {
     object value = ReadNonnullObject("int", reader);
     if (value is int || value is short || value is byte) {
         return (int) value;
     }
     if (value is string) {
         return PrimitiveParser.ParseInt((string) value);
     }
     PrimitiveParser.InvalidConversion("int", value);
     return 0;
 }
        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);
        }
 public static bool ReadBool(NetworkBinaryReader reader)
 {
     object value = ReadNonnullObject("bool", reader);
     if (value is bool)
     {
         return (bool) value;
     }
     if (value is string)
     {
         return PrimitiveParser.ParseBool((string) value);
     }
     throw PrimitiveParser.CreateProtocolViolationException("bool", value);
 }
        public static IDictionary<string, object> ReadMap(NetworkBinaryReader reader) {
            int entryCount = BytesWireFormatting.ReadInt32(reader);
            if (entryCount < 0) {
                string message = string.Format("Invalid (negative) entryCount: {0}", entryCount);
                throw new ProtocolViolationException(message);
            }

            IDictionary<string, object> table = new Dictionary<string, object>(entryCount);
            for (int entryIndex = 0; entryIndex < entryCount; entryIndex++) {
                string key = StreamWireFormatting.ReadUntypedString(reader);
                object value = StreamWireFormatting.ReadObject(reader);
                table[key] = value;
            }

            return table;
        }
Ejemplo n.º 9
0
        public static Frame ReadFrom(NetworkBinaryReader reader)
        {
            int type;
            int channel;

            try
            {
                type = reader.ReadByte();
            }
            catch (IOException ioe)
            {
                // If it's a WSAETIMEDOUT SocketException, unwrap it.
                // This might happen when the limit of half-open connections is
                // reached.
                if (ioe.InnerException == null ||
                    !(ioe.InnerException is SocketException) ||
                    ((SocketException)ioe.InnerException).SocketErrorCode != SocketError.TimedOut)
                    throw ioe;
                throw ioe.InnerException;
            }

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            channel = reader.ReadUInt16();
            int payloadSize = reader.ReadInt32(); // FIXME - throw exn on unreasonable value
            byte[] payload = reader.ReadBytes(payloadSize);
            if (payload.Length != payloadSize)
            {
                // Early EOF.
                throw new MalformedFrameException("Short frame - expected " +
                                                  payloadSize + " bytes, got " +
                                                  payload.Length + " bytes");
            }

            int frameEndMarker = reader.ReadByte();
            if (frameEndMarker != CommonFraming.Constants.FrameEnd)
            {
                throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
            }

            return new Frame(type, channel, payload);
        }
 public static string ReadShortstr(NetworkBinaryReader reader)
 {
     int byteCount = reader.ReadByte();
     return Encoding.UTF8.GetString(reader.ReadBytes(byteCount));
 }
 public static ushort ReadShort(NetworkBinaryReader reader)
 {
     return reader.ReadUInt16();
 }
 public static byte ReadOctet(NetworkBinaryReader reader)
 {
     return reader.ReadByte();
 }
 public static ulong ReadLonglong(NetworkBinaryReader reader)
 {
     return reader.ReadUInt64();
 }
 ///<exception cref="ProtocolViolationException"/>
 public static object ReadNonnullObject(string target, NetworkBinaryReader reader) {
     object value = ReadObject(reader);
     if (value == null) {
         throw new ProtocolViolationException(string.Format("Null {0} value not permitted",
                                                            target));
     }
     return value;
 }
Ejemplo n.º 15
0
        public static void ProcessProtocolHeader(NetworkBinaryReader reader)
        {
            try
            {
                byte b1 = reader.ReadByte();
                byte b2 = reader.ReadByte();
                byte b3 = reader.ReadByte();
                if (b1 != 'M' || b2 != 'Q' || b3 != 'P')
                {
                    throw new MalformedFrameException("Invalid AMQP protocol header from server");
                }

                int transportHigh = reader.ReadByte();
                int transportLow = reader.ReadByte();
                int serverMajor = reader.ReadByte();
                int serverMinor = reader.ReadByte();
                throw new PacketNotRecognizedException(transportHigh,
                                                       transportLow,
                                                       serverMajor,
                                                       serverMinor);
            }
            catch (EndOfStreamException)
            {
                // Ideally we'd wrap the EndOfStreamException in the
                // MalformedFrameException, but unfortunately the
                // design of MalformedFrameException's superclass,
                // ProtocolViolationException, doesn't permit
                // this. Fortunately, the call stack in the
                // EndOfStreamException is largely irrelevant at this
                // point, so can safely be ignored.
                throw new MalformedFrameException("Invalid AMQP protocol header from server");
            }
        }
 public static IList ReadArray(NetworkBinaryReader reader)
 {
     IList array = new ArrayList();
     long arrayLength = reader.ReadUInt32();
     Stream backingStream = reader.BaseStream;
     long startPosition = backingStream.Position;
     while ((backingStream.Position - startPosition) < arrayLength)
     {
         object value = ReadFieldValue(reader);
         array.Add(value);
     }
     return array;
 }
 public MethodArgumentReader(NetworkBinaryReader reader)
 {
     m_reader = reader;
     ClearBits();
 }
 public abstract ContentHeaderBase DecodeContentHeaderFrom(NetworkBinaryReader reader);
 public abstract MethodBase DecodeMethodFrom(NetworkBinaryReader reader);
 public static byte[] ReadBytes(NetworkBinaryReader reader) {
     object value = ReadObject(reader);
     if (value == null) {
         return null;
     }
     if (value is byte[]) {
         return (byte[]) value;
     }
     PrimitiveParser.InvalidConversion("byte[]", value);
     return null;
 }
 public static string ReadUntypedString(NetworkBinaryReader reader) {
     BinaryWriter buffer = NetworkBinaryWriter.TemporaryBinaryWriter(256);
     while (true) {
         byte b = reader.ReadByte();
         if (b == 0) {
             return Encoding.UTF8.GetString(NetworkBinaryWriter.TemporaryContents(buffer));
         } else {
             buffer.Write(b);
         }
     }
 }
        ///<exception cref="EndOfStreamException"/>
        ///<exception cref="ProtocolViolationException"/>
        public static object ReadObject(NetworkBinaryReader reader) {
            int typeTag = reader.ReadByte();
            switch (typeTag) {
	      case -1:
		  throw new EndOfStreamException("End of StreamMessage reached");

              case (int) StreamWireFormattingTag.Bool: {
                  byte value = reader.ReadByte();
                  switch (value) {
                    case 0x00: return false;
                    case 0x01: return true;
                    default: {
                        string message =
                            string.Format("Invalid boolean value in StreamMessage: {0}", value);
                        throw new ProtocolViolationException(message);
                    }
                  }
              }

              case (int) StreamWireFormattingTag.Byte:
                  return reader.ReadByte();

              case (int) StreamWireFormattingTag.Bytes: {
                  int length = reader.ReadInt32();
                  if (length == -1) {
                      return null;
                  } else {
                      return reader.ReadBytes(length);
                  }
              }

              case (int) StreamWireFormattingTag.Int16:
                  return reader.ReadInt16();

              case (int) StreamWireFormattingTag.Char:
                  return (char) reader.ReadUInt16();

              case (int) StreamWireFormattingTag.Int32:
                  return reader.ReadInt32();

              case (int) StreamWireFormattingTag.Int64:
                  return reader.ReadInt64();

              case (int) StreamWireFormattingTag.Single:
                  return reader.ReadSingle();

              case (int) StreamWireFormattingTag.Double:
                  return reader.ReadDouble();

              case (int) StreamWireFormattingTag.String:
                  return ReadUntypedString(reader);

              case (int) StreamWireFormattingTag.Null:
                  return null;

              default: {
                  string message = string.Format("Invalid type tag in StreamMessage: {0}",
                                                 typeTag);
                  throw new ProtocolViolationException(message);
              }
            }
        }
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// 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.
        ///</remarks>
        public static IDictionary ReadTable(NetworkBinaryReader reader)
        {
            Hashtable table = new Hashtable();
            long tableLength = reader.ReadUInt32();

            Stream backingStream = reader.BaseStream;
            long startPosition = backingStream.Position;
            while ((backingStream.Position - startPosition) < tableLength)
            {
                string key = ReadShortstr(reader);
                object value = ReadFieldValue(reader);

                if (!table.ContainsKey(key))
                {
                    table[key] = value;
                }
            }

            return table;
        }
 public static decimal ReadDecimal(NetworkBinaryReader reader)
 {
     byte scale = ReadOctet(reader);
     uint unsignedMantissa = ReadLong(reader);
     return AmqpToDecimal(scale, unsignedMantissa);
 }
 public static AmqpTimestamp ReadTimestamp(NetworkBinaryReader reader)
 {
     ulong stamp = ReadLonglong(reader);
     // 0-9 is afaict silent on the signedness of the timestamp.
     // See also MethodArgumentWriter.WriteTimestamp and AmqpTimestamp itself
     return new AmqpTimestamp((long)stamp);
 }
        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 = ReadOctet(reader);
                      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;
        }
Ejemplo n.º 27
0
        public static Frame ReadFrom(NetworkBinaryReader reader)
        {
            int type;
            int channel;

            type = reader.ReadByte();

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            channel = reader.ReadUInt16();
            int payloadSize = reader.ReadInt32(); // FIXME - throw exn on unreasonable value
            byte[] payload = reader.ReadBytes(payloadSize);
            if (payload.Length != payloadSize)
            {
                // Early EOF.
                throw new MalformedFrameException("Short frame - expected " +
                                                  payloadSize + " bytes, got " +
                                                  payload.Length + " bytes");
            }

            int frameEndMarker = reader.ReadByte();
            if (frameEndMarker != CommonFraming.Constants.FrameEnd)
            {
                throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
            }

            return new Frame(type, channel, payload);
        }
 public static uint ReadLong(NetworkBinaryReader reader)
 {
     return reader.ReadUInt32();
 }
        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 string ReadString(NetworkBinaryReader reader) {
     object value = ReadObject(reader);
     if (value == null) {
         return null;
     }
     if (value is byte[]) {
         PrimitiveParser.InvalidConversion("string", value);
         return null;
     }
     return value.ToString();
 }