Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4bda78d
Convert host and SRV lookups to async instead of blocking
softins Nov 9, 2026
c3e897a
Fix typo
softins Nov 9, 2026
4c42f34
Remove unused #define
softins Nov 9, 2026
18fbfa1
Add sequence number for CClient Connect and Disconnect
softins Nov 10, 2026
156cfed
Add sequence number for CConnectDlg directory resolution
softins Nov 12, 2026
8687b01
Add sequence number for CServerListManager directory resolution
softins Nov 13, 2026
14266e0
Corrections to hostname format validation
softins Nov 13, 2026
1039c7f
Only use IPv4 for directory in connectdlg
softins Nov 13, 2026
13ce6f3
Improve DirectoryAddress handling
softins Nov 13, 2026
31ac523
Return status from resolveHostname()
softins Nov 13, 2026
1ec14e1
Add CS_RESOLVING client state and emit signal for it
softins Nov 14, 2026
39be89c
In connect dialog, handle CS_RESOLVING state
softins Nov 14, 2026
f79deed
Change client state to CD_DISCONNECTED after failure to resolve
softins Nov 14, 2026
1a7d0d5
Set state to disconnected on every kind of failure
softins Nov 14, 2026
bc1aa0d
Suppress warning for zero SVR records
softins Nov 14, 2026
d119ccc
Ensure slot is always called within ResolveAddress()
softins Nov 14, 2026
863c0d7
Stop TimerReRequestServList before resolving a new directory
softins Nov 15, 2026
488b207
A few robustness improvements and simplification
softins Nov 16, 2026
a4ea676
Add connection state check to OnCLDisconnection()
softins Nov 16, 2026
f44aecf
Tidy up a comment
softins Nov 17, 2026
ab23b92
Refactor server registration timers for correct operation
softins Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 81 additions & 39 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ CClient::CClient ( const quint16 iPortNumber,
bJitterBufferOK ( true ),
bMuteMeInPersonalMix ( bNMuteMeInPersonalMix ),
iServerSockBufNumFrames ( DEF_NET_BUF_SIZE_NUM_BL ),
bRawAudioIsSupported ( false )
bRawAudioIsSupported ( false ),
iResolverSeq ( 0 )
{
int iOpusError;

Expand Down Expand Up @@ -334,6 +335,15 @@ void CClient::OnNewConnection()
//### TODO: END ###//
}

void CClient::OnCLDisconnection ( CHostAddress InetAddr )
{
if ( eConnectionState != CS_RESOLVING && InetAddr == Channel.GetAddress() )
{
// Stop client in case it received a Disconnection request
Stop();
}
}

void CClient::OnMuteStateHasChangedReceived ( int iServerChanID, bool bIsMuted )
{
// map iChanID from server channel ID to client channel ID
Expand Down Expand Up @@ -619,25 +629,6 @@ void CClient::SetRemoteChanPan ( const int iId, const float fPan )
StartTimerGainOrPan();
}

bool CClient::SetServerAddr ( QString strNAddr )
{
CHostAddress HostAddress;
if ( NetworkUtil::ParseNetworkAddress ( strNAddr, HostAddress, bIPv6Available ) )
{
// apply address to the channel
Channel.SetAddress ( HostAddress );

// By default, set server name to HostAddress. If using the Connect() method, this may be overwritten
SetConnectedServerName ( HostAddress.toString() );

return true;
}
else
{
return false; // invalid address
}
}

bool CClient::GetAndResetbJitterBufferOKFlag()
{
// get the socket buffer put status flag and reset it
Expand Down Expand Up @@ -1150,10 +1141,18 @@ void CClient::Stop()
/// @emit Disconnected
void CClient::Disconnect()
{
++iResolverSeq; // invalidate any uncompleted resolver requests

// Key off the connection state, not IsRunning() (which tracks the audio
// device): the two diverge while connecting and in headless mode, and on
// SIGTERM we must still send the disconnect message to the server.
if ( GetConnectionState() != CS_DISCONNECTED )
if ( eConnectionState == CS_RESOLVING )
{
// no need to Stop, as we haven't Started, but need to update connection state
SetConnectionState ( CS_DISCONNECTED );
emit Disconnected();
}
else if ( eConnectionState != CS_DISCONNECTED )
{
Stop();
}
Expand All @@ -1162,36 +1161,79 @@ void CClient::Disconnect()
/// @method
/// @brief Connects to strServerAddress. If a connection is currently requested
/// or established, that connection is terminated first.
/// @emit Connecting (strServerName) if SetServerAddr was valid. emit happens through Start().
/// @emit Connecting (strServerName) if address was valid. emit happens through Start().
/// Use to set CClientDlg to show being connected
/// @emit ConnectingFailed (error) if an error occurred
/// Use to display error message in CClientDlg
/// @param strServerAddress - the server address to connect to
/// @param strServerName - the human readable server name passed to Connecting()
void CClient::Connect ( const QString& strServerAddress, const QString& strServerName )
{
try
// disconnect from any current server first so that connecting to a
// different server while connected behaves as a reconnect
Disconnect();

// note that we are resolving the address
SetConnectionState ( CS_RESOLVING );
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
emit Resolving ( strServerAddress );

const int seq = iResolverSeq; // for capturing the current request

if ( NetworkUtil::ResolveAddress (
strServerAddress,
bIPv6Available,
this,
[this, seq] ( const QHostInfo& info, quint16 port ) {
// check that the request has not been invalidated
if ( seq == iResolverSeq )
{
OnHostResolved ( info, port ); // handle result
}
},
true, // allow hostname resolution
true ) ) // allow SVR resolution
{
SetConnectedServerName ( strServerName ); // set if resolution initiated ok
}
else
{
// disconnect from any current server first so that connecting to a
// different server while connected behaves as a reconnect
Disconnect();
SetConnectionState ( CS_DISCONNECTED ); // no longer CS_RESOLVING
emit ConnectingFailed ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) );
}
}

// Set server address and connect if valid address was supplied
if ( SetServerAddr ( strServerAddress ) )
{
SetConnectedServerName ( strServerName );
Start();
}
else
{
throw CGenErr ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) );
}
void CClient::OnHostResolved ( const QHostInfo& info, quint16 port )
{
if ( info.error() != QHostInfo::NoError )
{
SetConnectionState ( CS_DISCONNECTED ); // no longer CS_RESOLVING
emit ConnectingFailed ( "Cannot connect: " + info.errorString() );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to have a tr() to allow translation?

return;
}
catch ( const CGenErr& generr )

for ( const QHostAddress& HostAddr : info.addresses() )
{
Stop();
emit ConnectingFailed ( generr.GetErrorText() );
// qInfo() << "Resolved network address to" << HostAddr << "for proto" << HostAddr.protocol();
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol || ( HostAddr.protocol() == QAbstractSocket::IPv6Protocol && bIPv6Available ) )
Comment thread
ann0see marked this conversation as resolved.
Resolved
{
CHostAddress HostAddress ( HostAddr, port );
Channel.SetAddress ( HostAddress );
// sound device start can throw an exception, which we must catch here
try
{
Start();
}
catch ( const CGenErr& generr )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of this PR, but in general error handling is not great in Jamulus at the moment. So we might want to think about how to handle this in future. This is probably fine-ish (but a CGenErr is probably too general from a theoretical PoV...)

{
Stop(); // sets the connection state to CS_DISCONNECTED
emit ConnectingFailed ( generr.GetErrorText() );
}
return;
}
}

