Skip to content

Commit 8e48c03

Browse files
pgScorpioann0see
andcommitted
Refactor Connect out to CClient
This is an extract from #2550 Co-authored-by: ann0see <[email protected]>
1 parent 7258a55 commit 8e48c03

File tree

5 files changed

+118
-98
lines changed

5 files changed

+118
-98
lines changed

src/client.cpp

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
/* Implementation *************************************************************/
2828
CClient::CClient ( const quint16 iPortNumber,
2929
const quint16 iQosNumber,
30-
const QString& strConnOnStartupAddress,
3130
const QString& strMIDISetup,
3231
const bool bNoAutoJackConnect,
3332
const QString& strNClientName,
@@ -122,7 +121,7 @@ CClient::CClient ( const quint16 iPortNumber,
122121
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
123122
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived );
124123

125-
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected );
124+
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Stop );
126125

127126
QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection );
128127

@@ -184,13 +183,6 @@ CClient::CClient ( const quint16 iPortNumber,
184183
// start the socket (it is important to start the socket after all
185184
// initializations and connections)
186185
Socket.Start();
187-
188-
// do an immediate start if a server address is given
189-
if ( !strConnOnStartupAddress.isEmpty() )
190-
{
191-
SetServerAddr ( strConnOnStartupAddress );
192-
Start();
193-
}
194186
}
195187

196188
CClient::~CClient()
@@ -486,6 +478,9 @@ bool CClient::SetServerAddr ( QString strNAddr )
486478
// apply address to the channel
487479
Channel.SetAddress ( HostAddress );
488480

481+
// By default, set server name to HostAddress. If using the Connect() method, this may be overwritten
482+
SetConnectedServerName ( HostAddress.toString() );
483+
489484
return true;
490485
}
491486
else
@@ -773,11 +768,8 @@ void CClient::OnHandledSignal ( int sigNum )
773768
{
774769
case SIGINT:
775770
case SIGTERM:
776-
// if connected, terminate connection (needed for headless mode)
777-
if ( IsRunning() )
778-
{
779-
Stop();
780-
}
771+
// if connected, Stop client (needed for headless mode)
772+
Stop();
781773

782774
// this should trigger OnAboutToQuit
783775
QCoreApplication::instance()->exit();
@@ -872,8 +864,14 @@ void CClient::Start()
872864

873865
// start audio interface
874866
Sound.Start();
867+
868+
emit Connected ( GetConnectedServerName() );
875869
}
876870

871+
/// @method
872+
/// @brief Stops client and disconnects from server
873+
/// @emit Disconnected
874+
/// Use to set CClientDlg to show not being connected
877875
void CClient::Stop()
878876
{
879877
// stop audio interface
@@ -906,6 +904,42 @@ void CClient::Stop()
906904
// reset current signal level and LEDs
907905
bJitterBufferOK = true;
908906
SignalLevelMeter.Reset();
907+
908+
// emit Disconnected() to inform UI of disconnection
909+
emit Disconnected();
910+
}
911+
912+
/// @method
913+
/// @brief Connects to strServerAddress
914+
/// @emit Connected (strServerName) if the client wasn't running and SetServerAddr was valid. emit happens through Start().
915+
/// Use to set CClientDlg to show being connected
916+
/// @emit ConnectingFailed (error) if an error occurred
917+
/// Use to display error message in CClientDlg
918+
/// @param strServerAddress - the server address to connect to
919+
/// @param strServerName - the String argument to be passed to Connecting()
920+
void CClient::Connect ( QString strServerAddress, QString strServerName )
921+
{
922+
try
923+
{
924+
if ( !IsRunning() )
925+
{
926+
// Set server address and connect if valid address was supplied
927+
if ( SetServerAddr ( strServerAddress ) )
928+
{
929+
SetConnectedServerName ( strServerName );
930+
Start();
931+
}
932+
else
933+
{
934+
throw CGenErr ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) );
935+
}
936+
}
937+
}
938+
catch ( const CGenErr& generr )
939+
{
940+
Stop();
941+
emit ConnectingFailed ( generr.GetErrorText() );
942+
}
909943
}
910944

911945
void CClient::Init()

