public TimestampedMsg <In> Peek() { if (_broken) { throw new ConnectionReadError(); } TimestampedMsg <In> msg = _queue.Peek(); if (msg == null) { _broken = true; throw new ConnectionReadError(); } return(msg); }
public bool PeekWithTimeout(TimeSpan timeout, out TimestampedMsg <In> msg) { if (_broken) { throw new ConnectionReadError(); } if (!_queue.PeekWithTimeout(timeout, out msg)) { return(false); } if (msg == null) { _broken = true; throw new ConnectionReadError(); } return(true); }
// Processes the reply to the last request that we sent. // It's the caller's responsibility to guarantee that this message is indeed the // reply and not some other unrelated message. public void OnReply(TimestampedMsg <Req> msg) { Condition.Requires(msg, "msg").IsNotNull(); Callback cb; lock (_monitor) { if (_inflight == null) { _log.Error("Received response to a request we didn't send: ({0}) {1}", msg.Value.GetType(), msg.Value); return; } cb = _inflight; _inflight = null; } _connection.Scheduler.Schedule(() => cb.Done(msg)); _queue.TryProcess(); }
// Runs in the scheduler thread. void HandleMessage(IConnection <In, Out> connection, TimestampedMsg <In> msg) { // We can read _connection without a lock because we are in the scheduler thread. // _connection can't be modified while we are running. if (!Object.ReferenceEquals(connection, _connection)) { _log.Info("Message belongs to a stale connection. Dropping it."); return; } if (msg == null) { // Null message means unrecoverable network read error. Reconnect(); } else { // If it throws, that's OK. The exception will be caught and logged // by the scheduler. OnMessage?.Invoke(msg); } }
public void Push(TimestampedMsg <In> msg) { _queue.Push(msg); }