SetConnectionState ( CS_DISCONNECTED ); // no longer CS_RESOLVING
emit ConnectingFailed ( "Hostname returned no usable addresses" );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tr()? Moreover, I don't think this will tell the user enough. In general error messages should tell the user how they can fix the issue. I believe that we document in the style guide how error messages should look like. cc: @gilgongo ?

}

/// @method
Expand Down
20 changes: 9 additions & 11 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ enum EConnectionState
// stream towards the configured server and "established" once the server
// has assigned a channel ID to the client
CS_DISCONNECTED = 0, // no connection requested or established
CS_CONNECTING = 1, // connection requested but not yet established
CS_CONNECTED = 2 // connection established and current
CS_RESOLVING = 1, // resolving address
CS_CONNECTING = 2, // connection requested but not yet established
CS_CONNECTED = 3 // connection established and current
};

// audio in fader range
Expand Down Expand Up @@ -180,7 +181,6 @@ class CClient : public QObject

bool IsRunning() { return Sound.IsRunning(); }
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
bool SetServerAddr ( QString strNAddr );

// IPv6 Available
bool IsIPv6Available() { return bIPv6Available; }
Expand Down Expand Up @@ -478,6 +478,9 @@ class CClient : public QObject
int maxGainOrPanId;
int iCurPingTime;

