Ejemplo n.º 1
0
        public void WriteUIntToMemoryStream()
        {
            var stream = new MemoryStream();

            BigEndianUtilities.WriteUInt(stream, 0x01234567);

            CollectionAssert.AreEqual(new byte[] { 0x01, 0x23, 0x45, 0x67 }, stream.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Used to pack IPackageable to a stream.
        /// </summary>
        /// <param name="stream">MemoryStream in which IPackageable is packet into.</param>
        public void Pack(MemoryStream stream)
        {
            if (this.Options.Count == 0 || this.Options[0].Type != TDSPreLoginOptionTokenType.Version || this.Options[this.Options.Count - 1].Type != TDSPreLoginOptionTokenType.Terminator || !this.Terminated)
            {
                throw new InvalidOperationException();
            }

            var offset = (ushort)(((this.Options.Count - 1) * ((2 * sizeof(ushort)) + sizeof(byte))) + sizeof(byte));

            foreach (var option in this.Options)
            {
                option.Offset = offset;
                offset       += option.Length;
                option.Pack(stream);
            }

            foreach (var option in this.Options)
            {
                switch (option.Type)
                {
                case TDSPreLoginOptionTokenType.Encryption:
                {
                    stream.WriteByte((byte)this.Encryption);
                    break;
                }

                case TDSPreLoginOptionTokenType.FedAuthRequired:
                {
                    if (this.FedAuthRequired)
                    {
                        stream.WriteByte(0x01);
                    }
                    else
                    {
                        stream.WriteByte(0x00);
                    }

                    break;
                }

                case TDSPreLoginOptionTokenType.InstOpt:
                {
                    if (this.Instance != null && this.Instance.Length != 0)
                    {
                        stream.Write(this.Instance, 0, this.Instance.Length);
                    }
                    else
                    {
                        stream.WriteByte(0x00);
                    }

                    break;
                }

                case TDSPreLoginOptionTokenType.MARS:
                {
                    if (this.MARS)
                    {
                        stream.WriteByte(0x01);
                    }
                    else
                    {
                        stream.WriteByte(0x00);
                    }

                    break;
                }

                case TDSPreLoginOptionTokenType.NonceOpt:
                {
                    BigEndianUtilities.WriteByteArray(stream, this.Nonce);
                    break;
                }

                case TDSPreLoginOptionTokenType.ThreadID:
                {
                    BigEndianUtilities.WriteUInt(stream, this.ThreadID);
                    break;
                }

                case TDSPreLoginOptionTokenType.TraceID:
                {
                    this.TraceID.Pack(stream);
                    break;
                }

                case TDSPreLoginOptionTokenType.Version:
                {
                    this.ClientVersion.Pack(stream);
                    break;
                }
                }
            }
        }