Repository navigation
Support adb's delayed_ack in the TcpUsb bridge - #552
Merged
Merged
Conversation
Without delayed_ack a stream has one A_WRTE in flight until the peer's A_OKAY, so data arrives once per round trip regardless of bandwidth. Over a 186 ms link that is 5.5 deliveries per second, which caps screen mirroring (e.g. Android Studio's Device Mirroring) at a few fps. With delayed_ack, A_OPEN's arg1 and a 4-byte A_OKAY payload carry byte credits, as in adb's sockets.cpp. The bridge lists the feature in its banner when the client's banner does (adb hosts do with ADB_BURST_MODE=1). It then sends while it has credit and credits the client once bytes are written. A stream opened with arg1 = 0 keeps one packet in flight. Without the feature, nothing changes. Measured through an STF provider 186 ms away, 60 Hz writes over adb reverse: 5.5 bursts/s, 183 ms apart, without burst mode; 295 of 300 writes delivered as sent, 44 ms apart (the device's own pace), with it. exec-out 34 -> 90 Mbit/s. Signed-off-by: Arthur Yazhevich <[email protected]>
koral--
reviewed
Oct 5, 2026
Comment on lines
+166
to
+171
| this.transport.write(data, () => { | ||
| if (!this.ended) { | ||
| debug('O:A_OKAY'); | ||
| this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length))); | ||
| } | ||
| }); |
Member
There was a problem hiding this comment.
The write callback ignores its error argument, so a failed write is still acked as success and the client gets credit for bytes that never reached the device. Suggest closing the stream on error. (The !this.ended guard is fine: the stream is already closed by then.)
Suggested change
| this.transport.write(data, () => { | |
| if (!this.ended) { | |
| debug('O:A_OKAY'); | |
| this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length))); | |
| } | |
| }); | |
| this.transport.write(data, (err?: Error | null) => { | |
| if (err) { | |
| debug('transport write failed', err); | |
| this.end(); | |
| return; | |
| } | |
| if (!this.ended) { | |
| debug('O:A_OKAY'); | |
| this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, this.window.ack(data.length))); | |
| } | |
| }); |
Author
There was a problem hiding this comment.
Thanks, applied in 98b14ca: the stream ends on a write error instead of crediting the client.
Comment on lines
+89
to
+91
| this.conn.write(data, () => { | ||
| if (!this.ended) ack(); | ||
| }); |
Member
There was a problem hiding this comment.
Same as in service.ts: the write error is ignored, so a failed write is acked as success. Suggest ending the stream on error.
Suggested change
| this.conn.write(data, () => { | |
| if (!this.ended) ack(); | |
| }); | |
| this.conn.write(data, (err?: Error | null) => { | |
| if (err) { | |
| debug('conn write failed', err); | |
| this.end(); | |
| return; | |
| } | |
| if (!this.ended) ack(); | |
| }); |
Author
There was a problem hiding this comment.
Thanks, applied in 98b14ca: the stream ends on a write error instead of crediting the client.
With delayed_ack the bridge credits the client from the write callback. A failed write was still credited, as if the bytes had reached the device or the reverse connection. End the stream instead. Signed-off-by: Arthur Yazhevich <[email protected]>
koral--
approved these changes
Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Without adb's
delayed_ackfeature, a stream through the TcpUsb bridge has oneA_WRTEin flight until the peer'sA_OKAY. Data reaches the client once per round trip regardless of bandwidth, so over a WAN, screen mirroring stutters: frames arrive in bunches, one bunch per round trip. Android Studio's Device Mirroring and scrcpy are both affected.Change
The bridge now implements
delayed_ackthe way adb'ssockets.cppandadb.cppdo:features=delayed_ack;in itsA_CNXNbanner when the client's banner lists it. adb hosts list it when the server runs withADB_BURST_MODE=1.A_OPEN'sarg1and a 4-byteA_OKAYpayload carry byte credits. The bridge sends while it has credit, and credits the client only once its bytes have been written, so a slow device or reader still pushes back.arg1 = 0keeps one packet in flight. Without the feature, behaviour is unchanged.SendWindowclass holds the per-stream state, used by bothServiceandReverseStream.features=is;-terminated. Otherwise adb reads the trailing NUL as part of the value and silently doesn't enable the feature.Measurements
Through an STF 3.8.0 provider 182 ms away, scrcpy 4.1's video stream over
adb reverse(720×1600, the device produces a frame every 18 ms):ADB_BURST_MODE=1exec-outthroughput: 34 → 90 Mbit/s. Android Studio's Device Mirroring went from a few fps to usable.Testing
npm test: 209 passing.