// sequence number for asynchronous DNS resolver
int iResolverSeq;

protected slots:
void OnHandledSignal ( int sigNum );
void OnSendProtMessage ( CVector<uint8_t> vecMessage );
Expand All @@ -489,14 +492,7 @@ protected slots:
void OnJittBufSizeChanged ( int iNewJitBufSize );
void OnReqChanInfo() { Channel.SetRemoteInfo ( ChannelInfo ); }
void OnNewConnection();
void OnCLDisconnection ( CHostAddress InetAddr )
{
if ( InetAddr == Channel.GetAddress() )
{
// Stop client in case it received a Disconnection request
Stop();
}
}
void OnCLDisconnection ( CHostAddress InetAddr );
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );

void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage );
Expand All @@ -514,6 +510,7 @@ protected slots:
void OnMuteStateHasChangedReceived ( int iServerChanID, bool bIsMuted );
void OnCLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
void OnConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
void OnHostResolved ( const QHostInfo& info, quint16 port );

signals:
void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
Expand All @@ -538,6 +535,7 @@ protected slots:
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );

void ConnectionStateChanged ( EConnectionState eConnectionState );
void Resolving ( const QString& strServerName );
void Connecting ( const QString& strServerName );
void ConnectingFailed ( const QString& errorMessage );
void Disconnected();
Expand Down
53 changes: 43 additions & 10 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
// other
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );

QObject::connect ( pClient, &CClient::Resolving, this, &CClientDlg::OnResolving );

QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnecting );

QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
Expand Down Expand Up @@ -605,21 +607,38 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
// that the connection less message respond may not make it back but that
// is not critical since the next time Jamulus is started we have another
// chance and the update check is not time-critical at all)
CHostAddress UpdateServerHostAddress;

// Send the request to two servers for redundancy if either or both of them
// has a higher release version number, the reply will trigger the notification.

// Don't use SRV resolution when resolving update servers.
if ( !NetworkUtil::ResolveAddress ( UPDATECHECK1_ADDRESS, pClient->IsIPv6Available(), this, &CClientDlg::OnHostResolved, true, false ) )
{
qWarning() << "Cannot start update check for" << UPDATECHECK1_ADDRESS;
}

if ( NetworkUtil::ParseNetworkAddressBare ( UPDATECHECK1_ADDRESS, UpdateServerHostAddress, pClient->IsIPv6Available() ) )
if ( !NetworkUtil::ResolveAddress ( UPDATECHECK2_ADDRESS, pClient->IsIPv6Available(), this, &CClientDlg::OnHostResolved, true, false ) )
{
pClient->CreateCLServerListReqVerAndOSMes ( UpdateServerHostAddress );
qWarning() << "Cannot start update check for" << UPDATECHECK2_ADDRESS;
}
}

if ( NetworkUtil::ParseNetworkAddressBare ( UPDATECHECK2_ADDRESS, UpdateServerHostAddress, pClient->IsIPv6Available() ) )
void CClientDlg::OnHostResolved ( const QHostInfo& info, quint16 port )
{
if ( info.error() != QHostInfo::NoError )
{
pClient->CreateCLServerListReqVerAndOSMes ( UpdateServerHostAddress );
qWarning() << "Update lookup failed:" << info.errorString();
return;
}

for ( const QHostAddress& HostAddr : info.addresses() )
{
// qInfo() << "Resolved network address to" << HostAddr << "for proto" << HostAddr.protocol();
if ( HostAddr.protocol() == QAbstractSocket::IPv4Protocol ||
( HostAddr.protocol() == QAbstractSocket::IPv6Protocol && pClient->IsIPv6Available() ) )
{
CHostAddress HostAddress ( HostAddr, port );

pClient->CreateCLServerListReqVerAndOSMes ( HostAddress );
return;
}
}
}

