Skip to content

Commit f7f0515

Browse files
committed
Refactor Connect() and Disconnect() into two methods
1 parent a5af8cc commit f7f0515

File tree

2 files changed

+77
-50
lines changed

2 files changed

+77
-50
lines changed

src/client.cpp

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,26 @@ void CClient::StartDelayTimer()
466466
}
467467
}
468468

469+
bool CClient::SetServerAddr ( QString strNAddr )
470+
{
471+
CHostAddress HostAddress;
472+
#ifdef CLIENT_NO_SRV_CONNECT
473+
if ( NetworkUtil().ParseNetworkAddress ( strNAddr, HostAddress, bEnableIPv6 ) )
474+
#else
475+
if ( NetworkUtil().ParseNetworkAddressWithSrvDiscovery ( strNAddr, HostAddress, bEnableIPv6 ) )
476+
#endif
477+
{
478+
// apply address to the channel
479+
Channel.SetAddress ( HostAddress );
480+
481+
return true;
482+
}
483+
else
484+
{
485+
return false; // invalid address
486+
}
487+
}
488+
469489
bool CClient::GetAndResetbJitterBufferOKFlag()
470490
{
471491
// get the socket buffer put status flag and reset it
@@ -845,26 +865,65 @@ void CClient::OnClientIDReceived ( int iChanID )
845865
emit ClientIDReceived ( iChanID );
846866
}
847867

848-
bool CClient::Connect ( QString strServerAddress, QString strServerName )
868+
void CClient::Start()
849869
{
850-
if ( !Channel.IsEnabled() )
870+
// init object
871+
Init();
872+
873+
// enable channel
874+
Channel.SetEnable ( true );
875+
876+
// start audio interface
877+
Sound.Start();
878+
}
879+
880+
void CClient::Stop()
881+
{
882+
// start disconnection
883+
// Channel.Disconnect() should automatically disable Channel as soon as disconnected.
884+
// Note that this only works if sound is active!
885+
Channel.Disconnect();
886+
887+
QTime DieTime = QTime::currentTime().addMSecs ( 500 );
888+
while ( ( QTime::currentTime() < DieTime ) && Channel.IsEnabled() )
851889
{
852-
CHostAddress HostAddress;
890+
// exclude user input events because if we use AllEvents, it happens
891+
// that if the user initiates a connection and disconnection quickly
892+
// (e.g. quickly pressing enter five times), the software can get into
893+
// an unknown state
894+
QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
895+
}
853896

854-
if ( NetworkUtil().ParseNetworkAddress ( strServerAddress, HostAddress, bEnableIPv6 ) )
855-
{
856-
// init object
857-
Init();
858-
// apply address to the channel
859-
Channel.SetAddress ( HostAddress );
897+
// Now stop the audio interface
898+
Sound.Stop();
860899

861-
// enable channel
862-
Channel.SetEnable ( true );
900+
// in case we timed out, log warning and make sure Channel is disabled
901+
if ( Channel.IsEnabled() )
902+
{
903+
Channel.SetEnable ( false );
904+
}
863905

864-
// start audio interface
865-
Sound.Start();
906+
// Send disconnect message to server (Since we disable our protocol
907+
// receive mechanism with the next command, we do not evaluate any
908+
// respond from the server, therefore we just hope that the message
909+
// gets its way to the server, if not, the old behaviour time-out
910+
// disconnects the connection anyway).
911+
ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress() );
912+
913+
// reset current signal level and LEDs
914+
bJitterBufferOK = true;
915+
SignalLevelMeter.Reset();
916+
}
917+
918+
bool CClient::Connect ( QString strServerAddress, QString strServerName )
919+
{
920+
if ( !Channel.IsEnabled() )
921+
{
922+
// Set server address and connect if valid address was supplied
923+
if ( SetServerAddr ( strServerAddress ) ) {
924+
925+
Start();
866926

867-
// Notify ClientDlg
868927
emit Connecting ( strServerName );
869928

870929
return true;
@@ -878,41 +937,7 @@ bool CClient::Disconnect()
878937
{
879938
if ( Channel.IsEnabled() )
880939
{
881-
// start disconnection
882-
Channel.Disconnect();
883-
884-
// Channel.Disconnect() should automatically disable Channel as soon as disconnected.
885-
// Note that this only works if Sound is Active !
886-
887-
QTime DieTime = QTime::currentTime().addMSecs ( 500 );
888-
while ( ( QTime::currentTime() < DieTime ) && Channel.IsEnabled() )
889-
{
890-
// exclude user input events because if we use AllEvents, it happens
891-
// that if the user initiates a connection and disconnection quickly
892-
// (e.g. quickly pressing enter five times), the software can get into
893-
// an unknown state
894-
QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
895-
}
896-
897-
// Now stop the audio interface
898-
Sound.Stop();
899-
900-
// in case we timed out, log warning and make sure Channel is disabled
901-
if ( Channel.IsEnabled() )
902-
{
903-
Channel.SetEnable ( false );
904-
}
905-
906-
// Send disconnect message to server (Since we disable our protocol
907-
// receive mechanism with the next command, we do not evaluate any
908-
// respond from the server, therefore we just hope that the message
909-
// gets its way to the server, if not, the old behaviour time-out
910-
// disconnects the connection anyway).
911-
ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress() );
912-
913-
// reset current signal level and LEDs
914-
bJitterBufferOK = true;
915-
SignalLevelMeter.Reset();
940+
Stop();
916941

917942
emit Disconnected();
918943

src/client.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,19 @@ class CClient : public QObject
118118

119119
virtual ~CClient();
120120

121+
void Start();
121122
bool Connect ( QString strServerAddress, QString strServerName );
122123
bool Disconnect();
123124

124125
bool IsRunning() { return Sound.IsRunning(); }
125126
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); } // For OnTimerCheckAudioDeviceOk only
127+
bool SetServerAddr ( QString strNAddr );
126128

127129
double GetLevelForMeterdBLeft() { return SignalLevelMeter.GetLevelForMeterdBLeftOrMono(); }
128130
double GetLevelForMeterdBRight() { return SignalLevelMeter.GetLevelForMeterdBRight(); }
129131

130132
bool GetAndResetbJitterBufferOKFlag();
131-
133+
132134
bool IsConnected() { return Channel.IsConnected(); }
133135

134136
EGUIDesign GetGUIDesign() const { return eGUIDesign; }

0 commit comments

Comments
 (0)