Example #1
0
        private static void SerializeDouble(Protocol16Stream output, double value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Double, writeTypeCode);
            var doubleBuffer = _doubleBuffer.Value;

            doubleBuffer[0] = value;
            var buffer = _byteBuffer.Value;

            Buffer.BlockCopy(doubleBuffer, 0, buffer, 0, sizeof(double));
            if (BitConverter.IsLittleEndian)
            {
                byte b0 = buffer[0];
                byte b1 = buffer[1];
                byte b2 = buffer[2];
                byte b3 = buffer[3];
                buffer[0] = buffer[7];
                buffer[1] = buffer[6];
                buffer[2] = buffer[5];
                buffer[3] = buffer[4];
                buffer[4] = b3;
                buffer[5] = b2;
                buffer[6] = b1;
                buffer[7] = b0;
            }
            output.Write(buffer, 0, sizeof(double));
        }
Example #2
0
        private static void SerializeShort(Protocol16Stream output, short value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Short, writeTypeCode);
            var buffer = _byteBuffer.Value;

            buffer[0] = (byte)(value >> 8);
            buffer[1] = (byte)(value);
            output.Write(buffer, 0, sizeof(short));
        }
Example #3
0
 private static void SerializeString(Protocol16Stream output, string value, bool writeTypeCode)
 {
     output.WriteTypeCodeIfTrue(Protocol16Type.String, writeTypeCode);
     byte[] bytes = Encoding.UTF8.GetBytes(value);
     if (bytes.Length > short.MaxValue)
     {
         throw new NotSupportedException($"Strings that exceed a UTF8-encoded byte-length of {short.MaxValue} (short.MaxValue) are not supported. Yours is: {bytes.Length}");
     }
     SerializeShort(output, (short)bytes.Length, false);
     output.Write(bytes, 0, bytes.Length);
 }
Example #4
0
        private static void SerializeInteger(Protocol16Stream output, int value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Integer, writeTypeCode);
            var buffer = _byteBuffer.Value;

            buffer[0] = (byte)(value >> 24);
            buffer[1] = (byte)(value >> 16);
            buffer[2] = (byte)(value >> 8);
            buffer[3] = (byte)(value);
            output.Write(buffer, 0, sizeof(int));
        }
Example #5
0
        private static void SerializeIntArray(Protocol16Stream output, int[] ints, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.IntegerArray, writeTypeCode);
            SerializeInteger(output, ints.Length, false);
            var array = new byte[ints.Length * sizeof(int)];
            var idx   = 0;

            foreach (var number in ints)
            {
                array[idx++] = (byte)(number >> 24);
                array[idx++] = (byte)(number >> 16);
                array[idx++] = (byte)(number >> 8);
                array[idx++] = (byte)(number);
            }
            output.Write(array, 0, array.Length);
        }
Example #6
0
        private static void SerializeFloat(Protocol16Stream output, float value, bool writeTypeCode)
        {
            output.WriteTypeCodeIfTrue(Protocol16Type.Float, writeTypeCode);
            var floatBuffer = _floatBuffer.Value;

            floatBuffer[0] = value;
            var buffer = _byteBuffer.Value;

            Buffer.BlockCopy(floatBuffer, 0, buffer, 0, sizeof(float));
            if (BitConverter.IsLittleEndian)
            {
                byte b0 = buffer[0];
                byte b1 = buffer[1];
                buffer[0] = buffer[3];
                buffer[1] = buffer[2];
                buffer[2] = b1;
                buffer[3] = b0;
            }
            output.Write(buffer, 0, sizeof(float));
        }
Example #7
0
 private static void SerializeArraySegment(Protocol16Stream output, ArraySegment <byte> arraySegment, bool writeTypeCode)
 {
     output.WriteTypeCodeIfTrue(Protocol16Type.ByteArray, writeTypeCode);
     SerializeInteger(output, arraySegment.Count, false);
     output.Write(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
 }
Example #8
0
 private static void SerializeByteArray(Protocol16Stream output, byte[] obj, bool writeTypeCode)
 {
     output.WriteTypeCodeIfTrue(Protocol16Type.ByteArray, writeTypeCode);
     SerializeInteger(output, obj.Length, false);
     output.Write(obj, 0, obj.Length);
 }