private void read(fileBlockStream.index index, ref fastCSharp.code.cSharp.tcpBase.subByteArrayEvent buffer, Func<fastCSharp.code.cSharp.asynchronousMethod.returnValue<fastCSharp.code.cSharp.tcpBase.subByteArrayEvent>, bool> onReturn) { fileBlockStream fileStream = this.fileStream; if (fileStream == null) onReturn(default(fastCSharp.code.cSharp.tcpBase.subByteArrayEvent)); else fileStream.Read(index, onReturn); }
/// <summary> /// 开始读取文件 /// </summary> public unsafe void Read() { do { int readSize = index.Size + sizeof(int); try { if (FileStream.isDisposed == 0) { buffer = (memoryPool = memoryPool.GetPool(readSize)).Get(); FileStream fileReader = FileStream.fileReader; long 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(buffer, 0, readSize) == readSize) { fixed (byte* bufferFixed = buffer) { if (*(int*)bufferFixed == index.Size) readSize = index.Size; else log.Default.Add(FileStream.FileName + " index[" + index.Index.toString() + "] size[" + (*(int*)bufferFixed).toString() + "]<>" + index.Size.toString(), false, false); } } else readSize = 0; } } catch (Exception error) { log.Default.Add(error, null, false); } Func<fastCSharp.code.cSharp.asynchronousMethod.returnValue<fastCSharp.code.cSharp.tcpBase.subByteArrayEvent>, bool> onReaded = this.onReaded; if (readSize == index.Size) { if (onReaded(new fastCSharp.code.cSharp.tcpBase.subByteArrayEvent { Buffer = subArray<byte>.Unsafe(buffer, sizeof(int), index.Size), Event = memoryPool.PushSubArray })) buffer = null; else memoryPool.Push(ref buffer); } else { onReaded(default(fastCSharp.code.cSharp.tcpBase.subByteArrayEvent)); if (memoryPool != null) memoryPool.Push(ref buffer); } reader next = FileStream.next(this); if (next == null) { FileStream = null; onReaded = null; memoryPool = null; typePool<reader>.Push(this); return; } onReaded = next.onReaded; index = next.index; next.onReaded = null; typePool<reader>.Push(next); } while (true); }
/// <summary> /// 文件分块服务 /// </summary> /// <param name="fileName">文件全名</param> public fileBlockServer(string fileName) { fileStream = new fileBlockStream(fileName); }