public long?Read() { if (ReadQueue.Any()) { return(ReadQueue.Dequeue()); } else { return(null); } }
/// <summary> /// 处理已经解析出来的命令。 /// </summary> private void HandleReceiveCmds() { while (ReadQueue.Count > 0) { Log(Name, string.Format("将收到得消息丢给上层处理,时间:{0}", DataTime2String(DateTime.Now))); Packet packet = ReadQueue.Dequeue() as Packet; if (packet != null) { HandleCmd(packet); } } }
void ProcessBufferedIO(bool force = false) { BufferedIO io; while (WriteQueue.Count > 0) { io = WriteQueue.Peek(); // This means we wanted to wait until all the writes had been flushed // before we attempt to generate the hash of a given piece. if (io.manager == null && io.buffer == null) { io = WriteQueue.Dequeue(); io.tcs.SetResult(true); continue; } if (!force && !WriteLimiter.TryProcess(io.count)) { break; } io = WriteQueue.Dequeue(); try { Interlocked.Add(ref pendingWrites, -io.count); Write(io.manager, io.offset, io.buffer, io.count); io.tcs.SetResult(true); } catch (Exception ex) { io.tcs.SetException(ex); } } while (ReadQueue.Count > 0) { if (!force && !ReadLimiter.TryProcess(ReadQueue.Peek().count)) { break; } io = ReadQueue.Dequeue(); try { Interlocked.Add(ref pendingReads, -io.count); var result = Read(io.manager, io.offset, io.buffer, io.count); io.tcs.SetResult(result); } catch (Exception ex) { io.tcs.SetException(ex); } } }