Repository navigation
Sockets: fixes to ClientSocket_Qt - #1784
Merged
Merged
Conversation
Ports the same state-machine fixes as the WinSocket/POSIX PRs to the Qt implementation: - Report connect failures: errorOccurred/disconnected during a connect attempt now fire on_connect_finished(error_message) and return the socket to NOT_RUNNING, instead of leaving the state stuck at CONNECTING/DESTRUCTING (a refused connection used to leave the UI on 'Connecting...' forever). - Detect connection loss: an established connection that ends on its own (server close, reset) now fires on_disconnect(error_message) and returns to NOT_RUNNING instead of staying unusable. - Confirm connect success with a short grace period: on some platforms Qt emits connected() optimistically, and a refused connection emits connected() followed immediately by errorOccurred(). The success callback is now only fired 50 ms after connected(), if no error arrived in the meantime. - Reconnect after close(): close() used to destroy the QTcpSocket (and all of its slots), after which connect()/send() were silent no-ops. connect() now always creates a fresh QTcpSocket on the event thread pool, and destroys the previous one. close() is idempotent. - send() now actually reports how many bytes were written and blocks until the write completes (previously it returned the requested size without waiting for anything). A failed or closed connection returns the number of bytes that made it to the kernel. - readyRead now drains all available data (a single delivery can hold more than one 4096-byte buffer).
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.
Part of the socket state-machine fixes; the
on_disconnect()hook landed in #1780. This is the implementation currently in use on Windows.What was wrong
errorOccurred/disconnectedonly setDESTRUCTING;on_connect_finished(error)was never called, so a refused connection (e.g. a wrong IP) left the status line stuck on "Connecting..." forever and the socket unusable.close()was a silent no-op.close()destroyed the QTcpSocket (and with it all of its slots), soconnect()/send()afterwards did nothing at all.send()lied about the result. It returned the requested size without waiting for the write or checking anything.readyReadonly read one 4096-byte chunk per event, so a burst bigger than one buffer could sit in the socket until the next event.What this does
errorOccurred/disconnectedwhile connecting) now fireon_connect_finished(error_message)and return the socket toNOT_RUNNING.on_disconnect(error_message)and returns toNOT_RUNNING, soconnect()can be called again.connected()optimistically, and a refused connection emitsconnected()followed immediately byerrorOccurred()(verified with Qt 6.10 on Linux), so the success callback only fires if no error arrives in the meantime.connect()always creates a fresh QTcpSocket on the Qt event-thread pool and destroys the previous one;close()is blocking and idempotent.send()blocks until the write completes and returns the number of bytes actually handed to the kernel (0 if the connection is gone).readyReaddrains all available data in abytesAvailable()loop.Testing
Full functional test against a real TCP server (loopback, Qt 6.10.2): connect, send, receive (including a 20 KB burst through the drain loop), graceful close, RST, refused connect (reports "Connection refused" through
on_connect_finished),close()+ reconnect, and 50 rapid connect/disconnect cycles - all pass, stable across repeated runs.