#FEATURE by cg default
authorClaus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5475 8e2403d81a18
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Socket.st
--- a/Socket.st	Thu Apr 16 17:14:42 2020 +0200
+++ b/Socket.st	Sat May 02 21:40:13 2020 +0200
@@ -925,7 +925,7 @@
     "Created: 31.10.1995 / 18:54:11 / cg"
 !
 
-newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:milliSecondsOrTimeDuration
+newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil domainOrder:orderOrNil withTimeout:milliSecondsOrTimeDuration
     "create a new TCP client socket connecting to a service on hostNameOrAddress.
      If hostNameOrAddress is a string, try all the resolved addresses.
      Return a socket instance if ok, nil on failure.
@@ -937,40 +937,44 @@
     |socket addressList lastDomainSymbol lastNotification|
 
     hostNameOrAddress isString ifFalse:[
-	^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:milliSecondsOrTimeDuration.
+        ^ self newTCPclientToAddress:hostNameOrAddress port:aPortOrServiceName withTimeout:milliSecondsOrTimeDuration.
     ].
 
     addressList := SocketAddress
-			allForHostName:hostNameOrAddress
-			serviceName:aPortOrServiceName
-			domain:aDomainSymbolOrNil
-			type:#stream.
+                        allForHostName:hostNameOrAddress
+                        serviceName:aPortOrServiceName
+                        domain:aDomainSymbolOrNil
+                        type:#stream.
+
+    orderOrNil notNil ifTrue:[
+        addressList sort:[:a1 :a2 | (orderOrNil indexOf:a1 domain) < (orderOrNil indexOf:a2 domain)].
+    ].
 
     addressList do:[:eachAddress|
-	|domainSymbol|
-
-	domainSymbol := eachAddress domain.
-	domainSymbol ~~ lastDomainSymbol ifTrue:[
-	    socket notNil ifTrue:[
-		socket close.
-	    ].
-	    socket := self new domain:domainSymbol type:#stream.
-	    lastDomainSymbol := domainSymbol.
-	].
-	[
-	    (socket connectTo:eachAddress withTimeout:milliSecondsOrTimeDuration) ifTrue:[
-		^ socket.
-	    ].
-	] on:SocketErrorNotification do:[:ex|
-	    lastNotification := ex.
-	].
+        |domainSymbol|
+
+        domainSymbol := eachAddress domain.
+        domainSymbol ~~ lastDomainSymbol ifTrue:[
+            socket notNil ifTrue:[
+                socket close.
+            ].
+            socket := self new domain:domainSymbol type:#stream.
+            lastDomainSymbol := domainSymbol.
+        ].
+        [
+            (socket connectTo:eachAddress withTimeout:milliSecondsOrTimeDuration) ifTrue:[
+                ^ socket.
+            ].
+        ] on:SocketErrorNotification do:[:ex|
+            lastNotification := ex.
+        ].
     ].
     socket notNil ifTrue:[
-	socket close.
+        socket close.
     ].
     lastNotification notNil ifTrue:[
-	"if someone is interested in the notification - raise the last one"
-	lastNotification raiseRequest.
+        "if someone is interested in the notification - raise the last one"
+        lastNotification raiseRequest.
     ].
     ^ nil.
 
@@ -980,15 +984,32 @@
       Socket newTCPclientToHost:'www.exept.de' port:80 domain:nil withTimeout:1000.
 
       [
-	"if nobody is listening (Connection refused") the timeout does not apply"
-	Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:10s
+        "if nobody is listening (Connection refused") the timeout does not apply"
+        Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:10s
       ] on:SocketErrorNotification do:[:ex|
-	ex halt.
+        ex halt.
       ].
 EOC"
 
+    "Created: / 02-05-2020 / 21:24:28 / cg"
+!
+
+newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil withTimeout:milliSecondsOrTimeDuration
+    "create a new TCP client socket connecting to a service on hostNameOrAddress.
+     If hostNameOrAddress is a string, try all the resolved addresses.
+     Return a socket instance if ok, nil on failure.
+     Set aDomainSymbolOrNil to #AF_INET of #AF_INET6 to connect via a defined protocol.
+     Set aDomainSymbolOrNil to nil, to try all protocols.
+     If the milliSecondsOrTimeDuration arg is nonNil, stop trying to connect after that many milliSecondsOrTimeDurationeconds
+     and return nil."
+
+    ^ self
+        newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName domain:aDomainSymbolOrNil 
+        domainOrder:nil withTimeout:milliSecondsOrTimeDuration
+
     "Modified: / 19-01-2018 / 18:12:29 / stefan"
     "Modified: / 28-01-2019 / 10:48:18 / Stefan Vogel"
+    "Modified: / 02-05-2020 / 21:39:18 / cg"
 !
 
 newTCPclientToHost:hostNameOrAddress port:aPortOrServiceName withTimeout:milliSecondsOrTimeDuration