public void Flush(LoopbackSession session) { IWriteRequestQueue queue = session.WriteRequestQueue; if (!session.Closing) { lock (session.Lock) { try { if (queue.IsEmpty(session)) { return; } IWriteRequest req; DateTime currentTime = DateTime.Now; while ((req = queue.Poll(session)) != null) { Object m = req.Message; _chain.PushEvent(new IoEvent(IoEventType.MessageSent, session, req), false); session.RemoteSession.FilterChain.FireMessageReceived(GetMessageCopy(m)); IoBuffer buf = m as IoBuffer; if (buf != null) { session.IncreaseWrittenBytes(buf.Remaining, currentTime); } } } finally { if (_chain._flushEnabled) { _chain.FlushEvents(); } } } FlushPendingDataQueues(session); } else { List <IWriteRequest> failedRequests = new List <IWriteRequest>(); IWriteRequest req; while ((req = queue.Poll(session)) != null) { failedRequests.Add(req); } if (failedRequests.Count > 0) { WriteToClosedSessionException cause = new WriteToClosedSessionException(failedRequests); foreach (IWriteRequest r in failedRequests) { r.Future.Exception = cause; } session.FilterChain.FireExceptionCaught(cause); } } }
private void ClearWriteRequestQueue(SocketSession session) { IWriteRequestQueue writeRequestQueue = session.WriteRequestQueue; IWriteRequest req; List <IWriteRequest> failedRequests = new List <IWriteRequest>(); if ((req = writeRequestQueue.Poll(session)) != null) { IoBuffer buf = req.Message as IoBuffer; if (buf != null) { // The first unwritten empty buffer must be // forwarded to the filter chain. if (buf.HasRemaining) { buf.Reset(); failedRequests.Add(req); } else { session.FilterChain.FireMessageSent(req); } } else { failedRequests.Add(req); } // Discard others. while ((req = writeRequestQueue.Poll(session)) != null) { failedRequests.Add(req); } } // Create an exception and notify. if (failedRequests.Count > 0) { WriteToClosedSessionException cause = new WriteToClosedSessionException(failedRequests); foreach (IWriteRequest r in failedRequests) { //session.DecreaseScheduledBytesAndMessages(r); r.Future.Exception = cause; } session.FilterChain.FireExceptionCaught(cause); } }
/// <inheritdoc/> public IWriteRequest Poll(IoSession session) { IWriteRequest answer = _queue.Poll(session); if (Object.ReferenceEquals(answer, CLOSE_REQUEST)) { _session.Close(); Dispose(_session); answer = null; } return(answer); }