Skip to content

Commit 8bc81d7

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

File tree

4 files changed

+103
-77
lines changed

4 files changed

+103
-77
lines changed

src/client.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ CClient::CClient ( const quint16 iPortNumber,
122122
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
123123
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived );
124124

125-
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected );
125+
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnect );
126126

127127
QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection );
128128

@@ -773,11 +773,8 @@ void CClient::OnHandledSignal ( int sigNum )
773773
{
774774
case SIGINT:
775775
case SIGTERM:
776-
// if connected, terminate connection (needed for headless mode)
777-
if ( IsRunning() )
778-
{
779-
Stop();
780-
}
776+
// if connected, disconnect (needed for headless mode)
777+
Disconnect();
781778

782779
// this should trigger OnAboutToQuit
783780
QCoreApplication::instance()->exit();
@@ -908,6 +905,47 @@ void CClient::Stop()
908905
SignalLevelMeter.Reset();
909906
}
910907

908+
/// @method
909+
/// @brief Connects to strServerAddress
910+
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid.
911+
/// Use to set CClientDlg to show being connected
912+
/// @emit ConnectingFailed (error) if an error occurred
913+
/// Use to display error message in CClientDlg
914+
/// @param strServerAddress - the server address to connect to
915+
/// @param strServerName - the String argument to be passed to Connecting()
916+
void CClient::Connect ( QString strServerAddress, QString strServerName )
917+
{
918+
try {
919+
if ( !IsRunning() )
920+
{
921+
// Set server address and connect if valid address was supplied
922+
if ( SetServerAddr ( strServerAddress ) )
923+
{
924+
Start();
925+
emit Connecting ( strServerName );
926+
}
927+
else {
928+
throw CGenErr ( "Received invalid server address. Please check for typos in the provided server address." );
929+
}
930+
}
931+
}
932+
catch ( const CGenErr& generr )
933+
{
934+
Disconnect();
935+
emit ConnectingFailed ( generr.GetErrorText() );
936+
}
937+
}
938+
939+
/// @method
940+
/// @brief Disconnects client by calling Stop()
941+
/// @emit Disconnected
942+
/// Use to set CClientDlg to show not being connected
943+
void CClient::Disconnect()
944+
{
945+
Stop();
946+
emit Disconnected();
947+
}
948+
911949
void CClient::Init()
912950
{
913951
// check if possible frame size factors are supported

src/client.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ class CClient : public QObject
121121

122122
void Start();
123123
void Stop();
124+
void Connect ( QString strServerAddress, QString strServerName );
125+
void Disconnect();
126+
124127
bool IsRunning() { return Sound.IsRunning(); }
125128
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
126129
bool SetServerAddr ( QString strNAddr );
@@ -387,7 +390,7 @@ protected slots:
387390
{
388391
if ( InetAddr == Channel.GetAddress() )
389392
{
390-
emit Disconnected();
393+
Disconnect();
391394
}
392395
}
393396
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -427,7 +430,12 @@ protected slots:
427430

428431
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
429432

433+
void ConnectClient ( QString strServerAddress );
434+
void Connecting ( QString strServerName );
435+
void ConnectingFailed ( QString errorMessage );
436+
void DisconnectClient();
430437
void Disconnected();
438+
431439
void SoundDeviceChanged ( QString strError );
432440
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
433441
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,12 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
277277
{
278278
// initiate connection (always show the address in the mixer board
279279
// (no alias))
280-
Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
280+
281+
// initiate connection
282+
283+
pClient->Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
284+
// TODO: Find out why without this the mixer status issue still occurs.
285+
OnConnect ( strConnOnStartupAddress );
281286
}
282287

283288
// File menu --------------------------------------------------------------
@@ -473,7 +478,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
473478
// other
474479
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
475480

476-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
481+
QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect );
482+
483+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
484+
485+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
477486

478487
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
479488

@@ -608,11 +617,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
608617
ConnectDlg.close();
609618
AnalyzerConsole.close();
610619

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

617623
// make sure all current fader settings are applied to the settings struct
618624
MainMixerBoard->StoreAllFaderSettings();
@@ -730,15 +736,9 @@ void CClientDlg::OnConnectDlgAccepted()
730736
}
731737
}
732738

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-
740739
// initiate connection
741-
Connect ( strSelectedAddress, strMixerBoardLabel );
740+
741+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
742742

