imsd: what the client flow already holds is read before an SMS wait is judged
The run loop judged the wait of the message in flight — its MESSAGE's final, its submit report — after draining the server ports and before its read of the client flow. A final that had arrived in time and was still unread at that pass counted as none: the segment was sent again — before the repeat existed, the message failed — and the final then found nothing to own (issue #3, the third of its three cases). The pass now reads what the flow holds, without waiting, before it judges: SipFlow::RecvReady returns a message that is whole in the frame buffer or readable on the socket now. RecvMsg's read of the socket moved into a helper both use (ReadOnce), and the handling of a message read from the flow into OnFlowMessage, for the loop's two reads. Tested on the workstation's loopback with the daemon's own SipFlow built around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture, flow-read): over TCP and over UDP RecvReady returns a message the socket holds without waiting and none when it holds none, and RecvMsg waits its time and returns a message when one comes; over TCP RecvReady leaves a message that is not whole yet. On the dev phone the binary of this range's last commit judged a report's wait at that pass, 130.06 s after the final, and sent the repeat (the capture's phone-form6, visit-journal.txt); the pass has not run with a final waiting unread: that takes a final timed to it.
This commit is contained in:
parent
23892e8e81
commit
1fa4d77fcd
2 changed files with 111 additions and 57 deletions
20
AGENTS.md
20
AGENTS.md
|
|
@ -151,7 +151,8 @@ status-report book's entries (made at a message's start, and only for one
|
||||||
that asked for a report: `StartNextSms`), the routing of a MESSAGE
|
that asked for a report: `StartNextSms`), the routing of a MESSAGE
|
||||||
final to the message in flight (`OnSmsResponse` — ahead of the reg-event
|
final to the message in flight (`OnSmsResponse` — ahead of the reg-event
|
||||||
subscription's handler in the run loop, after it inside `AwaitFinal`), the
|
subscription's handler in the run loop, after it inside `AwaitFinal`), the
|
||||||
judgement of the final's wait and of the report's after the server ports' drain, the reassembler's
|
judgement of the final's wait and of the report's after the server ports' drain and the read of what
|
||||||
|
the client flow already holds, the reassembler's
|
||||||
30-minute age-out in the run loop, the retransmission check around
|
30-minute age-out in the run loop, the retransmission check around
|
||||||
`OnSmsMessage`, the delivery reports' finals (`reportCallIds_`), a send
|
`OnSmsMessage`, the delivery reports' finals (`reportCallIds_`), a send
|
||||||
failure attributed by Call-ID, and the forwarding of a status report's
|
failure attributed by Call-ID, and the forwarding of a status report's
|
||||||
|
|
@ -763,7 +764,17 @@ Rules:
|
||||||
a 408, a 504 or an RP-ERROR; a repeat rejected as a duplicate, and one
|
a 408, a 504 or an RP-ERROR; a repeat rejected as a duplicate, and one
|
||||||
ended in its turn; the first attempt's report after its repeat went
|
ended in its turn; the first attempt's report after its repeat went
|
||||||
out.
|
out.
|
||||||
`tests/Engine` pins the reducer for each.
|
`tests/Engine` pins the reducer for each. The read of the client flow before a wait is
|
||||||
|
judged: `SipFlow`'s two reads ran on the workstation's loopback, the
|
||||||
|
daemon's class built around a bench `main` (2026-11-20 to 2026-11-22,
|
||||||
|
the fp6 journal's `2026-11-20-issues-2-5` capture, `flow-read`: over TCP and
|
||||||
|
over UDP `RecvReady` returns a message the socket holds without
|
||||||
|
waiting and none when it holds none, and `RecvMsg` waits its time and
|
||||||
|
returns a message when one comes; over TCP `RecvReady` leaves a
|
||||||
|
message that is not whole yet; over UDP it passes a keepalive's CRLFs
|
||||||
|
over); the run loop around them judged a wait on the dev phone (the
|
||||||
|
report's, in the repeat's bench above), and has not run with a final
|
||||||
|
waiting unread at that pass.
|
||||||
- A request cut short of its Content-Length on the client flow ran on the
|
- A request cut short of its Content-Length on the client flow ran on the
|
||||||
workstation's loopback (2026-11-22, the fp6 journal's
|
workstation's loopback (2026-11-22, the fp6 journal's
|
||||||
`2026-11-20-issues-2-5` capture, `cut-short`: `SipFlow` built around a
|
`2026-11-20-issues-2-5` capture, `cut-short`: `SipFlow` built around a
|
||||||
|
|
@ -1940,8 +1951,9 @@ do that answers without state.
|
||||||
reason — after a repeat the repeat's end and the first attempt's —, at
|
reason — after a repeat the repeat's end and the first attempt's —, at
|
||||||
once: a report that still comes for the first attempt is then answered
|
once: a report that still comes for the first attempt is then answered
|
||||||
and logged (Receiving, below) and changes nothing; a wait is judged at the run loop's passes,
|
and logged (Receiving, below) and changes nothing; a wait is judged at the run loop's passes,
|
||||||
after the server ports are read, so an answer read before that counts
|
after the server ports are read and what the client flow already holds
|
||||||
however late. Not done of the clause's repeat: it cannot be switched off
|
is (`SipFlow::RecvReady`, without waiting), so a final that
|
||||||
|
the flow held when it was read counts, however late. Not done of the clause's repeat: it cannot be switched off
|
||||||
("should be capable of being disabled through MMI"), and a failed
|
("should be capable of being disabled through MMI"), and a failed
|
||||||
message is not kept for the user to send again under its TP-MR — a
|
message is not kept for the user to send again under its TP-MR — a
|
||||||
`Send` of the same text is a new message with the next TP-MR, which the
|
`Send` of the same text is a new message with the next TP-MR, which the
|
||||||
|
|
|
||||||
|
|
@ -525,6 +525,35 @@ public:
|
||||||
pollfd p{fd_, POLLIN, 0};
|
pollfd p{fd_, POLLIN, 0};
|
||||||
int r = poll(&p, 1, ms);
|
int r = poll(&p, 1, ms);
|
||||||
if (r <= 0) { if (r == 0) return std::nullopt; if (errno == EINTR) continue; return std::nullopt; }
|
if (r <= 0) { if (r == 0) return std::nullopt; if (errno == EINTR) continue; return std::nullopt; }
|
||||||
|
std::optional<std::string> m;
|
||||||
|
if (!ReadOnce(m)) return std::nullopt;
|
||||||
|
if (m) return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A message the flow already holds — whole in the frame buffer, or
|
||||||
|
// readable on the socket now — without waiting; none when it holds
|
||||||
|
// none whole.
|
||||||
|
std::optional<std::string> RecvReady() {
|
||||||
|
for (;;) {
|
||||||
|
if (auto m = fb_.TryExtract()) return m;
|
||||||
|
pollfd p{fd_, POLLIN, 0};
|
||||||
|
if (poll(&p, 1, 0) <= 0) return std::nullopt;
|
||||||
|
std::optional<std::string> m;
|
||||||
|
if (!ReadOnce(m)) return std::nullopt;
|
||||||
|
if (m) return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int Fd() const { return fd_; }
|
||||||
|
bool Alive() const { return fd_ >= 0 && alive_; }
|
||||||
|
Transport CurrentTransport() const { return transport_; }
|
||||||
|
// What was read and not yet dispatched goes with the flow: nothing
|
||||||
|
// read from it can be answered once it is closed.
|
||||||
|
void Close() { if (fd_ >= 0) { close(fd_); fd_ = -1; } alive_ = false; fb_ = {}; }
|
||||||
|
private:
|
||||||
|
// One read of the socket once poll(2) has named it: `m` is the message a
|
||||||
|
// datagram held (UDP); on TCP the bytes go to the frame buffer. False
|
||||||
|
// when the flow is dead and the caller has nothing more to read.
|
||||||
|
bool ReadOnce(std::optional<std::string>& m) {
|
||||||
char buf[65535];
|
char buf[65535];
|
||||||
ssize_t n = recv(fd_, buf, sizeof buf, 0);
|
ssize_t n = recv(fd_, buf, sizeof buf, 0);
|
||||||
if (transport_ == Transport::Udp) {
|
if (transport_ == Transport::Udp) {
|
||||||
|
|
@ -537,38 +566,31 @@ public:
|
||||||
// PKT_TOOBIG), so UDP has no dead-flow signal beyond these and
|
// PKT_TOOBIG), so UDP has no dead-flow signal beyond these and
|
||||||
// the refresh timeouts.
|
// the refresh timeouts.
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) continue;
|
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return true;
|
||||||
Log(std::format("UDP flow: recv error: {}", std::strerror(errno)));
|
Log(std::format("UDP flow: recv error: {}", std::strerror(errno)));
|
||||||
alive_ = false; return std::nullopt;
|
alive_ = false; return false;
|
||||||
}
|
}
|
||||||
if (n == 0) continue;
|
if (n == 0) return true;
|
||||||
const std::string_view d(buf, static_cast<std::size_t>(n));
|
const std::string_view d(buf, static_cast<std::size_t>(n));
|
||||||
std::string why;
|
std::string why;
|
||||||
if (auto m = imsd::sip::DatagramMessage(d, &why)) return m;
|
m = imsd::sip::DatagramMessage(d, &why);
|
||||||
if (!why.empty()) {
|
if (!m && !why.empty()) {
|
||||||
// A request cut short of its Content-Length is answered 400
|
// A request cut short of its Content-Length is answered 400
|
||||||
// (RFC 3261 18.3; CutShortRequest says which); whatever else
|
// (RFC 3261 18.3; CutShortRequest says which); whatever else
|
||||||
// DatagramMessage refuses is ignored.
|
// DatagramMessage refuses is ignored.
|
||||||
const auto head = imsd::sip::CutShortRequest(d);
|
const auto head = imsd::sip::CutShortRequest(d);
|
||||||
Log(std::format("UDP: {} — {}", why, head ? "answered 400" : "ignored"));
|
Log(std::format("UDP: {} — {}", why, head ? "answered 400" : "ignored"));
|
||||||
if (head && !Send(imsd::msg::BuildBadRequest(*head, BadRequestSecret()))) return std::nullopt;
|
if (head && !Send(imsd::msg::BuildBadRequest(*head, BadRequestSecret()))) return false;
|
||||||
}
|
}
|
||||||
continue;
|
return true;
|
||||||
}
|
}
|
||||||
// EOF/RST is not a timeout: mark the flow dead so the engine's
|
// EOF/RST is not a timeout: mark the flow dead so the engine's
|
||||||
// reconnect path notices within one loop turn instead of at the
|
// reconnect path notices within one loop turn instead of at the
|
||||||
// next (possibly 30-min-away) keepalive refresh.
|
// next (possibly 30-min-away) keepalive refresh.
|
||||||
if (n <= 0) { alive_ = false; return std::nullopt; }
|
if (n <= 0) { alive_ = false; return false; }
|
||||||
fb_.Append(std::string_view(buf, static_cast<std::size_t>(n)));
|
fb_.Append(std::string_view(buf, static_cast<std::size_t>(n)));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
int Fd() const { return fd_; }
|
|
||||||
bool Alive() const { return fd_ >= 0 && alive_; }
|
|
||||||
Transport CurrentTransport() const { return transport_; }
|
|
||||||
// What was read and not yet dispatched goes with the flow: nothing
|
|
||||||
// read from it can be answered once it is closed.
|
|
||||||
void Close() { if (fd_ >= 0) { close(fd_); fd_ = -1; } alive_ = false; fb_ = {}; }
|
|
||||||
private:
|
|
||||||
int fd_ = -1;
|
int fd_ = -1;
|
||||||
bool alive_ = false;
|
bool alive_ = false;
|
||||||
bool lastConnectLocal_ = false;
|
bool lastConnectLocal_ = false;
|
||||||
|
|
@ -1502,13 +1524,23 @@ public:
|
||||||
reply_ = in.reply;
|
reply_ = in.reply;
|
||||||
DispatchRequest(in.msg);
|
DispatchRequest(in.msg);
|
||||||
}
|
}
|
||||||
// the SMS deadline (the final's wait, the report's) is judged right
|
// the SMS deadline (the final's wait, the report's) is judged after
|
||||||
// after the server ports were drained: a submit report that landed
|
// the server ports were drained above — a submit report that landed
|
||||||
// during a blocking re-REGISTER below must not count as a timeout
|
// during a blocking re-REGISTER below must not count as a timeout —
|
||||||
if (smsOut_ && smsOut_->Deadline() > 0 && Mono() > smsOut_->Deadline()) {
|
// and after what the client flow already holds is read here, without
|
||||||
|
// waiting: a final that came in time and was not yet read must not
|
||||||
|
// count as one either
|
||||||
|
if (SmsWaitOver()) {
|
||||||
|
while (SmsWaitOver()) {
|
||||||
|
auto pending = sip_.RecvReady();
|
||||||
|
if (!pending) break;
|
||||||
|
OnFlowMessage(*pending);
|
||||||
|
}
|
||||||
|
if (SmsWaitOver()) {
|
||||||
ExecuteSms(smsOut_->OnDeadline(Mono()));
|
ExecuteSms(smsOut_->OnDeadline(Mono()));
|
||||||
AfterSms();
|
AfterSms();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// a concatenated set that got no segment for §7.9's 30 min ages out
|
// a concatenated set that got no segment for §7.9's 30 min ages out
|
||||||
// on the loop's clock, whether or not another MESSAGE ever comes
|
// on the loop's clock, whether or not another MESSAGE ever comes
|
||||||
for (const auto& d : reassembler_.Purge(Mono(), SmsReassemblyMaxAge))
|
for (const auto& d : reassembler_.Purge(Mono(), SmsReassemblyMaxAge))
|
||||||
|
|
@ -1522,18 +1554,7 @@ public:
|
||||||
Execute(call_->OnDeadline());
|
Execute(call_->OnDeadline());
|
||||||
MaybeDropCall();
|
MaybeDropCall();
|
||||||
}
|
}
|
||||||
auto msg = sip_.RecvMsg(0.3);
|
if (auto msg = sip_.RecvMsg(0.3)) OnFlowMessage(*msg);
|
||||||
if (!msg) continue;
|
|
||||||
auto st = imsd::sip::Status(*msg);
|
|
||||||
if (!st) { reply_ = [this](const std::string& r) { sip_.Send(r); }; DispatchRequest(*msg); continue; }
|
|
||||||
std::string cseq(imsd::sip::Header(*msg, "CSeq").value_or(""));
|
|
||||||
std::string callid(imsd::sip::Header(*msg, "Call-ID").value_or(""));
|
|
||||||
if (call_ && callid == call_->CallId() && cseq.ends_with("INVITE")) {
|
|
||||||
Execute(call_->OnInviteResponse(*msg));
|
|
||||||
MaybeDropCall();
|
|
||||||
} else if (!OnSmsResponse(*msg, *st)) {
|
|
||||||
OnSubscribeResponse(*msg, *st);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// shutdown: release an active call, keep the SA/registration for resume
|
// shutdown: release an active call, keep the SA/registration for resume
|
||||||
if (call_) { Execute(call_->OnHangup()); MaybeDropCall(); }
|
if (call_) { Execute(call_->OnHangup()); MaybeDropCall(); }
|
||||||
|
|
@ -1550,6 +1571,27 @@ private:
|
||||||
};
|
};
|
||||||
struct Stopping {}; // the engine was told to stop while a bring-up waited
|
struct Stopping {}; // the engine was told to stop while a bring-up waited
|
||||||
|
|
||||||
|
// Whether the wait of the message in flight — its MESSAGE's final, its
|
||||||
|
// submit report — has run out by the clock.
|
||||||
|
bool SmsWaitOver() const { return smsOut_ && smsOut_->Deadline() > 0 && Mono() > smsOut_->Deadline(); }
|
||||||
|
|
||||||
|
// A message read from the client flow outside a REGISTER wait: a request
|
||||||
|
// is dispatched and answered on the flow; a response goes to the call
|
||||||
|
// whose INVITE it answers, else to the short message or the delivery
|
||||||
|
// report whose MESSAGE it answers, else to the reg-event subscription.
|
||||||
|
void OnFlowMessage(const std::string& msg) {
|
||||||
|
auto st = imsd::sip::Status(msg);
|
||||||
|
if (!st) { reply_ = [this](const std::string& r) { sip_.Send(r); }; DispatchRequest(msg); return; }
|
||||||
|
std::string cseq(imsd::sip::Header(msg, "CSeq").value_or(""));
|
||||||
|
std::string callid(imsd::sip::Header(msg, "Call-ID").value_or(""));
|
||||||
|
if (call_ && callid == call_->CallId() && cseq.ends_with("INVITE")) {
|
||||||
|
Execute(call_->OnInviteResponse(msg));
|
||||||
|
MaybeDropCall();
|
||||||
|
} else if (!OnSmsResponse(msg, *st)) {
|
||||||
|
OnSubscribeResponse(msg, *st);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A bring-up the network deferred (a fresh-SA throttle) or refused: wait
|
// A bring-up the network deferred (a fresh-SA throttle) or refused: wait
|
||||||
// it out here, in process, on the recovery policy's schedule, then try
|
// it out here, in process, on the recovery policy's schedule, then try
|
||||||
// the bring-up again. There is nothing to reconnect over meanwhile (the
|
// the bring-up again. There is nothing to reconnect over meanwhile (the
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue