Skip to content

Commit

Permalink
Refactor Connect() and Disconnect() into two methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ann0see committed Sep 8, 2024
1 parent a5af8cc commit 1614b81
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
123 changes: 74 additions & 49 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,26 @@ void CClient::StartDelayTimer()
}
}

bool CClient::SetServerAddr ( QString strNAddr )
{
CHostAddress HostAddress;
#ifdef CLIENT_NO_SRV_CONNECT
if ( NetworkUtil().ParseNetworkAddress ( strNAddr, HostAddress, bEnableIPv6 ) )
#else
if ( NetworkUtil().ParseNetworkAddressWithSrvDiscovery ( strNAddr, HostAddress, bEnableIPv6 ) )
#endif
{
// apply address to the channel
Channel.SetAddress ( HostAddress );

return true;
}
else
{
return false; // invalid address
}
}

bool CClient::GetAndResetbJitterBufferOKFlag()
{
// get the socket buffer put status flag and reset it
Expand Down Expand Up @@ -845,26 +865,65 @@ void CClient::OnClientIDReceived ( int iChanID )
emit ClientIDReceived ( iChanID );
}

bool CClient::Connect ( QString strServerAddress, QString strServerName )
void CClient::Start()
{
if ( !Channel.IsEnabled() )
// init object
Init();

// enable channel
Channel.SetEnable ( true );

// start audio interface
Sound.Start();
}

void CClient::Stop()
{
// start disconnection
// Channel.Disconnect() should automatically disable Channel as soon as disconnected.
// Note that this only works if sound is active!
Channel.Disconnect();

QTime DieTime = QTime::currentTime().addMSecs ( 500 );
while ( ( QTime::currentTime() < DieTime ) && Channel.IsEnabled() )
{
CHostAddress HostAddress;
// exclude user input events because if we use AllEvents, it happens
// that if the user initiates a connection and disconnection quickly
// (e.g. quickly pressing enter five times), the software can get into
// an unknown state
QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
}

if ( NetworkUtil().ParseNetworkAddress ( strServerAddress, HostAddress, bEnableIPv6 ) )
{
// init object
Init();
// apply address to the channel
Channel.SetAddress ( HostAddress );
// Now stop the audio interface
Sound.Stop();

// enable channel
Channel.SetEnable ( true );
// in case we timed out, log warning and make sure Channel is disabled
if ( Channel.IsEnabled() )
{
Channel.SetEnable ( false );
}

// start audio interface
Sound.Start();
// Send disconnect message to server (Since we disable our protocol
// receive mechanism with the next command, we do not evaluate any
// respond from the server, therefore we just hope that the message
// gets its way to the server, if not, the old behaviour time-out
// disconnects the connection anyway).
ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress() );

// reset current signal level and LEDs
bJitterBufferOK = true;
SignalLevelMeter.Reset();
}

bool CClient::Connect ( QString strServerAddress, QString strServerName )
{
if ( !Channel.IsEnabled() )
{
// Set server address and connect if valid address was supplied
if ( SetServerAddr ( strServerAddress ) ) {

Start();

// Notify ClientDlg
emit Connecting ( strServerName );

return true;
Expand All @@ -878,41 +937,7 @@ bool CClient::Disconnect()
{
if ( Channel.IsEnabled() )
{
// start disconnection
Channel.Disconnect();

// Channel.Disconnect() should automatically disable Channel as soon as disconnected.
// Note that this only works if Sound is Active !

QTime DieTime = QTime::currentTime().addMSecs ( 500 );
while ( ( QTime::currentTime() < DieTime ) && Channel.IsEnabled() )
{
// exclude user input events because if we use AllEvents, it happens
// that if the user initiates a connection and disconnection quickly
// (e.g. quickly pressing enter five times), the software can get into
// an unknown state
QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
}

// Now stop the audio interface
Sound.Stop();

// in case we timed out, log warning and make sure Channel is disabled
if ( Channel.IsEnabled() )
{
Channel.SetEnable ( false );
}

// Send disconnect message to server (Since we disable our protocol
// receive mechanism with the next command, we do not evaluate any
// respond from the server, therefore we just hope that the message
// gets its way to the server, if not, the old behaviour time-out
// disconnects the connection anyway).
ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress() );

// reset current signal level and LEDs
bJitterBufferOK = true;
SignalLevelMeter.Reset();
Stop();

emit Disconnected();

Expand Down
4 changes: 3 additions & 1 deletion src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ class CClient : public QObject

virtual ~CClient();

void Start();
bool Connect ( QString strServerAddress, QString strServerName );
bool Disconnect();

bool IsRunning() { return Sound.IsRunning(); }
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); } // For OnTimerCheckAudioDeviceOk only
bool SetServerAddr ( QString strNAddr );

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

bool GetAndResetbJitterBufferOKFlag();

bool IsConnected() { return Channel.IsConnected(); }

EGUIDesign GetGUIDesign() const { return eGUIDesign; }
Expand Down

0 comments on commit 1614b81

Please sign in to comment.