Пример #1
0
        /// <summary>
        /// Loaded file from a cache to event arguments.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="callback">Callback which should be called to return tile if it is available or null.</param>
        public override void LoadAsync(string fileName, Action <byte[]> callback)
        {
            CacheRecord record = this.GetCacheRecord(fileName);

            byte[] tile = null;
            if (record == null)
            {
                ICacheRecordMetadata metaData = this.LoadFileMetadata(fileName);
                if (metaData != null && metaData.Expires >= DateTime.Now)
                {
                    Stream stream = this.OpenCacheStream(fileName);
                    if (stream.Length == 0)
                    {
                        stream.Dispose();
                    }
                    else
                    {
                        AsyncStreamOperations.LoadAsync(stream, callback);
                        return;
                    }
                }
            }
            else
            {
                tile = record.TileBody;
            }

            callback(tile);
        }
Пример #2
0
        /// <summary>
        /// Saves byte array asynchronously to the stream.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <param name="tile">Byte array.</param>
        /// <param name="callback">Callback.</param>
        /// <param name="userState">User state.</param>
        public static void SaveAsync(Stream stream, byte[] tile, Action <object> callback, object userState)
        {
            AsyncStreamOperations writer = new AsyncStreamOperations(stream);

            writer.writerCallback = callback;
            writer.userState      = userState;
            writer.tileBodyPart   = tile;

            writer.stream.BeginWrite(writer.tileBodyPart, 0, writer.tileBodyPart.Length, WriteCallback, writer);
        }
Пример #3
0
        /// <summary>
        /// Loads stream asynchronously to byte array.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <param name="callback">Callback.</param>
        public static void LoadAsync(Stream stream, Action <byte[]> callback)
        {
            AsyncStreamOperations loader = new AsyncStreamOperations(stream);

            loader.readerCallback = callback;
            loader.memoryStream   = new MemoryStream();
            loader.tileBodyPart   = new byte[TileBodyPartLength];

            loader.stream.BeginRead(loader.tileBodyPart, 0, TileBodyPartLength, ReadCallback, loader);
        }
Пример #4
0
        private static void WriteCallback(IAsyncResult asyncResult)
        {
            AsyncStreamOperations writer = (AsyncStreamOperations)asyncResult.AsyncState;

            writer.stream.EndWrite(asyncResult);
            writer.Dispose();
            if (writer.writerCallback != null)
            {
                writer.writerCallback(writer.userState);
            }
            writer = null;
        }
Пример #5
0
        private static void ReadCallback(IAsyncResult asyncResult)
        {
            AsyncStreamOperations loader = (AsyncStreamOperations)asyncResult.AsyncState;
            int loaded = loader.stream.EndRead(asyncResult);

            if (asyncResult.IsCompleted && loaded > 0)
            {
                loader.memoryStream.Write(loader.tileBodyPart, 0, loaded);
                loader.stream.BeginRead(loader.tileBodyPart, 0, TileBodyPartLength, ReadCallback, loader);
                return;
            }

            byte[] tile = loader.memoryStream.ToArray();
            loader.Dispose();

            loader.readerCallback(tile);
            loader = null;
        }
Пример #6
0
        private void SaveCacheFile(CacheRecord cacheRecord)
        {
            Stream cacheStream = null;

            try
            {
                cacheStream = this.CreateCacheStream(cacheRecord.FileName);
            }
            catch
            {
            }

            if (cacheStream != null)
            {
                AsyncStreamOperations.SaveAsync(cacheStream, cacheRecord.TileBody, this.SaveFileMetadataCallback, cacheRecord);
            }
            else
            {
                cacheRecord.CompeleEvent.Set();
            }
        }