27
27
/* Implementation *************************************************************/
28
28
CClient::CClient ( const quint16 iPortNumber,
29
29
const quint16 iQosNumber,
30
- const QString& strConnOnStartupAddress,
31
30
const QString& strMIDISetup,
32
31
const bool bNoAutoJackConnect,
33
32
const QString& strNClientName,
@@ -184,13 +183,6 @@ CClient::CClient ( const quint16 iPortNumber,
184
183
// start the socket (it is important to start the socket after all
185
184
// initializations and connections)
186
185
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
- }
194
186
}
195
187
196
188
CClient::~CClient ()
@@ -337,7 +329,7 @@ void CClient::CreateServerJitterBufferMessage()
337
329
void CClient::OnCLPingReceived ( CHostAddress InetAddr, int iMs )
338
330
{
339
331
// make sure we are running and the server address is correct
340
- if ( IsRunning () && ( InetAddr == Channel.GetAddress () ) )
332
+ if ( Channel. IsEnabled () && ( InetAddr == Channel.GetAddress () ) )
341
333
{
342
334
// take care of wrap arounds (if wrapping, do not use result)
343
335
const int iCurDiff = EvaluatePingMessage ( iMs );
@@ -474,26 +466,6 @@ void CClient::StartDelayTimer()
474
466
}
475
467
}
476
468
477
- bool CClient::SetServerAddr ( QString strNAddr )
478
- {
479
- CHostAddress HostAddress;
480
- #ifdef CLIENT_NO_SRV_CONNECT
481
- if ( NetworkUtil ().ParseNetworkAddress ( strNAddr, HostAddress, bEnableIPv6 ) )
482
- #else
483
- if ( NetworkUtil ().ParseNetworkAddressWithSrvDiscovery ( strNAddr, HostAddress, bEnableIPv6 ) )
484
- #endif
485
- {
486
- // apply address to the channel
487
- Channel.SetAddress ( HostAddress );
488
-
489
- return true ;
490
- }
491
- else
492
- {
493
- return false ; // invalid address
494
- }
495
- }
496
-
497
469
bool CClient::GetAndResetbJitterBufferOKFlag ()
498
470
{
499
471
// get the socket buffer put status flag and reset it
@@ -626,12 +598,16 @@ QString CClient::SetSndCrdDev ( const QString strNewDev )
626
598
Sound.Start ();
627
599
}
628
600
629
- // in case of an error inform the GUI about it
630
601
if ( !strError.isEmpty () )
631
602
{
632
- emit SoundDeviceChanged ( strError );
603
+ // due to error, disconnect
604
+ Disconnect ();
633
605
}
634
606
607
+ // in case of an error, this will inform the GUI about it
608
+
609
+ emit SoundDeviceChanged ();
610
+
635
611
return strError;
636
612
}
637
613
@@ -758,8 +734,18 @@ void CClient::OnSndCrdReinitRequest ( int iSndCrdResetType )
758
734
}
759
735
MutexDriverReinit.unlock ();
760
736
737
+ if ( !strError.isEmpty () )
738
+ {
739
+ #ifndef HEADLESS
740
+ QMessageBox::critical ( 0 , APP_NAME, strError, tr ( " Ok" ) );
741
+ #else
742
+ qCritical () << qUtf8Printable ( strError );
743
+ exit ( 1 );
744
+ #endif
745
+ }
746
+
761
747
// inform GUI about the sound card device change
762
- emit SoundDeviceChanged ( strError );
748
+ emit SoundDeviceChanged ( );
763
749
}
764
750
765
751
void CClient::OnHandledSignal ( int sigNum )
@@ -773,11 +759,8 @@ void CClient::OnHandledSignal ( int sigNum )
773
759
{
774
760
case SIGINT:
775
761
case SIGTERM:
776
- // if connected, terminate connection (needed for headless mode)
777
- if ( IsRunning () )
778
- {
779
- Stop ();
780
- }
762
+ // if connected, terminate connection
763
+ Disconnect ();
781
764
782
765
// this should trigger OnAboutToQuit
783
766
QCoreApplication::instance ()->exit ();
@@ -862,50 +845,89 @@ void CClient::OnClientIDReceived ( int iChanID )
862
845
emit ClientIDReceived ( iChanID );
863
846
}
864
847
865
- void CClient::Start ( )
848
+ bool CClient::Connect ( QString strServerAddress, QString strServerName )
866
849
{
867
- // init object
868
- Init ();
850
+ if ( !Channel.IsEnabled () )
851
+ {
852
+ CHostAddress HostAddress;
853
+
854
+ if ( NetworkUtil ().ParseNetworkAddress ( strServerAddress, HostAddress, bEnableIPv6 ) )
855
+ {
856
+ // init object
857
+ Init ();
869
858
870
- // enable channel
871
- Channel.SetEnable ( true );
859
+ // apply address to the channel
860
+ Channel.SetAddress ( HostAddress );
872
861
873
- // start audio interface
874
- Sound.Start ();
862
+ // enable channel
863
+ Channel.SetEnable ( true );
864
+
865
+ // start audio interface
866
+ Sound.Start ();
867
+
868
+ // Notify ClientDlg
869
+ emit Connecting ( strServerName );
870
+
871
+ return true ;
872
+ }
873
+ }
874
+
875
+ return false ;
875
876
}
876
877
877
- void CClient::Stop ()
878
+ bool CClient::Disconnect ()
878
879
{
879
- // stop audio interface
880
- Sound.Stop ();
881
-
882
- // disable channel
883
- Channel.SetEnable ( false );
884
-
885
- // wait for approx. 100 ms to make sure no audio packet is still in the
886
- // network queue causing the channel to be reconnected right after having
887
- // received the disconnect message (seems not to gain much, disconnect is
888
- // still not working reliably)
889
- QTime DieTime = QTime::currentTime ().addMSecs ( 100 );
890
- while ( QTime::currentTime () < DieTime )
891
- {
892
- // exclude user input events because if we use AllEvents, it happens
893
- // that if the user initiates a connection and disconnection quickly
894
- // (e.g. quickly pressing enter five times), the software can get into
895
- // an unknown state
896
- QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
897
- }
898
-
899
- // Send disconnect message to server (Since we disable our protocol
900
- // receive mechanism with the next command, we do not evaluate any
901
- // respond from the server, therefore we just hope that the message
902
- // gets its way to the server, if not, the old behaviour time-out
903
- // disconnects the connection anyway).
904
- ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress () );
905
-
906
- // reset current signal level and LEDs
907
- bJitterBufferOK = true ;
908
- SignalLevelMeter.Reset ();
880
+ if ( Channel.IsEnabled () )
881
+ {
882
+ // start disconnection
883
+ Channel.Disconnect ();
884
+
885
+ // Channel.Disconnect() should automatically disable Channel as soon as disconnected.
886
+ // Note that this only works if Sound is Active !
887
+
888
+ QTime DieTime = QTime::currentTime ().addMSecs ( 500 );
889
+ while ( ( QTime::currentTime () < DieTime ) && Channel.IsEnabled () )
890
+ {
891
+ // exclude user input events because if we use AllEvents, it happens
892
+ // that if the user initiates a connection and disconnection quickly
893
+ // (e.g. quickly pressing enter five times), the software can get into
894
+ // an unknown state
895
+ QCoreApplication::processEvents ( QEventLoop::ExcludeUserInputEvents, 100 );
896
+ }
897
+
898
+ // Now stop the audio interface
899
+ Sound.Stop ();
900
+
901
+ // in case we timed out, log warning and make sure Channel is disabled
902
+ if ( Channel.IsEnabled () )
903
+ {
904
+ Channel.SetEnable ( false );
905
+ }
906
+
907
+ // Send disconnect message to server (Since we disable our protocol
908
+ // receive mechanism with the next command, we do not evaluate any
909
+ // respond from the server, therefore we just hope that the message
910
+ // gets its way to the server, if not, the old behaviour time-out
911
+ // disconnects the connection anyway).
912
+ ConnLessProtocol.CreateCLDisconnection ( Channel.GetAddress () );
913
+
914
+ // reset current signal level and LEDs
915
+ bJitterBufferOK = true ;
916
+ SignalLevelMeter.Reset ();
917
+
918
+ emit Disconnected ();
919
+
920
+ return true ;
921
+ }
922
+ else
923
+ {
924
+ // make sure sound is stopped too
925
+ Sound.Stop ();
926
+
927
+ emit Disconnected ();
928
+
929
+ return false ;
930
+ }
909
931
}
910
932
911
933
void CClient::Init ()
@@ -921,7 +943,7 @@ void CClient::Init()
921
943
bFraSiFactSafeSupported = true ;
922
944
#else
923
945
bFraSiFactPrefSupported = ( Sound.Init ( iFraSizePreffered ) == iFraSizePreffered );
924
- bFraSiFactDefSupported = ( Sound.Init ( iFraSizeDefault ) == iFraSizeDefault );
946
+ bFraSiFactDefSupported = ( Sound.Init ( iFraSizeDefault ) == iFraSizeDefault );
925
947
bFraSiFactSafeSupported = ( Sound.Init ( iFraSizeSafe ) == iFraSizeSafe );
926
948
#endif
927
949
0 commit comments