Preparing your design workspace

Loading the problem and your saved attempt…

Asynchronous TCP Message Channel

Design a bounded message API over one TCP session, with local circular buffers, partial-I/O framing and explicit asynchronous acceptance and terminal outcomes.

Design the endpoint objects, public interfaces and background coordination for one process sending messages to another over TCP.

The report mixes shared-memory discussion with a concrete TCP API case; this practice explicitly chooses that TCP case, with process-local circular buffers rather than a shared-memory mapping.

For this exercise, assume the connection is already established, one application caller uses each endpoint, and the sending process sends only in one direction. A fixed positive capacity C bounds each local record buffer and a fixed maximum L, with1<=L<=2^31-1, bounds message bytes. Empty messages are valid.

Use a four-byte unsigned big-endian length followed by exactly that many payload bytes; TCP reads/writes may complete partially and do not preserve message boundaries. Application enqueue returns Enqueued, Full, Closed or TooLarge; it copies accepted bytes and does not wait for socket I/O or capacity.

Validate size first: a message larger than L returns TooLarge even after closure; a size-valid enqueue after writer closure or transport failure returns Closed. Full and TooLarge change nothing.

Application poll returns a complete independent Message copy, Empty, Closed or Failed without performing or waiting for socket I/O. Brief local synchronization is allowed; the API is not promised lock-free or wait-free. FIFO means accepted-send order while the connection remains healthy.

Enqueued means local acceptance, not remote delivery or application processing. Background transport workers preserve partial offsets and reuse outgoing storage only after its full frame is written; incoming slots are reserved before a new frame and published only when complete.

The outgoing buffer counts a partially written head frame toward C; an incoming partial-frame reservation counts toward C. Full incoming storage pauses further reads instead of overwriting or dropping unread messages.

Writer closure is idempotent: reject later sends, drain accepted frames, then half-close the sending direction. Clean EOF between frames leads to Closed only after complete received messages drain.

An I/O error, invalid length or EOF inside a frame leads to sticky Failed after already completed received messages drain; incomplete bytes are never returned, and outstanding send delivery may be unknown.

For this exercise, exclude connection setup, real socket implementation, reconnect/retry, durable recovery, multiple application callers, zero-copy leases and exactly-once application processing. No executable implementation is required.

The framing, capacities, ownership, caller counts, lifecycle and errors are authored practice assumptions.