/// <summary> /// 添加数据 /// </summary> /// <param name="buffer">数据</param> /// <param name="onWrite">添加数据回调委托</param> void IBlock.Append(ref AppendBuffer buffer, AutoCSer.Net.TcpServer.ServerCallback <ulong> onWrite) { if (isDisposed == 0) { ulong index = buffer.Index & Server.MaxIndex; if (index == 0 || (long)index + (buffer.Buffer.Length + sizeof(int)) > fileLength) { if (writeRequests.IsPushHead(new WriteRequest(ref buffer, onWrite))) { writeWait.Set(); } if (isDisposed != 0) { for (WriteRequest head = writeRequests.GetClear(); head != null; head = head.Error()) { ; } } } else { ReadRequest readRequest = new ReadRequest { Index = (long)index, Size = buffer.Buffer.Length }; readRequest.WriteRequest = new FileWriteRequest(ref buffer, onWrite, this); if (readRequests.IsPushHead(readRequest)) { readWait.Set(); } if (isDisposed != 0) { for (ReadRequest head = readRequests.GetClear(); head != null; head = head.Error()) { ; } for (WriteRequest head = writeRequests.GetClear(); head != null; head = head.Error()) { ; } } } } else { onWrite.Callback(0); } }
internal void Append(FileWriteRequest writeRequest) { if (isDisposed == 0) { if (writeRequests.IsPushHead(writeRequest)) { writeWait.Set(); } if (isDisposed != 0) { for (WriteRequest head = writeRequests.GetClear(); head != null; head = head.Error()) { ; } } } else { writeRequest.Error(); } }
/// <summary> /// 写入数据线程 /// </summary> private void write() { WriteRequest next; using (writeStream) { do { if (isDisposed == 0) { writeWait.Wait(); } WriteRequest head = writeRequests.GetClear(); if (head == null) { return; } do { try { do { next = head.Write(this); head.LinkNext = waitFlushRequest; waitFlushRequest = head; head = next; if (waitFlushFileSize < 0) { while (waitFlushRequest != null) { waitFlushRequest = waitFlushRequest.OnFlush(); } waitFlushFileSize = maxFlushSize; } }while (head != null); break; } catch (Exception error) { log.Add(LogType.Error, error); } if (head == null) { break; } head = head.Error(); }while (head != null); if (waitFlushRequest != null) { try { writeStream.Flush(true); do { waitFlushRequest = waitFlushRequest.OnFlush(); }while (waitFlushRequest != null); waitFlushFileSize = maxFlushSize; } catch (Exception error) { log.Add(LogType.Error, error); while (waitFlushRequest != null) { waitFlushRequest = waitFlushRequest.Error(); } } } }while (true); } }