Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to deserialize an array from the given source buffer.
        /// </summary>
        /// <param name="source">The target buffer. In the event of success, the current position of the buffer will be advanced.</param>
        /// <param name="value">The deserialized array, if this method returns true.</param>
        /// <returns>True if deserialization succeeded, otherwise false.</returns>
        public unsafe static bool TryDeserialize(ref RecvBuffer source, out float[] value)
        {
            int resetPosition = source.CurrentPos;

            int arrayLength = -1;

            if (!TryDeserialize(ref source, out arrayLength))
            {
                value = default(float[]);
                return(false);
            }

            if (source.End - source.CurrentPos < sizeof(float) * arrayLength)
            {
                source.CurrentPos = resetPosition;
                value             = default(float[]);
                return(false);
            }

            value = new float[arrayLength];

#if false
            fixed(byte *bufPtr = source.Buffer)
            {
                float *arrayStart = (float *)(bufPtr + source.CurrentPos);

                for (int i = 0; i < arrayLength; ++i)
                {
                    value[i] = arrayStart[i];
                }
            }
#else
            Buffer.BlockCopy(source.Buffer, source.CurrentPos, value, 0, arrayLength * sizeof(float));
#endif
            source.CurrentPos += arrayLength * sizeof(float);

            return(true);
        }