/// <summary> /// 开始读取文件 /// </summary> public unsafe void Read() { do { var readSize = index.Size + sizeof(int); try { if (FileStream.isDisposed == 0) { TmphBuffer = (memoryPool = TmphMemoryPool.GetPool(readSize)).Get(); var fileReader = FileStream.fileReader; var offset = fileReader.Position - index.Index; if (offset >= 0 || -offset < index.Index) fileReader.Seek(offset, SeekOrigin.Current); else fileReader.Seek(index.Index, SeekOrigin.Begin); if (fileReader.Read(TmphBuffer, 0, readSize) == readSize) { fixed (byte* bufferFixed = TmphBuffer) { if (*(int*)bufferFixed == index.Size) readSize = index.Size; else TmphLog.Default.Add( FileStream.FileName + " index[" + index.Index.toString() + "] size[" + (*(int*)bufferFixed).toString() + "]<>" + index.Size.toString(), false, false); } } else readSize = 0; } } catch (Exception error) { TmphLog.Default.Add(error, null, false); } var onReaded = this.onReaded; if (readSize == index.Size) { if ( onReaded(new TmphTcpBase.TmphSubByteArrayEvent { TmphBuffer = TmphSubArray<byte>.Unsafe(TmphBuffer, sizeof(int), index.Size), Event = memoryPool.PushSubArray })) TmphBuffer = null; else memoryPool.Push(ref TmphBuffer); } else { onReaded(default(TmphTcpBase.TmphSubByteArrayEvent)); if (memoryPool != null) memoryPool.Push(ref TmphBuffer); } var next = FileStream.next(this); if (next == null) { FileStream = null; onReaded = null; memoryPool = null; TmphTypePool<TmphReader>.Push(this); return; } onReaded = next.onReaded; index = next.index; next.onReaded = null; TmphTypePool<TmphReader>.Push(next); } while (true); }
/// <summary> /// 文件读取 /// </summary> /// <param name="index">读取文件位置</param> /// <param name="onReaded">读取文件回调函数</param> /// <returns></returns> public static TmphReader Get(TmphIndex index, Func<TmphAsynchronousMethod.TmphReturnValue<TmphTcpBase.TmphSubByteArrayEvent>, bool> onReaded) { var reader = TmphTypePool<TmphReader>.Pop(); if (reader == null) { try { reader = new TmphReader(); } catch { return null; } } reader.onReaded = onReaded; reader.index = index; return reader; }
/// <summary> /// 读取文件分块数据//showjim+cache /// </summary> /// <param name="index">文件分块数据位置</param> /// <param name="size">文件分块字节大小</param> /// <param name="onReaded"></param> internal unsafe void Read(TmphIndex index, Func<TmphAsynchronousMethod.TmphReturnValue<TmphTcpBase.TmphSubByteArrayEvent>, bool> onReaded) { if (onReaded == null) TmphLog.Error.Throw(TmphLog.TmphExceptionType.Null); var endIndex = index.EndIndex; if (index.Size > 0 && ((int)index.Index & 3) == 0 && endIndex <= fileBufferLength) { if (endIndex <= fileLength) { var reader = TmphReader.Get(index, onReaded); if (reader != null) { if (set(reader)) TmphThreadPool.TinyPool.FastStart(reader.ReadHandle, null, null); return; } } else { TmphMemoryPool memoryPool = null; byte[] TmphBuffer = null; var copyedSize = int.MinValue; TmphInterlocked.NoCheckCompareSetSleep0(ref bufferLock); if (isDisposed == 0) { if (index.Index >= bufferIndex) { index.Index -= bufferIndex; try { TmphBuffer = (memoryPool = TmphMemoryPool.GetPool(index.Size)).Get(index.Size); foreach (var nextData in buffers.array) { var data = nextData.SubArray; if (index.Index != 0) { if (index.Index >= data.Count) { index.Index -= data.Count; continue; } data.UnsafeSet(data.StartIndex + (int)index.Index, data.Count - (int)index.Index); index.Index = 0; } if (copyedSize < 0) { fixed (byte* dataFixed = data.array) { if (*(int*)(dataFixed + data.StartIndex) != index.Size) break; } if ((copyedSize = data.Count - sizeof(int)) == 0) continue; data.UnsafeSet(data.StartIndex + sizeof(int), copyedSize); copyedSize = 0; } var copySize = index.Size - copyedSize; if (data.Count >= copySize) { Buffer.BlockCopy(data.array, data.StartIndex, TmphBuffer, copyedSize, copySize); copyedSize = index.Size; break; } Buffer.BlockCopy(data.array, data.StartIndex, TmphBuffer, copyedSize, copySize); copyedSize += copySize; } } catch (Exception error) { TmphLog.Default.Add(error, null, false); } finally { bufferLock = 0; } if (copyedSize == index.Size) { onReaded(new TmphTcpBase.TmphSubByteArrayEvent { TmphBuffer = TmphSubArray<byte>.Unsafe(TmphBuffer, 0, index.Size), Event = memoryPool.PushSubArray }); return; } } else { bufferLock = 0; var reader = TmphReader.Get(index, onReaded); if (reader != null) { TmphThreadPool.TinyPool.FastStart(waitHandle, reader, null, null); return; } } } else bufferLock = 0; } } onReaded(default(TmphTcpBase.TmphSubByteArrayEvent)); }