src/client.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class CClient : public QObject
110110
public:
111111
CClient ( const quint16 iPortNumber,
112112
const quint16 iQosNumber,
113-
const QString& strConnOnStartupAddress,
114113
const QString& strMIDISetup,
115114
const bool bNoAutoJackConnect,
116115
const QString& strNClientName,
@@ -121,6 +120,12 @@ class CClient : public QObject
121120

122121
void Start();
123122
void Stop();
123+
void Connect ( QString strServerAddress, QString strServerName );
124+
125+
// The ConnectedServerName is emitted by Connected() to update the UI with a human readable server name
126+
void SetConnectedServerName ( const QString strServerName ) { strConnectedServerName = strServerName; };
127+
QString GetConnectedServerName() const { return strConnectedServerName; };
128+
124129
bool IsRunning() { return Sound.IsRunning(); }
125130
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
126131
bool SetServerAddr ( QString strNAddr );
@@ -287,6 +292,10 @@ class CClient : public QObject
287292
int EvaluatePingMessage ( const int iMs );
288293
void CreateServerJitterBufferMessage();
289294

295+
// information for the connected server
296+
297+
QString strConnectedServerName;
298+
290299
// only one channel is needed for client application
291300
CChannel Channel;
292301
CProtocol ConnLessProtocol;
@@ -387,7 +396,8 @@ protected slots:
387396
{
388397
if ( InetAddr == Channel.GetAddress() )
389398
{
390-
emit Disconnected();
399+
// Stop client in case it received a Disconnection request
400+
Stop();
391401
}
392402
}
393403
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -427,7 +437,11 @@ protected slots:
427437

428438
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
429439

440+
void Connected ( QString strServerName );
441+
void ConnectingFailed ( QString errorMessage );
442+
void DisconnectClient();
430443
void Disconnected();
444+
431445
void SoundDeviceChanged ( QString strError );
432446
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
433447
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
/* Implementation *************************************************************/
2828
CClientDlg::CClientDlg ( CClient* pNCliP,
2929
CClientSettings* pNSetP,
30-
const QString& strConnOnStartupAddress,
3130
const QString& strMIDISetup,
3231
const bool bNewShowComplRegConnList,
3332
const bool bShowAnalyzerConsole,
@@ -272,14 +271,6 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
272271
TimerCheckAudioDeviceOk.setSingleShot ( true ); // only check once after connection
273272
TimerDetectFeedback.setSingleShot ( true );
274273

275-
// Connect on startup ------------------------------------------------------
276-
if ( !strConnOnStartupAddress.isEmpty() )
277-
{
278-
// initiate connection (always show the address in the mixer board
279-
// (no alias))
280-
Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
281-
}
282-
283274
// File menu --------------------------------------------------------------
284275
QMenu* pFileMenu = new QMenu ( tr ( "&File" ), this );
285276

@@ -473,7 +464,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
473464
// other
474465
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
475466

476-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
467+
QObject::connect ( pClient, &CClient::Connected, this, &CClientDlg::OnConnect );
468+
469+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
470+
471+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
477472

478473
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
479474

@@ -608,11 +603,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
608603
ConnectDlg.close();
609604
AnalyzerConsole.close();
610605

611-
// if connected, terminate connection
612-
if ( pClient->IsRunning() )
613-
{
614-
pClient->Stop();
615-
}
606+
// Disconnect if needed
607+
pClient->Stop();
616608

617609
// make sure all current fader settings are applied to the settings struct
618610
MainMixerBoard->StoreAllFaderSettings();
@@ -730,15 +722,9 @@ void CClientDlg::OnConnectDlgAccepted()
730722
}
731723
}
732724

733-
// first check if we are already connected, if this is the case we have to
734-
// disconnect the old server first
735-
if ( pClient->IsRunning() )
736-
{
737-
Disconnect();
738-
}
739-
740725
// initiate connection
741-
Connect ( strSelectedAddress, strMixerBoardLabel );
726+
727+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
742728

