Skip to content

Commit 0b6a2bb

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 a1c0765 commit 0b6a2bb

File tree

4 files changed

+104
-79
lines changed

4 files changed

+104
-79
lines changed

src/client.cpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,56 @@ 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 returned true.
911+
/// @param strServerAddress - the server address to connect to
912+
/// @param strServerName - the String argument to be passed to Connecting()
913+
/// @result true if client wasn't running and SetServerAddr returned true, false otherwise
914+
bool CClient::Connect ( QString strServerAddress, QString strServerName )
915+
{
916+
if ( !IsRunning() )
917+
{
918+
// Set server address and connect if valid address was supplied
919+
if ( SetServerAddr ( strServerAddress ) )
920+
{
921+
922+
Start();
923+
924+
emit Connecting ( strServerName );
925+
926+
return true;
927+
}
928+
}
929+
930+
return false;
931+
}
932+
933+
/// @method
934+
/// @brief Disconnects client
935+
/// @emit Disconnected
936+
/// @result true if client wasn't running, false otherwise
937+
bool CClient::Disconnect()
938+
{
939+
if ( IsRunning() )
940+
{
941+
Stop();
942+
943+
emit Disconnected();
944+
945+
return true;
946+
}
947+
else
948+
{
949+
// make sure sound is stopped too
950+
Sound.Stop();
951+
952+
emit Disconnected();
953+
954+
return false;
955+
}
956+
}
957+
911958
void CClient::Init()
912959
{
913960
// check if possible frame size factors are supported

src/client.h

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

122122
void Start();
123123
void Stop();
124+
bool Connect ( QString strServerAddress, QString strServerName );
125+
bool Disconnect();
126+
124127
bool IsRunning() { return Sound.IsRunning(); }
125128
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
126129
bool SetServerAddr ( QString strNAddr );
@@ -427,7 +430,9 @@ protected slots:
427430

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

433+
void Connecting ( QString strServerName );
430434
void Disconnected();
435+
431436
void SoundDeviceChanged ( QString strError );
432437
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
433438
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ 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+
// TODO: Refactor this for failing call on Connect()
283+
284+
pClient->Connect ( strConnOnStartupAddress, strConnOnStartupAddress );
285+
// TODO: Find out why without this the mixer status issue still occurs.
286+
OnConnect ( strConnOnStartupAddress );
281287
}
282288

283289
// File menu --------------------------------------------------------------
@@ -473,7 +479,9 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
473479
// other
474480
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
475481

476-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
482+
QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect );
483+
484+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
477485

478486
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
479487

@@ -608,11 +616,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
608616
ConnectDlg.close();
609617
AnalyzerConsole.close();
610618

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

617622
// make sure all current fader settings are applied to the settings struct
618623
MainMixerBoard->StoreAllFaderSettings();
@@ -730,16 +735,14 @@ void CClientDlg::OnConnectDlgAccepted()
730735
}
731736
}
732737

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() )
738+
// initiate connection
739+
// TODO: Refactor this for failing call on Connect()
740+
741+
if ( pClient->Connect ( strSelectedAddress, strMixerBoardLabel ) )
736742
{
737-
Disconnect();
743+
OnConnect ( strMixerBoardLabel );
738744
}
739745

740-
// initiate connection
741-
Connect ( strSelectedAddress, strMixerBoardLabel );
742-
743746
// reset flag
744747
bConnectDlgWasShown = false;
745748
}
@@ -748,13 +751,10 @@ void CClientDlg::OnConnectDlgAccepted()
748751
void CClientDlg::OnConnectDisconBut()
749752
{
750753
// the connect/disconnect button implements a toggle functionality
751-
if ( pClient->IsRunning() )
752-
{
753-
Disconnect();
754-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
755-
}
756-
else
754+
if ( !pClient->Disconnect() )
757755
{
756+
// If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog
757+
// TODO: Refactor to have robust error handling
758758
ShowConnectionSetupDialog();
759759
}
760760
}
@@ -859,7 +859,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
859859
// disconnect from that server.
860860
if ( !LicenceDlg.exec() )
861861
{
862-
Disconnect();
862+
pClient->Disconnect();
863863
}
864864

865865
// unmute the client output stream if local mute button is not pressed
@@ -1164,7 +1164,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
11641164
// the sound device setup has a problem, disconnect any active connection
11651165
if ( pClient->IsRunning() )
11661166
{
1167-
Disconnect();
1167+
pClient->Disconnect();
11681168
}
11691169

11701170
// show the error message of the device setup
@@ -1193,65 +1193,36 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
11931193
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
11941194
}
11951195

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

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-
}
1199+
// hide label connect to server
1200+
lblConnectToServer->hide();
1201+
lbrInputLevelL->setEnabled ( true );
1202+
lbrInputLevelR->setEnabled ( true );
12171203

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

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

1226-
// set server name in audio mixer group box title
1227-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1210+
// start timer for level meter bar and ping time measurement
1211+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1212+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1213+
TimerPing.start ( PING_UPDATE_TIME_MS );
1214+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12281215

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-
}
1216+
// audio feedback detection
1217+
if ( pSettings->bEnableFeedbackDetection )
1218+
{
1219+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1220+
bDetectFeedback = true;
12411221
}
12421222
}
12431223

1244-
void CClientDlg::Disconnect()
1224+
void CClientDlg::OnDisconnect()
12451225
{
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-
}
1254-
12551226
// change connect button text to "connect"
12561227
butConnect->setText ( tr ( "C&onnect" ) );
12571228

@@ -1293,6 +1264,9 @@ void CClientDlg::Disconnect()
12931264

12941265
// clear mixer board (remove all faders)
12951266
MainMixerBoard->HideAll();
1267+
1268+
// Reset the deco
1269+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
12961270
}
12971271

12981272
void CClientDlg::UpdateDisplay()

src/clientdlg.h

Lines changed: 2 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,8 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
127125
CAnalyzerConsole AnalyzerConsole;
128126

129127
public slots:
128+
void OnConnect ( const QString& strServerName );
129+
void OnDisconnect();
130130
void OnConnectDisconBut();
131131
void OnTimerSigMet();
132132
void OnTimerBuffersLED();
@@ -232,7 +232,6 @@ public slots:
232232
}
233233

234234
void OnConnectDlgAccepted();
235-
void OnDisconnected() { Disconnect(); }
236235
void OnGUIDesignChanged();
237236
void OnMeterStyleChanged();
238237
void OnRecorderStateReceived ( ERecorderState eRecorderState );

0 commit comments

Comments
 (0)