Expand Down Expand Up @@ -771,7 +790,7 @@ void CClientDlg::OnConnectDlgAccepted()
void CClientDlg::OnConnectDisconBut()
{
// the connect/disconnect button implements a toggle functionality
if ( pClient->IsRunning() )
if ( pClient->GetConnectionState() != CS_DISCONNECTED )
{
pClient->Disconnect();
}
Expand Down Expand Up @@ -1220,6 +1239,14 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
}

void CClientDlg::OnResolving ( const QString& strMixerBoardLabel )
{
Q_UNUSED ( strMixerBoardLabel ); // might want to use it in the future

// change connect button text to "disconnect" while resolving too
butConnect->setText ( tr ( "&Disconnect" ) );
}

void CClientDlg::OnConnecting ( const QString& strMixerBoardLabel )
{

Expand Down Expand Up @@ -1248,7 +1275,13 @@ void CClientDlg::OnConnecting ( const QString& strMixerBoardLabel )
}
}

void CClientDlg::OnConnectingFailed ( const QString& strError ) { QMessageBox::critical ( this, APP_NAME, strError, tr ( "Close" ), nullptr ); }
void CClientDlg::OnConnectingFailed ( const QString& strError )
{
// change connect button text back to "connect"
butConnect->setText ( tr ( "C&onnect" ) );

QMessageBox::critical ( this, APP_NAME, strError, tr ( "Close" ), nullptr );
}

void CClientDlg::OnDisconnect()
{
Expand Down
2 changes: 2 additions & 0 deletions src/clientdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
CAnalyzerConsole AnalyzerConsole;

public slots:
void OnResolving ( const QString& strServerName );
void OnConnecting ( const QString& strServerName );
void OnConnectingFailed ( const QString& strErrorText );
void OnDisconnect();
Expand Down Expand Up @@ -260,6 +261,7 @@ public slots:
void SetMixerBoardDeco ( const ERecorderState newRecorderState, const EGUIDesign eNewDesign );
void OnAudioChannelsChanged() { UpdateRevSelection(); }
void OnNumClientsChanged ( int iNewNumClients );
void OnHostResolved ( const QHostInfo& info, quint16 port );

void accept() { close(); } // introduced by pljones

Expand Down
36 changes: 30 additions & 6 deletions src/clientrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,42 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe
return;
}

CHostAddress haDirectoryAddress;

// Allow IPv4 only for communicating with Directories
if ( !NetworkUtil::ParseNetworkAddress ( jsonDirectoryIp.toString(), haDirectoryAddress, false ) )
// Start asynchronous address resolution with lambda callback slot
bool accepted = NetworkUtil::ResolveAddress (
jsonDirectoryIp.toString(),
false, // Currently IPv4 only for directories
pClient,
[pClient] ( const QHostInfo& info, quint16 port ) {
// this callback runs later, after the RPC handler has returned
if ( info.error() != QHostInfo::NoError )
{
qWarning() << "Directory lookup failed:" << info.errorString();
return;
}

// select a usable IPv4 address
for ( const QHostAddress& addr : info.addresses() )
{
if ( addr.protocol() == QAbstractSocket::IPv4Protocol )
{
CHostAddress haDirectoryAddress ( addr, port );

pClient->CreateCLReqServerListMes ( haDirectoryAddress );
return;
}
}
qWarning() << "No usable address found for directory";
},
true, // allow hostname lookup
true ); // allow SRV lookup

if ( !accepted )
{
response["error"] =
CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: directory is not a valid socket address" );
return;
}

// send the request for the server list
pClient->CreateCLReqServerListMes ( haDirectoryAddress );
response["result"] = "ok";
} );

Expand Down
Loading
Loading