コード例 #1
0
        /// <summary>
        /// Deserialize an object from a byte array asynchronously, using user defined logic.
        /// </summary>
        /// <param name="data">The byte array containing the serialized data.</param>
        /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
        /// <param name="callback">A callback to get the deserialized object.</param>
        /// <param name="progress">A callback to get progress notifications.</param>
        /// <returns>An enumerator to wait for the deserialization completion.</returns>
        public IEnumerator Deserialize(byte[] data, DeserializationCallback deserialization_callback, Action <float> progress = null)
        {
            DateTime time = DateTime.Now + progressRefresRate;
            float    progress_percentage = 0f;

            using (Buffer buffer = GetBufferFromExistingData(data, p => progress_percentage = p))
            {
                Task <uint> result = Task.Run(() => deserialization_callback(this, buffer));

                while (!result.IsCompleted)
                {
                    if (progress != null && DateTime.Now >= time)
                    {
                        progress(progress_percentage);

                        time = DateTime.Now + progressRefresRate;
                    }

                    yield return(null);
                }

                if (result.Exception != null)
                {
                    throw new DeserializationException("An error occured during deserialization.", result.Exception);
                }
                else if (result.Result != data.Length)
                {
                    throw new DeserializationException("Invalid deserialization. Not all available data was used.", null);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Deserialize an object from a byte array synchronously, using user defined logic.
        /// </summary>
        /// <param name="data">The byte array containing the serialized data.</param>
        /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
        public void Deserialize(byte[] data, DeserializationCallback deserialization_callback)
        {
            using (Buffer buffer = GetBufferFromExistingData(data))
            {
                try
                {
                    uint read = deserialization_callback(this, buffer);

                    if (read != data.Length)
                    {
                        throw new DeserializationException("Not all available data was used.", null);
                    }
                }
                catch (Exception e)
                {
                    throw new DeserializationException("An error occured during deserialization.", e);
                }
            }
        }
コード例 #3
0
ファイル: HighLevelAPI.cs プロジェクト: clarte53/clarte-utils
        /// <summary>
        /// Deserialize an object from a buffer synchronously, using user defined logic.
        /// </summary>
        /// <param name="buffer">The buffer containing the serialized data.</param>
        /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
        public void Deserialize(Buffer buffer, DeserializationCallback deserialization_callback)
        {
            using (buffer)
            {
                try
                {
                    uint read = deserialization_callback(this, buffer);

                    if (read != buffer.Size)
                    {
                        throw new DeserializationException("Not all available data was used.", null);
                    }
                }
                catch (Exception e)
                {
                    throw new DeserializationException("An error occured during deserialization.", e);
                }
            }
        }
コード例 #4
0
ファイル: HighLevelAPI.cs プロジェクト: clarte53/clarte-utils
        /// <summary>
        /// Deserialize an object from a byte array asynchronously, using user defined logic.
        /// </summary>
        /// <param name="buffer">The buffer containing the serialized data.</param>
        /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
        /// <param name="progress">A callback to get progress notifications.</param>
        /// <returns>An enumerator to wait for the deserialization completion.</returns>
        public IEnumerator Deserialize(Buffer buffer, DeserializationCallback deserialization_callback, Action <float> progress = null)
        {
            DateTime time = DateTime.Now + progressRefresRate;
            float    progress_percentage = 0f;

            using (buffer)
            {
                if (buffer.Context.progress == null)
                {
                    buffer.Context.progress = p => progress_percentage = p;
                }

                Task <uint> result = Task.Run(() => deserialization_callback(this, buffer));

                while (!result.IsCompleted)
                {
                    if (progress != null && DateTime.Now >= time)
                    {
                        progress(progress_percentage);

                        time = DateTime.Now + progressRefresRate;
                    }

                    yield return(null);
                }

                if (result.Exception != null)
                {
                    throw new DeserializationException("An error occured during deserialization.", result.Exception);
                }
                else if (result.Result != buffer.Size)
                {
                    throw new DeserializationException("Not all available data was used.", null);
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Deserialize an object from a file asynchronously, using user defined logic.
        /// </summary>
        /// <param name="filename">The name of the file where to get the deserialized data.</param>
        /// /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
        /// <param name="progress">A callback to get progress notifications.</param>
        /// <returns>An enumerator to wait for the deserialization completion.</returns>
        public IEnumerator Deserialize(string filename, DeserializationCallback deserialization_callback, Action <float> progress = null)
        {
            byte[] data = System.IO.File.ReadAllBytes(filename);

            return(Deserialize(data, deserialization_callback, progress));
        }
コード例 #6
0
ファイル: HighLevelAPI.cs プロジェクト: clarte53/clarte-utils
 /// <summary>
 /// Deserialize an object from a byte array synchronously, using user defined logic.
 /// </summary>
 /// <param name="data">The byte array containing the serialized data.</param>
 /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
 public void Deserialize(byte[] data, DeserializationCallback deserialization_callback)
 {
     Deserialize(GetBufferFromExistingData(data, false), deserialization_callback);
 }
コード例 #7
0
ファイル: HighLevelAPI.cs プロジェクト: clarte53/clarte-utils
 /// <summary>
 /// Deserialize an object from a file asynchronously, using user defined logic.
 /// </summary>
 /// <param name="filename">The name of the file where to get the deserialized data.</param>
 /// /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
 /// <param name="progress">A callback to get progress notifications.</param>
 /// <returns>An enumerator to wait for the deserialization completion.</returns>
 public IEnumerator Deserialize(string filename, DeserializationCallback deserialization_callback, Action <float> progress = null)
 {
     return(Deserialize(ReadFromFile(filename), deserialization_callback, progress));
 }
コード例 #8
0
ファイル: HighLevelAPI.cs プロジェクト: clarte53/clarte-utils
 /// <summary>
 /// Deserialize an object from a byte array asynchronously, using user defined logic.
 /// </summary>
 /// <param name="data">The byte array containing the serialized data.</param>
 /// <param name="deserialization_callback">The callback used to deserialize the data once the context is set.</param>
 /// <param name="callback">A callback to get the deserialized object.</param>
 /// <param name="progress">A callback to get progress notifications.</param>
 /// <returns>An enumerator to wait for the deserialization completion.</returns>
 public IEnumerator Deserialize(byte[] data, DeserializationCallback deserialization_callback, Action <float> progress = null)
 {
     return(Deserialize(GetBufferFromExistingData(data, false), deserialization_callback, progress));
 }