743729
// reset flag
744730
bConnectDlgWasShown = false;
@@ -750,11 +736,12 @@ void CClientDlg::OnConnectDisconBut()
750736
// the connect/disconnect button implements a toggle functionality
751737
if ( pClient->IsRunning() )
752738
{
753-
Disconnect();
754-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
739+
pClient->Stop();
755740
}
756741
else
757742
{
743+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
744+
// TODO: Refactor to have robust error handling
758745
ShowConnectionSetupDialog();
759746
}
760747
}
@@ -859,7 +846,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
859846
// disconnect from that server.
860847
if ( !LicenceDlg.exec() )
861848
{
862-
Disconnect();
849+
pClient->Stop();
863850
}
864851

865852
// unmute the client output stream if local mute button is not pressed
@@ -1164,7 +1151,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
11641151
// the sound device setup has a problem, disconnect any active connection
11651152
if ( pClient->IsRunning() )
11661153
{
1167-
Disconnect();
1154+
pClient->Stop();
11681155
}
11691156

11701157
// show the error message of the device setup
@@ -1193,65 +1180,38 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
11931180
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
11941181
}
11951182

1196-
void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel )
1183+
void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
11971184
{
1198-
// set address and check if address is valid
1199-
if ( pClient->SetServerAddr ( strSelectedAddress ) )
1200-
{
1201-
// try to start client, if error occurred, do not go in
1202-
// running state but show error message
1203-
try
1204-
{
1205-
if ( !pClient->IsRunning() )
1206-
{
1207-
pClient->Start();
1208-
}
1209-
}
12101185

1211-
catch ( const CGenErr& generr )
1212-
{
1213-
// show error message and return the function
1214-
QMessageBox::critical ( this, APP_NAME, generr.GetErrorText(), "Close", nullptr );
1215-
return;
1216-
}
1186+
// hide label connect to server
1187+
lblConnectToServer->hide();
1188+
lbrInputLevelL->setEnabled ( true );
1189+
lbrInputLevelR->setEnabled ( true );
12171190

1218-
// hide label connect to server
1219-
lblConnectToServer->hide();
1220-
lbrInputLevelL->setEnabled ( true );
1221-
lbrInputLevelR->setEnabled ( true );
1191+
// change connect button text to "disconnect"
1192+
butConnect->setText ( tr ( "&Disconnect" ) );
12221193

1223-
// change connect button text to "disconnect"
1224-
butConnect->setText ( tr ( "&Disconnect" ) );
1194+
// set server name in audio mixer group box title
1195+
MainMixerBoard->SetServerName ( strMixerBoardLabel );
12251196

1226-
// set server name in audio mixer group box title
1227-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1197+
// start timer for level meter bar and ping time measurement
1198+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1199+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1200+
TimerPing.start ( PING_UPDATE_TIME_MS );
1201+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12281202

1229-
// start timer for level meter bar and ping time measurement
1230-
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1231-
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1232-
TimerPing.start ( PING_UPDATE_TIME_MS );
1233-
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
1234-
1235-
// audio feedback detection
1236-
if ( pSettings->bEnableFeedbackDetection )
1237-
{
1238-
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1239-
bDetectFeedback = true;
1240-
}
1203+
// audio feedback detection
1204+
if ( pSettings->bEnableFeedbackDetection )
1205+
{
1206+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1207+
bDetectFeedback = true;
12411208
}
12421209
}
12431210

1244-
void CClientDlg::Disconnect()
1245-
{
1246-
// only stop client if currently running, in case we received
1247-
// the stopped message, the client is already stopped but the
1248-
// connect/disconnect button and other GUI controls must be
1249-
// updated
1250-
if ( pClient->IsRunning() )
1251-
{
1252-
pClient->Stop();
1253-
}
1211+
void CClientDlg::OnConnectingFailed ( const QString& strError ) { QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr ); }
12541212

1213+
void CClientDlg::OnDisconnect()
1214+
{
12551215
// change connect button text to "connect"
12561216
butConnect->setText ( tr ( "C&onnect" ) );
12571217

@@ -1293,6 +1253,9 @@ void CClientDlg::Disconnect()
12931253

12941254
// clear mixer board (remove all faders)
12951255
MainMixerBoard->HideAll();
1256+
1257+
// Reset the deco
1258+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
12961259
}
12971260

12981261
void CClientDlg::UpdateDisplay()

0 commit comments

Comments
 (0)