@@ -795,31 +795,14 @@ public bool StartServer()
795795 {
796796 if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
797797 {
798- NetworkLog . LogInfo ( "StartServer()" ) ;
798+ NetworkLog . LogInfo ( nameof ( StartServer ) ) ;
799799 }
800800
801- if ( IsServer || IsClient )
801+ if ( ! CanStart ( StartType . Server ) )
802802 {
803- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
804- {
805- NetworkLog . LogWarning ( "Cannot start server while an instance is already running" ) ;
806- }
807-
808803 return false ;
809804 }
810805
811- if ( NetworkConfig . ConnectionApproval )
812- {
813- if ( ConnectionApprovalCallback == null )
814- {
815- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
816- {
817- NetworkLog . LogWarning (
818- "No ConnectionApproval callback defined. Connection approval will timeout" ) ;
819- }
820- }
821- }
822-
823806 Initialize ( true ) ;
824807
825808 // If we failed to start then shutdown and notify user that the transport failed to start
@@ -853,13 +836,8 @@ public bool StartClient()
853836 NetworkLog . LogInfo ( nameof ( StartClient ) ) ;
854837 }
855838
856- if ( IsServer || IsClient )
839+ if ( ! CanStart ( StartType . Client ) )
857840 {
858- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
859- {
860- NetworkLog . LogWarning ( "Cannot start client while an instance is already running" ) ;
861- }
862-
863841 return false ;
864842 }
865843
@@ -890,28 +868,11 @@ public bool StartHost()
890868 NetworkLog . LogInfo ( nameof ( StartHost ) ) ;
891869 }
892870
893- if ( IsServer || IsClient )
871+ if ( ! CanStart ( StartType . Host ) )
894872 {
895- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
896- {
897- NetworkLog . LogWarning ( "Cannot start host while an instance is already running" ) ;
898- }
899-
900873 return false ;
901874 }
902875
903- if ( NetworkConfig . ConnectionApproval )
904- {
905- if ( ConnectionApprovalCallback == null )
906- {
907- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
908- {
909- NetworkLog . LogWarning (
910- "No ConnectionApproval callback defined. Connection approval will timeout" ) ;
911- }
912- }
913- }
914-
915876 Initialize ( true ) ;
916877
917878 // If we failed to start then shutdown and notify user that the transport failed to start
@@ -960,6 +921,52 @@ public bool StartHost()
960921 return true ;
961922 }
962923
924+ private enum StartType
925+ {
926+ Server ,
927+ Host ,
928+ Client
929+ }
930+
931+ private bool CanStart ( StartType type )
932+ {
933+ if ( IsListening )
934+ {
935+ if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
936+ {
937+ NetworkLog . LogWarning ( "Cannot start " + type + " while an instance is already running" ) ;
938+ }
939+
940+ return false ;
941+ }
942+
943+ if ( NetworkConfig . ConnectionApproval )
944+ {
945+ if ( ConnectionApprovalCallback == null )
946+ {
947+ if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
948+ {
949+ NetworkLog . LogWarning (
950+ "No ConnectionApproval callback defined. Connection approval will timeout" ) ;
951+ }
952+ }
953+ }
954+
955+ if ( ConnectionApprovalCallback != null )
956+ {
957+ if ( ! NetworkConfig . ConnectionApproval )
958+ {
959+ if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
960+ {
961+ NetworkLog . LogWarning (
962+ "A ConnectionApproval callback is defined but ConnectionApproval is disabled. In order to use ConnectionApproval it has to be explicitly enabled " ) ;
963+ }
964+ }
965+ }
966+
967+ return true ;
968+ }
969+
963970 public void SetSingleton ( )
964971 {
965972 Singleton = this ;
0 commit comments