Revision: 29698
          http://sourceforge.net/p/bibdesk/svn/29698
Author:   hofman
Date:     2025-10-10 17:35:34 +0000 (Fri, 10 Oct 2025)
Log Message:
-----------
Update try count from calling method. Set sharing name of server only when 
setting up the DO connection succeeds.

Modified Paths:
--------------
    trunk/bibdesk/BDSKSharingServer.m

Modified: trunk/bibdesk/BDSKSharingServer.m
===================================================================
--- trunk/bibdesk/BDSKSharingServer.m   2025-10-10 16:50:04 UTC (rev 29697)
+++ trunk/bibdesk/BDSKSharingServer.m   2025-10-10 17:35:34 UTC (rev 29698)
@@ -136,10 +136,11 @@
 }
 #pragma clang diagnostic pop
 
-- (instancetype)initForSharingServer:(BDSKSharingServer *)aSharingServer 
NS_DESIGNATED_INITIALIZER;
+- (instancetype)initWithSharingName:(NSString *)aSharingName 
forSharingServer:(BDSKSharingServer *)aSharingServer NS_DESIGNATED_INITIALIZER;
 - (instancetype)init NS_UNAVAILABLE;
 
 @property NSUInteger numberOfConnections;
+@property (nonatomic, readonly) NSString *sharingName;
 
 - (void)notifyClientsOfChange;
 
@@ -334,19 +335,19 @@
 
 - (void)_enableSharing
 {
-    if (status == BDSKSharingStatusOff && tryCount < MAX_TRY_COUNT) {
-        // set the (next) sharing name
-        if (tryCount == 0)
-            [self setSharingName:[BDSKSharingServer sharingName]];
-        else
-            [self setSharingName:[NSString stringWithFormat:@"%@-%ld", 
[BDSKSharingServer sharingName], (long)tryCount]];
-        ++tryCount;
-        
-        asyncServer = [[BDSKAsyncSharingServer alloc] 
initForSharingServer:self];
-        // the netService is created in the callback
-        
-        [self setStatus:BDSKSharingStatusStarting];
-    }
+    BDSKASSERT(status == BDSKSharingStatusOff);
+    
+    // set the (next) sharing name
+    NSString *trySharingName = nil;
+    if (tryCount == 0)
+        trySharingName = [BDSKSharingServer sharingName];
+    else
+        trySharingName = [NSString stringWithFormat:@"%@-%ld", 
[BDSKSharingServer sharingName], (long)tryCount];
+    
+    asyncServer = [[BDSKAsyncSharingServer alloc] 
initWithSharingName:trySharingName forSharingServer:self];
+    // the netService is created in the callback
+    
+    [self setStatus:BDSKSharingStatusStarting];
 }
 
 - (void)enableSharing
@@ -415,6 +416,8 @@
     
     BDSKPRECONDITION(netService == nil);
     
+    [self setSharingName:[aServer sharingName]];
+    
     // lazily instantiate the NSNetService object that will advertise on our 
behalf
     netService = [self newNetServiceWithSharingName:[self sharingName] 
port:port];
     
@@ -488,7 +491,10 @@
     [self setStatus:BDSKSharingStatusOff];
     
     // try again with a different name
-    [self _enableSharing];
+    if (tryCount < MAX_TRY_COUNT) {
+        ++tryCount;
+        [self _enableSharing];
+    }
 }
 
 - (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary 
*)errorDict
@@ -498,8 +504,10 @@
     [self disableSharing];
     
     // if we have a name collision, restart with another name
-    if (err == NSNetServicesCollisionError)
+    if (err == NSNetServicesCollisionError && tryCount < MAX_TRY_COUNT) {
+        ++tryCount;
         [self _enableSharing];
+    }
     
     if (status == BDSKSharingStatusOff) {
         // we did not restart, either we had another error, or we tried too 
many times
@@ -574,6 +582,7 @@
 
 @implementation BDSKAsyncSharingServer
 
+@synthesize sharingName;
 @dynamic numberOfConnections;
 
 // This is the minimal version for the client that we require
@@ -580,12 +589,12 @@
 // If we introduce incompatible changes in future, bump this to avoid sharing 
breakage
 + (NSString *)requiredClientProtocolVersion { return @"0"; }
 
-- (instancetype)initForSharingServer:(BDSKSharingServer *)aSharingServer
+- (instancetype)initWithSharingName:(NSString *)aSharingName 
forSharingServer:(BDSKSharingServer *)aSharingServer 
 {
     self = [super init];
     if (self) {
         sharingServer = aSharingServer;
-        sharingName = [sharingServer sharingName];
+        sharingName = aSharingName;
         connectedClients = [[NSMutableArray alloc] init];
         registeredClients = [[NSMutableDictionary alloc] init];
         numberOfConnections = 0;

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to