Beispiel #1
0
        private void WritePassword(MyCatPacket packet)
        {
            bool   secure   = (Flags & ClientFlags.SECURE_CONNECTION) != 0;
            object password = GetPassword();

            if (password is string)
            {
                if (secure)
                {
                    packet.WriteLenString((string)password);
                }
                else
                {
                    packet.WriteString((string)password);
                }
            }
            else if (password == null)
            {
                packet.WriteByte(0);
            }
            else if (password is byte[])
            {
                packet.Write(password as byte[]);
            }
            else
            {
                throw new MyCatException("Unexpected password format: " + password.GetType());
            }
        }
        private void WriteOldGuid(MyCatPacket packet, Guid guid, bool binary)
        {
            byte[] bytes = guid.ToByteArray();

            if (binary)
            {
                packet.WriteLength(bytes.Length);
                packet.Write(bytes);
            }
            else
            {
                packet.WriteStringNoNull("_binary ");
                packet.WriteByte((byte)'\'');
                EscapeByteArray(bytes, bytes.Length, packet);
                packet.WriteByte((byte)'\'');
            }
        }
Beispiel #3
0
        private static void EscapeByteArray(byte[] bytes, int length, MyCatPacket packet)
        {
            for (int x = 0; x < length; x++)
            {
                byte b = bytes[x];
                if (b == '\0')
                {
                    packet.WriteByte((byte)'\\');
                    packet.WriteByte((byte)'0');
                }

                else if (b == '\\' || b == '\'' || b == '\"')
                {
                    packet.WriteByte((byte)'\\');
                    packet.WriteByte(b);
                }
                else
                {
                    packet.WriteByte(b);
                }
            }
        }
Beispiel #4
0
        void IMyCatValue.WriteValue(MyCatPacket packet, bool binary, object val, int length)
        {
            byte[] buffToWrite = null;

            try
            {
                buffToWrite = ((MyCatGeometry)val)._valBinary;
            }
            catch
            {
                buffToWrite = val as Byte[];
            }

            if (buffToWrite == null)
            {
                MyCatGeometry v = new MyCatGeometry(0, 0);
                MyCatGeometry.TryParse(val.ToString(), out v);
                buffToWrite = v._valBinary;
            }

            byte[] result = new byte[GEOMETRY_LENGTH];

            for (int i = 0; i < buffToWrite.Length; i++)
            {
                if (buffToWrite.Length < GEOMETRY_LENGTH)
                {
                    result[i + 4] = buffToWrite[i];
                }
                else
                {
                    result[i] = buffToWrite[i];
                }
            }

            packet.WriteStringNoNull("_binary ");
            packet.WriteByte((byte)'\'');
            EscapeByteArray(result, GEOMETRY_LENGTH, packet);
            packet.WriteByte((byte)'\'');
        }
Beispiel #5
0
        void IMyCatValue.WriteValue(MyCatPacket packet, bool binary, object val, int length)
        {
            sbyte v = (val is sbyte) ? (sbyte)val : Convert.ToSByte(val);

            if (binary)
            {
                packet.WriteByte((byte)v);
            }
            else
            {
                packet.WriteStringNoNull(v.ToString());
            }
        }
        void IMyCatValue.WriteValue(MyCatPacket packet, bool binary, object val, int length)
        {
            if (!(val is TimeSpan))
            {
                throw new MyCatException("Only TimeSpan objects can be serialized by MyCatTimeSpan");
            }

            TimeSpan ts       = (TimeSpan)val;
            bool     negative = ts.TotalMilliseconds < 0;

            ts = ts.Duration();

            if (binary)
            {
                if (ts.Milliseconds > 0)
                {
                    packet.WriteByte(12);
                }
                else
                {
                    packet.WriteByte(8);
                }

                packet.WriteByte((byte)(negative ? 1 : 0));
                packet.WriteInteger(ts.Days, 4);
                packet.WriteByte((byte)ts.Hours);
                packet.WriteByte((byte)ts.Minutes);
                packet.WriteByte((byte)ts.Seconds);
                if (ts.Milliseconds > 0)
                {
                    long mval = ts.Milliseconds * 1000;
                    packet.WriteInteger(mval, 4);
                }
            }
            else
            {
                String s = String.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5:0000000}'",
                                         negative ? "-" : "", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Ticks % 10000000);

                packet.WriteStringNoNull(s);
            }
        }
        void IMyCatValue.WriteValue(MyCatPacket packet, bool binary, object value, int length)
        {
            MyCatDateTime dtValue;

            string valueAsString = value as string;

            if (value is DateTime)
            {
                dtValue = new MyCatDateTime(type, (DateTime)value);
            }
            else if (valueAsString != null)
            {
                dtValue = MyCatDateTime.Parse(valueAsString);
            }
            else if (value is MyCatDateTime)
            {
                dtValue = (MyCatDateTime)value;
            }
            else
            {
                throw new MyCatException("Unable to serialize date/time value.");
            }

            if (!binary)
            {
                SerializeText(packet, dtValue);
                return;
            }

            if (dtValue.Microsecond > 0)
            {
                packet.WriteByte(11);
            }
            else
            {
                packet.WriteByte(7);
            }

            packet.WriteInteger(dtValue.Year, 2);
            packet.WriteByte((byte)dtValue.Month);
            packet.WriteByte((byte)dtValue.Day);
            if (type == MyCatDbType.Date)
            {
                packet.WriteByte(0);
                packet.WriteByte(0);
                packet.WriteByte(0);
            }
            else
            {
                packet.WriteByte((byte)dtValue.Hour);
                packet.WriteByte((byte)dtValue.Minute);
                packet.WriteByte((byte)dtValue.Second);
            }

            if (dtValue.Microsecond > 0)
            {
                long val = dtValue.Microsecond;
                for (int x = 0; x < 4; x++)
                {
                    packet.WriteByte((byte)(val & 0xff));
                    val >>= 8;
                }
            }
        }