743743
// reset flag
744744
bConnectDlgWasShown = false;
@@ -750,11 +750,12 @@ void CClientDlg::OnConnectDisconBut()
750750
// the connect/disconnect button implements a toggle functionality
751751
if ( pClient->IsRunning() )
752752
{
753-
Disconnect();
754-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
753+
pClient->Disconnect();
755754
}
756755
else
757756
{
757+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
758+
// TODO: Refactor to have robust error handling
758759
ShowConnectionSetupDialog();
759760
}
760761
}
@@ -859,7 +860,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
859860
// disconnect from that server.
860861
if ( !LicenceDlg.exec() )
861862
{
862-
Disconnect();
863+
pClient->Disconnect();
863864
}
864865

865866
// unmute the client output stream if local mute button is not pressed
@@ -1164,7 +1165,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
11641165
// the sound device setup has a problem, disconnect any active connection
11651166
if ( pClient->IsRunning() )
11661167
{
1167-
Disconnect();
1168+
pClient->Disconnect();
11681169
}
11691170

11701171
// show the error message of the device setup
@@ -1193,65 +1194,41 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
11931194
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
11941195
}
11951196

1196-
void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel )
1197+
void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
11971198
{
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-
}
1210-
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-
}
12171199

1218-
// hide label connect to server
1219-
lblConnectToServer->hide();
1220-
lbrInputLevelL->setEnabled ( true );
1221-
lbrInputLevelR->setEnabled ( true );
1200+
// hide label connect to server
1201+
lblConnectToServer->hide();
1202+
lbrInputLevelL->setEnabled ( true );
1203+
lbrInputLevelR->setEnabled ( true );
12221204

1223-
// change connect button text to "disconnect"
1224-
butConnect->setText ( tr ( "&Disconnect" ) );
1205+
// change connect button text to "disconnect"
1206+
butConnect->setText ( tr ( "&Disconnect" ) );
12251207

1226-
// set server name in audio mixer group box title
1227-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1208+
// set server name in audio mixer group box title
1209+
MainMixerBoard->SetServerName ( strMixerBoardLabel );
12281210

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
1211+
// start timer for level meter bar and ping time measurement
1212+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1213+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1214+
TimerPing.start ( PING_UPDATE_TIME_MS );
1215+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12341216

1235-
// audio feedback detection
1236-
if ( pSettings->bEnableFeedbackDetection )
1237-
{
1238-
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1239-
bDetectFeedback = true;
1240-
}
1217+
// audio feedback detection
1218+
if ( pSettings->bEnableFeedbackDetection )
1219+
{
1220+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1221+
bDetectFeedback = true;
12411222
}
12421223
}
12431224

1244-
void CClientDlg::Disconnect()
1225+
void CClientDlg::OnConnectingFailed ( const QString& strError )
12451226
{
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-
}
1227+
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
1228+
}
12541229

1230+
void CClientDlg::OnDisconnect()
1231+
{
12551232
// change connect button text to "connect"
12561233
butConnect->setText ( tr ( "C&onnect" ) );
12571234

@@ -1293,6 +1270,9 @@ void CClientDlg::Disconnect()
12931270

12941271
// clear mixer board (remove all faders)
12951272
MainMixerBoard->HideAll();
1273+
1274+
// Reset the deco
1275+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
12961276
}
12971277

12981278
void CClientDlg::UpdateDisplay()

src/clientdlg.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
9494
void ShowAnalyzerConsole();
9595
void UpdateAudioFaderSlider();
9696
void UpdateRevSelection();
97-
void Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel );
98-
void Disconnect();
9997
void ManageDragNDrop ( QDropEvent* Event, const bool bCheckAccept );
10098
void SetPingTime ( const int iPingTime, const int iOverallDelayMs, const CMultiColorLED::ELightColor eOverallDelayLEDColor );
10199

@@ -127,6 +125,9 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
127125
CAnalyzerConsole AnalyzerConsole;
128126

129127
public slots:
128+
void OnConnect ( const QString& strServerName );
129+
void OnConnectingFailed ( const QString& strErrorText );
130+
void OnDisconnect();
130131
void OnConnectDisconBut();
131132
void OnTimerSigMet();
132133
void OnTimerBuffersLED();
@@ -232,7 +233,6 @@ public slots:
232233
}
233234

234235
void OnConnectDlgAccepted();
235-
void OnDisconnected() { Disconnect(); }
236236
void OnGUIDesignChanged();
237237
void OnMeterStyleChanged();
238238
void OnRecorderStateReceived ( ERecorderState eRecorderState );

0 commit comments

Comments
 (0)