Revision: 21849
          http://sourceforge.net/p/bibdesk/svn/21849
Author:   hofman
Date:     2018-02-09 13:44:31 +0000 (Fri, 09 Feb 2018)
Log Message:
-----------
use port number for DO sharing for service, if we can find it, so we don't need 
to create a dummy port

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

Modified: trunk/bibdesk/BDSKSharingServer.m
===================================================================
--- trunk/bibdesk/BDSKSharingServer.m   2018-02-09 07:30:38 UTC (rev 21848)
+++ trunk/bibdesk/BDSKSharingServer.m   2018-02-09 13:44:31 UTC (rev 21849)
@@ -101,7 +101,7 @@
 @end
 
 @protocol BDSKSharingServerMainThread <BDSKAsyncDOServerMainThread>
-- (oneway void)localThreadServerDidSetup:(BOOL)success;
+- (oneway void)localThreadServerDidSetup:(BOOL)success port:(int)port;
 @end
 
 #pragma mark -
@@ -253,38 +253,38 @@
     [self restartSharingIfNeeded];
 }
 
-- (NSNetService *)newNetServiceWithSharingName:(NSString *)aSharingName {
-    uint16_t chosenPort = 0;
-    
-    // Here, create the socket from traditional BSD socket calls
-    // keep track of this so we can close it when we stop the net service
-    socketDescriptor = -1;
-    struct sockaddr_in serverAddress;
-    socklen_t namelen = sizeof(serverAddress);
-    
-    if((socketDescriptor = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
-        memset(&serverAddress, 0, sizeof(serverAddress));
-        serverAddress.sin_family = AF_INET;
-        serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
-        serverAddress.sin_port = 0; // allows the kernel to choose the port 
for us.
+- (NSNetService *)newNetServiceWithSharingName:(NSString *)aSharingName 
port:(int)port {
+    if (port == 0) {
+        // if we could find no port number, create a dummy socket from 
traditional BSD socket calls
+        // keep track of this so we can close it when we stop the net service
+        socketDescriptor = -1;
+        struct sockaddr_in serverAddress;
+        socklen_t namelen = sizeof(serverAddress);
         
-        if(bind(socketDescriptor, (struct sockaddr *)&serverAddress, 
sizeof(serverAddress)) < 0) {
-            close(socketDescriptor);
-            socketDescriptor = -1;
-            return nil;
+        if((socketDescriptor = socket(AF_INET, SOCK_STREAM, 0)) > 0) {
+            memset(&serverAddress, 0, sizeof(serverAddress));
+            serverAddress.sin_family = AF_INET;
+            serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
+            serverAddress.sin_port = 0; // allows the kernel to choose the 
port for us.
+            
+            if(bind(socketDescriptor, (struct sockaddr *)&serverAddress, 
sizeof(serverAddress)) < 0) {
+                close(socketDescriptor);
+                socketDescriptor = -1;
+                return nil;
+            }
+            
+            // Find out what port number was chosen for us.
+            if(getsockname(socketDescriptor, (struct sockaddr 
*)&serverAddress, &namelen) < 0) {
+                close(socketDescriptor);
+                socketDescriptor = -1;
+                return nil;
+            }
+            
+            port = ntohs(serverAddress.sin_port);
         }
-        
-        // Find out what port number was chosen for us.
-        if(getsockname(socketDescriptor, (struct sockaddr *)&serverAddress, 
&namelen) < 0) {
-            close(socketDescriptor);
-            socketDescriptor = -1;
-            return nil;
-        }
-        
-        chosenPort = ntohs(serverAddress.sin_port);
     }
     
-    NSNetService *aNetService = [[NSNetService alloc] initWithDomain:@"" 
type:BDSKNetServiceDomain name:aSharingName port:chosenPort];
+    NSNetService *aNetService = [[NSNetService alloc] initWithDomain:@"" 
type:BDSKNetServiceDomain name:aSharingName port:port];
     [aNetService setDelegate:self];
     NSMutableDictionary *dictionary = [NSMutableDictionary 
dictionaryWithCapacity:4];
     [dictionary setObject:[BDSKSharingServer supportedProtocolVersion] 
forKey:BDSKTXTVersionKey];
@@ -333,7 +333,8 @@
         [netService stop];
         BDSKDESTROY(netService);
         
-        close(socketDescriptor);
+        if(socketDescriptor != -1)
+            close(socketDescriptor);
         socketDescriptor = -1;
         
         [server stopDOServer];
@@ -366,7 +367,7 @@
     }
 }
 
-- (void)server:(BDSKSharingDOServer *)aServer didSetup:(BOOL)success {
+- (void)server:(BDSKSharingDOServer *)aServer didSetup:(BOOL)success 
port:(int)port {
     BDSKASSERT(aServer == server || server == nil);
     if (success) {
         // the service was able to register the port
@@ -374,7 +375,7 @@
         BDSKPRECONDITION(netService == nil);
         
         // lazily instantiate the NSNetService object that will advertise on 
our behalf
-        netService = [self newNetServiceWithSharingName:[self sharingName]];
+        netService = [self newNetServiceWithSharingName:[self sharingName] 
port:port];
         
         BDSKPOSTCONDITION(netService != nil);
         
@@ -587,9 +588,9 @@
     [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKClientConnectionsChangedNotification object:nil];
 }
 
-- (oneway void)localThreadServerDidSetup:(BOOL)success
+- (oneway void)localThreadServerDidSetup:(BOOL)success port:(int)port
 {
-    [sharingServer server:(BDSKSharingDOServer *)self didSetup:success];
+    [sharingServer server:(BDSKSharingDOServer *)self didSetup:success 
port:port];
 }
 
 #pragma mark Server Thread
@@ -604,11 +605,19 @@
 {
     // setup our DO server that will handle requests for publications and 
passwords
     BOOL success = YES;
+    int port = 0;
     @try {
         NSPort *receivePort = [NSSocketPort port];
         if([[NSSocketPortNameServer sharedInstance] registerPort:receivePort 
name:sharingName] == NO)
             @throw [NSString stringWithFormat:@"*** BDSKSharingDOServer: 
Unable to register, socket port = %p, name = '%@'", receivePort, sharingName];
         
+        // find out what port was chosen for us
+        struct sockaddr *address = (struct sockaddr *)[[(NSSocketPort 
*)receivePort address] bytes];
+        if(address->sa_family == AF_INET)
+            port = ntohs(((struct sockaddr_in *)address)->sin_port);
+        else if(address->sa_family == AF_INET6)
+            port = ntohs(((struct sockaddr_in6 *)address)->sin6_port);
+
         connection = [[NSConnection alloc] initWithReceivePort:receivePort 
sendPort:nil];
         NSProtocolChecker *checker = [NSProtocolChecker 
protocolCheckerWithTarget:self protocol:@protocol(BDSKSharingServer)];
         [connection setRootObject:checker];
@@ -622,7 +631,7 @@
         // the callback from the delegate should stop the DO server, and may 
try again with a different name
     }
     @finally {
-        [[self serverOnMainThread] localThreadServerDidSetup:success];
+        [[self serverOnMainThread] localThreadServerDidSetup:success 
port:port];
     }
 }
 

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to