Beispiel #1
0
        public void WriteByteArrayToMemoryStream()
        {
            var stream = new MemoryStream();

            BigEndianUtilities.WriteByteArray(stream, new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef });

            CollectionAssert.AreEqual(new byte[] { 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01 }, stream.ToArray());
        }
Beispiel #2
0
 public void Pack(MemoryStream stream)
 {
     BigEndianUtilities.WriteByteArray(stream, TraceID);
     BigEndianUtilities.WriteByteArray(stream, ActivityID);
     BigEndianUtilities.WriteULong(stream, ActivitySequence);
 }
        public void Pack(MemoryStream stream)
        {
            if (Options.Count == 0 || Options[0].Type != TDSPreLoginOptionTokenType.Version || Options[Options.Count - 1].Type != TDSPreLoginOptionTokenType.Terminator || !Terminated)
            {
                throw new InvalidOperationException();
            }

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

            foreach (var option in Options)
            {
                // ToDo
                option.Offset = offset;
                option.Pack(stream);
            }

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

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

                case TDSPreLoginOptionTokenType.InstOpt:
                {
                    throw new NotSupportedException();
                }

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

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

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

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

                case TDSPreLoginOptionTokenType.Version:
                {
                    ClientVersion.Pack(stream);
                    break;
                }
                }
            }
        }
Beispiel #4
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;
                }
                }
            }
        }