Skip to content

Commit a77dcfe

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

File tree

5 files changed

+134
-115
lines changed

5 files changed

+134
-115
lines changed

src/client.cpp

Lines changed: 59 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,53 @@ 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 Stops the client if the client is running
914+
/// @emit Disconnected
915+
void CClient::Disconnect()
916+
{
917+
if ( IsRunning() )
918+
{
919+
Stop();
920+
}
921+
}
922+
923+
/// @method
924+
/// @brief Connects to strServerAddress
925+
/// @emit Connected (strServerName) if the client wasn't running and SetServerAddr was valid. emit happens through Start().
926+
/// Use to set CClientDlg to show being connected
927+
/// @emit ConnectingFailed (error) if an error occurred
928+
/// Use to display error message in CClientDlg
929+
/// @param strServerAddress - the server address to connect to
930+
/// @param strServerName - the String argument to be passed to Connecting()
931+
void CClient::Connect ( QString strServerAddress, QString strServerName )
932+
{
933+
try
934+
{
935+
if ( !IsRunning() )
936+
{
937+
// Set server address and connect if valid address was supplied
938+
if ( SetServerAddr ( strServerAddress ) )
939+
{
940+
SetConnectedServerName ( strServerName );
941+
Start();
942+
}
943+
else
944+
{
945+
throw CGenErr ( tr ( "Received invalid server address. Please check for typos in the provided server address." ) );
946+
}
947+
}
948+
}
949+
catch ( const CGenErr& generr )
950+
{
951+
Stop();
952+
emit ConnectingFailed ( generr.GetErrorText() );
953+
}
909954
}
910955

911956
void CClient::Init()

src/client.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ class CClient : public QObject
111111
public:
112112
CClient ( const quint16 iPortNumber,
113113
const quint16 iQosNumber,
114-
const QString& strConnOnStartupAddress,
115114
const QString& strMIDISetup,
116115
const bool bNoAutoJackConnect,
117116
const QString& strNClientName,
@@ -122,6 +121,13 @@ class CClient : public QObject
122121

123122
void Start();
124123
void Stop();
124+
void Disconnect();
125+
void Connect ( QString strServerAddress, QString strServerName );
126+
127+
// The ConnectedServerName is emitted by Connected() to update the UI with a human readable server name
128+
void SetConnectedServerName ( const QString strServerName ) { strConnectedServerName = strServerName; };
129+
QString GetConnectedServerName() const { return strConnectedServerName; };
130+
125131
bool IsRunning() { return Sound.IsRunning(); }
126132
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
127133
bool SetServerAddr ( QString strNAddr );
@@ -288,6 +294,10 @@ class CClient : public QObject
288294
int EvaluatePingMessage ( const int iMs );
289295
void CreateServerJitterBufferMessage();
290296

297+
// information for the connected server
298+
299+
QString strConnectedServerName;
300+
291301
// only one channel is needed for client application
292302
CChannel Channel;
293303
CProtocol ConnLessProtocol;
@@ -388,7 +398,8 @@ protected slots:
388398
{
389399
if ( InetAddr == Channel.GetAddress() )
390400
{
391-
emit Disconnected();
401+
// Stop client in case it received a Disconnection request
402+
Stop();
392403
}
393404
}
394405
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
@@ -428,7 +439,11 @@ protected slots:
428439

429440
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
430441

442+
void Connected ( QString strServerName );
443+
void ConnectingFailed ( QString errorMessage );
444+
void DisconnectClient();
431445
void Disconnected();
446+
432447
void SoundDeviceChanged ( QString strError );
433448
void ControllerInFaderLevel ( int iChannelIdx, int iValue );
434449
void ControllerInPanValue ( int iChannelIdx, int iValue );

src/clientdlg.cpp

Lines changed: 40 additions & 78 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

@@ -483,7 +474,11 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
483474
// other
484475
QObject::connect ( pClient, &CClient::ConClientListMesReceived, this, &CClientDlg::OnConClientListMesReceived );
485476

486-
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnected );
477+
QObject::connect ( pClient, &CClient::Connected, this, &CClientDlg::OnConnect );
478+
479+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
480+
481+
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
487482

488483
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
489484

@@ -618,11 +613,8 @@ void CClientDlg::closeEvent ( QCloseEvent* Event )
618613
ConnectDlg.close();
619614
AnalyzerConsole.close();
620615

621-
// if connected, terminate connection
622-
if ( pClient->IsRunning() )
623-
{
624-
pClient->Stop();
625-
}
616+
// Disconnect if needed
617+
pClient->Disconnect();
626618

627619
// make sure all current fader settings are applied to the settings struct
628620
MainMixerBoard->StoreAllFaderSettings();
@@ -740,15 +732,11 @@ void CClientDlg::OnConnectDlgAccepted()
740732
}
741733
}
742734

743-
// first check if we are already connected, if this is the case we have to
744-
// disconnect the old server first
745-
if ( pClient->IsRunning() )
746-
{
747-
Disconnect();
748-
}
735+
// Disconnect the client. We could be currently connected.
736+
pClient->Disconnect();
749737

750738
// initiate connection
751-
Connect ( strSelectedAddress, strMixerBoardLabel );
739+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
752740

