@@ -795,31 +795,14 @@ public bool StartServer()
795
795
{
796
796
if ( NetworkLog . CurrentLogLevel <= LogLevel . Developer )
797
797
{
798
- NetworkLog . LogInfo ( "StartServer()" ) ;
798
+ NetworkLog . LogInfo ( nameof ( StartServer ) ) ;
799
799
}
800
800
801
- if ( IsServer || IsClient )
801
+ if ( ! CanStart ( StartType . Server ) )
802
802
{
803
- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
804
- {
805
- NetworkLog . LogWarning ( "Cannot start server while an instance is already running" ) ;
806
- }
807
-
808
803
return false ;
809
804
}
810
805
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
-
823
806
Initialize ( true ) ;
824
807
825
808
// If we failed to start then shutdown and notify user that the transport failed to start
@@ -853,13 +836,8 @@ public bool StartClient()
853
836
NetworkLog . LogInfo ( nameof ( StartClient ) ) ;
854
837
}
855
838
856
- if ( IsServer || IsClient )
839
+ if ( ! CanStart ( StartType . Client ) )
857
840
{
858
- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
859
- {
860
- NetworkLog . LogWarning ( "Cannot start client while an instance is already running" ) ;
861
- }
862
-
863
841
return false ;
864
842
}
865
843
@@ -890,28 +868,11 @@ public bool StartHost()
890
868
NetworkLog . LogInfo ( nameof ( StartHost ) ) ;
891
869
}
892
870
893
- if ( IsServer || IsClient )
871
+ if ( ! CanStart ( StartType . Host ) )
894
872
{
895
- if ( NetworkLog . CurrentLogLevel <= LogLevel . Normal )
896
- {
897
- NetworkLog . LogWarning ( "Cannot start host while an instance is already running" ) ;
898
- }
899
-
900
873
return false ;
901
874
}
902
875
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
-
915
876
Initialize ( true ) ;
916
877
917
878
// If we failed to start then shutdown and notify user that the transport failed to start
@@ -960,6 +921,52 @@ public bool StartHost()
960
921
return true ;
961
922
}
962
923
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
+
963
970
public void SetSingleton ( )
964
971
{
965
972
Singleton = this ;
0 commit comments