Skip to content

Commit 535f781

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

File tree

2 files changed

+82
-50
lines changed

2 files changed

+82
-50
lines changed

src/client.cpp

+78-49
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,69 @@ 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+
//### TODO: BEGIN ###//
904+
// Add error logging
905+
//### TODO: END ###//
863906

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

867-
// Notify ClientDlg
868931
emit Connecting ( strServerName );
869932

870933
return true;
@@ -878,41 +941,7 @@ bool CClient::Disconnect()
878941
{
879942
if ( Channel.IsEnabled() )
880943
{
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();
944+
Stop();
916945

917946
emit Disconnected();
918947

src/client.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,20 @@ class CClient : public QObject
118118

119119
virtual ~CClient();
120120

121+
void Start();
122+
void Stop();
121123
bool Connect ( QString strServerAddress, QString strServerName );
122124
bool Disconnect();
123125

124126
bool IsRunning() { return Sound.IsRunning(); }
125127
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); } // For OnTimerCheckAudioDeviceOk only
128+
bool SetServerAddr ( QString strNAddr );
126129

127130
double GetLevelForMeterdBLeft() { return SignalLevelMeter.GetLevelForMeterdBLeftOrMono(); }
128131
double GetLevelForMeterdBRight() { return SignalLevelMeter.GetLevelForMeterdBRight(); }
129132

130133
bool GetAndResetbJitterBufferOKFlag();
131-
134+
132135
bool IsConnected() { return Channel.IsConnected(); }
133136

134137
EGUIDesign GetGUIDesign() const { return eGUIDesign; }

0 commit comments

Comments
 (0)