Skip to content

Protocol Exchange Flow

finthetechguy edited this page Nov 23, 2026 · 1 revision

Exchange Flow

Once the link is up, the two sides trade a fixed series of objects over YY. One side opens each step and the other answers it. In ctrpass's terms the Lead side opens and the Wait side answers (see Link Setup for the roles). The object layouts are on Exchange Objects.

Implemented in: link, cec::object, yy::session.

The objects each side prepares

Before the link comes up, each side builds its list of objects from what it plans to trade:

# Type Name Payload
1 0x41 Session open Random values, own MAC, current time
2 0x43 Capabilities 16 fixed bytes
3 0x11 Services Every chosen service record and the token
4 0x21 Console ID 8 bytes
5 0x23 Unknown u32 1
6 0x31 Offer One entry per title it sends: mode, flags, total bytes, message count
7 0x33 Verdict Its own offer entries, all admitted
8 0x81 Message list Title and size of each envelope that follows
9+ envelope Envelopes One per title, holding that title's containers

The answering side does not send these in order. It picks the matching object from its list when a request arrives, as below.

Request and answer

Opener sends Answerer replies with
0x41 session open Its own session open, retyped to 0x42
0x43 capabilities Its own capabilities, unchanged type 0x43
0x11 services Its own services, retyped to 0x12
0x21 console ID Its own console ID, retyped to 0x22
0x23 A new 0x24 object with u32 2
0x31 offer Its own offer, retyped to 0x32. It also decides, title by title, which of the opener's titles it takes.
0x33 verdict Its verdict on the opener's offer, type 0x34. It also drops its own envelopes for titles the opener's verdict refused.
0x81 message list Its own message list and all its envelopes, sent one per turn. Only the first 0x81 is answered.

"Retyped" means the object is sent with only its type field changed.

Full sequence

This is the Lead role, with ctrpass opening. In the Wait role the arrows are the same with the two participants swapped: the console opens and ctrpass answers.

sequenceDiagram
    participant L as Lead (opens)
    participant W as Wait (answers)
    Note over L,W: Link is up. Each arrow is one YY connection.
    L->>W: 0x41 session open
    W->>L: 0x42 session open
    L->>W: 0x43 capabilities
    W->>L: 0x43 capabilities
    L->>W: 0x11 services
    W->>L: 0x12 services
    L->>W: 0x21 console ID
    W->>L: 0x22 console ID
    L->>W: 0x23 value 1
    W->>L: 0x24 value 2
    L->>W: 0x31 offer
    Note over W: Decides which of L's titles fit
    W->>L: 0x32 offer (W's own titles)
    Note over L: Decides which of W's titles fit
    L->>W: 0x33 verdict on W's offer
    Note over W: Drops own envelopes L refused
    W->>L: 0x34 verdict on L's offer
    Note over L: Drops own envelopes W refused, rebuilds its message list
    Note over L: Lead waits 60 ms
    L->>W: 0x81 message list
    W->>L: 0x81 message list
    L->>W: Envelope for title A
    W->>L: Envelope for title X
    L->>W: Envelope for title B
    W->>L: Envelope for title Y
    Note over L,W: Session lingers 2 s, then ends
Loading

The envelopes alternate because each side opens its next object after receiving one. When one side runs out, the other continues after a 400 ms idle timeout.

Offers and verdicts

The offer and verdict objects carry the same title list. Each entry has a code. Code 1 admits a title and code 2 refuses it.

flowchart TD
    offer["Peer's offer: title, bytes, count"] --> fit{"Title set to receive and the whole title fits the inbox?"}
    fit -- yes --> admit["Code 1, admit"]
    fit -- no --> refuse["Code 2, refuse"]
    admit --> reply["Verdict object"]
    refuse --> reply
    reply --> peer["Peer keeps only admitted envelopes"]
Loading

ctrpass admits a title only if:

  • the title is in the store and set to receive, and
  • the total bytes and the number of messages of that title in the offer both fit the free space of its inbox.

A title is admitted whole or not at all.

How ctrpass builds its verdict as Lead

  • If the console's offer names exactly the same set of titles as ctrpass's offer, the verdict is ctrpass's own offer entries, put in the console's order.
  • Otherwise the verdict is the console's offer list with the type changed to 0x33, every code first set to admit.
  • The codes from ctrpass's admission decision are then written into the entries.

How ctrpass applies a verdict

When the peer's verdict (0x33 or 0x34) arrives, ctrpass:

  1. collects the titles whose code is 1;
  2. keeps only the envelopes after its message list whose title is admitted, in the order of the verdict;
  3. rebuilds the message list to match the envelopes kept;
  4. records the titles it held but the peer refused.

In the Lead role, ctrpass holds back its verdict, message list and envelopes until the console's offer and verdict arrive. Its verdict needs the console's offer, and its envelopes depend on the console's verdict.

When an exchange counts

A side keeps nothing it received unless the other side got as far as sending its message list. ctrpass treats the console as having sent it when the console sent FIN, a message list object, or any envelope.

flowchart TD
    end_["Session ends"] --> sent{"Console sent its message list?"}
    sent -- no --> ab["Abandoned: the console stored nothing"]
    sent -- yes --> fin["Finished"]
    fin --> deliv["Messages counted as sent: containers in envelopes the console acknowledged in full"]
    fin --> recv["Messages received: containers in the console's envelopes"]
Loading

ctrpass also drops a received container whose send mode is 0 unless ctrpass's own send-mode-0 message of the same title was delivered in this exchange. Mii Plaza messages use mode 0. The Plaza sends and receives in the same exchange.

State machine of one session

stateDiagram-v2
    [*] --> Idle
    Idle --> Sending: open next object (SYN)
    Sending --> Sending: SYN|ACK, data, ACKs, resends
    Sending --> Idle: whole object acknowledged
    Sending --> Failed: 16 tries without progress
    Idle --> Receiving: peer SYN
    Sending --> Receiving: peer SYN (if all data was sent, the object counts as delivered)
    Receiving --> Receiving: data, gaps, END
    Receiving --> Idle: whole object received
    Idle --> Idle: 400 ms idle, open next object
    Idle --> Ended: nothing left, and FIN or linger passed
    Ended --> [*]
    Failed --> [*]
Loading

After receiving an object the session goes back to Idle and immediately opens its next object, if it has one. As the answering side, ctrpass adds the answer to its outbox before that happens, so the answer is what goes out next.

Limits

Limit Value
Titles in one offer, verdict or message list 24
Containers in one envelope 64
Envelope size 1 MiB
Arrivals for a title Refused if they exceed the bytes or count that title's offer declared

Clone this wiki locally