Exemplo n.º 1
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");
            }
        }
Exemplo n.º 2
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));
 }
Exemplo n.º 4
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 byte ReadOctet(NetworkBinaryReader reader)
 {
     return reader.ReadByte();
 }
        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;
        }
 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.
        ///</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 = 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 '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);
                }

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

            return table;
        }