Priority: P3: Somewhat important
Affects Version/s: 4.7.2
Fix Version/s: None
Component/s: Network: Sockets
Currently QAbstractSocket has two signals:
void stateChanged( QAbstractSocket::SocketState state )
void error( QAbstractSocket::SocketError error )
One of the main challenges is that it is difficult to see these changes in connection to each other, like for example if I want to know both that the state changed from ConnectingState to UnconnectedState and that the error was a ConnectionRefusedError. These signals seem to be sent out in an arbitrary order, in this case the error signal is emitted after the state change. If on the other hand I go from a ConnectedState to ClosingState with a RemoteHostClosedError, then the error signal is emitted before the state change. And some state changes obviously do not emit errors at all because they're not errors. Even the same change from ConnectedState to ClosingState may emit an
error if the remote host closed it but not if we closed it ourselves.
The second challenge I see is that there's really no way to clear the error, for example if I use a socket and try connecting and get a ConnectionRefused error then try connecting to a different host it'll still say you have an error. So really the only way to know if you have a new error is to listen for the error signal. Which previously mentioned may be issued after the state change rather than before.
The third challenge, which is the most trivial to work around is that it's very annoying not to know what the old state was in the stateChanged signal - if you're first using this signal it's probably because you want to handle several different state changes and it matters from what state you went to UnconnectedState. Rather than store it myself, it would be convenient if the socket emitted it.
So much for the issue, this is what I would propose:
1. Add a "NoError" value to the SocketError enum
2. Add a signal QAbstractSocket::changedState( QAbstractSocket::SocketState newState, QAbstractSocket::SocketState oldState, QAbstractSocket::SocketError error ). If the state change is natural - that is not due to an error then the last argument is QAbstractSocket::NoError. If the state change is due to an error, then the error code says what error. If you continue to use the socket then any further state changes report QAbstractSocket::NoError again (unless another error occurs, of course).
This would make it much, much easier to keep track of the actual condition the socket is in and to respond properly to errors. As it is now I can query both the state() and error() functions but I can't be sure they've actually been updated with the correct value yet, since the signal order changes.