753741
// reset flag
754742
bConnectDlgWasShown = false;
@@ -760,11 +748,12 @@ void CClientDlg::OnConnectDisconBut()
760748
// the connect/disconnect button implements a toggle functionality
761749
if ( pClient->IsRunning() )
762750
{
763-
Disconnect();
764-
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
751+
pClient->Stop();
765752
}
766753
else
767754
{
755+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
756+
// TODO: Refactor to have robust error handling
768757
ShowConnectionSetupDialog();
769758
}
770759
}
@@ -869,7 +858,7 @@ void CClientDlg::OnLicenceRequired ( ELicenceType eLicenceType )
869858
// disconnect from that server.
870859
if ( !LicenceDlg.exec() )
871860
{
872-
Disconnect();
861+
pClient->Disconnect();
873862
}
874863

875864
// unmute the client output stream if local mute button is not pressed
@@ -1172,10 +1161,7 @@ void CClientDlg::OnSoundDeviceChanged ( QString strError )
11721161
if ( !strError.isEmpty() )
11731162
{
11741163
// the sound device setup has a problem, disconnect any active connection
1175-
if ( pClient->IsRunning() )
1176-
{
1177-
Disconnect();
1178-
}
1164+
pClient->Disconnect();
11791165

11801166
// show the error message of the device setup
11811167
QMessageBox::critical ( this, APP_NAME, strError, tr ( "Ok" ), nullptr );
@@ -1203,65 +1189,38 @@ void CClientDlg::OnCLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int
12031189
ConnectDlg.SetPingTimeAndNumClientsResult ( InetAddr, iPingTime, iNumClients );
12041190
}
12051191

1206-
void CClientDlg::Connect ( const QString& strSelectedAddress, const QString& strMixerBoardLabel )
1192+
void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
12071193
{
1208-
// set address and check if address is valid
1209-
if ( pClient->SetServerAddr ( strSelectedAddress ) )
1210-
{
1211-
// try to start client, if error occurred, do not go in
1212-
// running state but show error message
1213-
try
1214-
{
1215-
if ( !pClient->IsRunning() )
1216-
{
1217-
pClient->Start();
1218-
}
1219-
}
1220-
1221-
catch ( const CGenErr& generr )
1222-
{
1223-
// show error message and return the function
1224-
QMessageBox::critical ( this, APP_NAME, generr.GetErrorText(), "Close", nullptr );
1225-
return;
1226-
}
12271194

1228-
// hide label connect to server
1229-
lblConnectToServer->hide();
1230-
lbrInputLevelL->setEnabled ( true );
1231-
lbrInputLevelR->setEnabled ( true );
1195+
// hide label connect to server
1196+
lblConnectToServer->hide();
1197+
lbrInputLevelL->setEnabled ( true );
1198+
lbrInputLevelR->setEnabled ( true );
12321199

1233-
// change connect button text to "disconnect"
1234-
butConnect->setText ( tr ( "&Disconnect" ) );
1200+
// change connect button text to "disconnect"
1201+
butConnect->setText ( tr ( "&Disconnect" ) );
12351202

1236-
// set server name in audio mixer group box title
1237-
MainMixerBoard->SetServerName ( strMixerBoardLabel );
1203+
// set server name in audio mixer group box title
1204+
MainMixerBoard->SetServerName ( strMixerBoardLabel );
12381205

1239-
// start timer for level meter bar and ping time measurement
1240-
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1241-
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1242-
TimerPing.start ( PING_UPDATE_TIME_MS );
1243-
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
1206+
// start timer for level meter bar and ping time measurement
1207+
TimerSigMet.start ( LEVELMETER_UPDATE_TIME_MS );
1208+
TimerBuffersLED.start ( BUFFER_LED_UPDATE_TIME_MS );
1209+
TimerPing.start ( PING_UPDATE_TIME_MS );
1210+
TimerCheckAudioDeviceOk.start ( CHECK_AUDIO_DEV_OK_TIME_MS ); // is single shot timer
12441211

1245-
// audio feedback detection
1246-
if ( pSettings->bEnableFeedbackDetection )
1247-
{
1248-
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1249-
bDetectFeedback = true;
1250-
}
1212+
// audio feedback detection
1213+
if ( pSettings->bEnableFeedbackDetection )
1214+
{
1215+
TimerDetectFeedback.start ( DETECT_FEEDBACK_TIME_MS ); // single shot timer
1216+
bDetectFeedback = true;
12511217
}
12521218
}
12531219

1254-
void CClientDlg::Disconnect()
1255-
{
1256-
// only stop client if currently running, in case we received
1257-
// the stopped message, the client is already stopped but the
1258-
// connect/disconnect button and other GUI controls must be
1259-
// updated
1260-
if ( pClient->IsRunning() )
1261-
{
1262-
pClient->Stop();
1263-
}
1220+
void CClientDlg::OnConnectingFailed ( const QString& strError ) { QMessageBox::critical ( this, APP_NAME, strError, tr ( "Close" ), nullptr ); }
12641221

1222+
void CClientDlg::OnDisconnect()
1223+
{
12651224
// change connect button text to "connect"
12661225
butConnect->setText ( tr ( "C&onnect" ) );
12671226

@@ -1303,6 +1262,9 @@ void CClientDlg::Disconnect()
13031262

13041263
// clear mixer board (remove all faders)
13051264
MainMixerBoard->HideAll();
1265+
1266+
// Reset the deco
1267+
SetMixerBoardDeco ( RS_UNDEFINED, pClient->GetGUIDesign() );
13061268
}
13071269

13081270
void CClientDlg::UpdateDisplay()

0 commit comments

Comments
 (0)