diff --git a/src/dorkbox/network/Client.kt b/src/dorkbox/network/Client.kt index f781fc7d..d690bdad 100644 --- a/src/dorkbox/network/Client.kt +++ b/src/dorkbox/network/Client.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -153,6 +153,9 @@ open class Client(config: ClientConfiguration = ClientC private val handshake = ClientHandshake(this, logger) + @Volatile + internal var clientConnectionInProgress = CountDownLatch(0) + @Volatile private var slowDownForException = false @@ -562,10 +565,12 @@ open class Client(config: ClientConfiguration = ClientC handshakeTimeoutNs = TimeUnit.HOURS.toNanos(1) } + val startTime = System.nanoTime() var success = false while (!stopConnectOnShutdown && (connectionTimoutInNs == 0L || System.nanoTime() - startTime < connectionTimoutInNs)) { logger.trace("Starting connect process...") + clientConnectionInProgress = CountDownLatch(1) if (isShutdown()) { resetOnError() @@ -600,7 +605,7 @@ open class Client(config: ClientConfiguration = ClientC // throws a ConnectTimedOutException if the client cannot connect for any reason to the server handshake ports handshakeConnection = ClientHandshakeDriver.build( - config = config, + endpoint = this, aeronDriver = aeronDriver, autoChangeToIpc = autoChangeToIpc, remoteAddress = remoteAddress, @@ -609,6 +614,7 @@ open class Client(config: ClientConfiguration = ClientC clientListenPort = config.port, remotePort2 = port2, handshakeTimeoutNs = handshakeTimeoutNs, + connectionTimoutInNs = connectionTimoutInNs, reliable = reliable, tagName = tag, logger = logger @@ -626,12 +632,15 @@ open class Client(config: ClientConfiguration = ClientC connect0(handshake, handshakeConnection, handshakeTimeoutNs) success = true slowDownForException = false + clientConnectionInProgress.countDown() // once we're done with the connection process, stop trying break } catch (e: ClientRetryException) { + clientConnectionInProgress.countDown() + aeronDriver.closeIfSingle() // if we are the ONLY instance using the media driver, restart it + if (stopConnectOnShutdown) { - aeronDriver.closeIfSingle() break } @@ -648,11 +657,9 @@ open class Client(config: ClientConfiguration = ClientC logger.info(message) } - // maybe the aeron driver isn't running? (or isn't running correctly?) - aeronDriver.closeIfSingle() // if we are the ONLY instance using the media driver, stop it - slowDownForException = true } catch (e: ClientRejectedException) { + clientConnectionInProgress.countDown() aeronDriver.closeIfSingle() // if we are the ONLY instance using the media driver, stop it if (stopConnectOnShutdown) { @@ -673,6 +680,7 @@ open class Client(config: ClientConfiguration = ClientC throw e } } catch (e: Exception) { + clientConnectionInProgress.countDown() aeronDriver.closeIfSingle() // if we are the ONLY instance using the media driver, restart it if (stopConnectOnShutdown) { @@ -696,7 +704,7 @@ open class Client(config: ClientConfiguration = ClientC endpointIsRunning.lazySet(false) if (stopConnectOnShutdown) { - val exception = ClientException("Client closed during connection attempt. Aborting connection attempts.").cleanStackTrace(3) + val exception = ClientException("Client closed during connection attempt to '$remoteAddressString'. Aborting connection attempts.").cleanStackTrace(3) listenerManager.notifyError(exception) // if we are waiting for this connection to connect (on a different thread, for example), make sure to release it. closeLatch.countDown() @@ -800,6 +808,7 @@ open class Client(config: ClientConfiguration = ClientC // we are now connected, so we can connect to the NEW client-specific ports val clientConnection = ClientConnectionDriver.build( + shutdown = shutdown, aeronDriver = aeronDriver, handshakeTimeoutNs = handshakeTimeoutNs, handshakeConnection = handshakeConnection, diff --git a/src/dorkbox/network/Server.kt b/src/dorkbox/network/Server.kt index f1eff01f..4efe2ad7 100644 --- a/src/dorkbox/network/Server.kt +++ b/src/dorkbox/network/Server.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/aeron/AeronDriver.kt b/src/dorkbox/network/aeron/AeronDriver.kt index 6680cc61..1bf5222f 100644 --- a/src/dorkbox/network/aeron/AeronDriver.kt +++ b/src/dorkbox/network/aeron/AeronDriver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/aeron/AeronDriverInternal.kt b/src/dorkbox/network/aeron/AeronDriverInternal.kt index 7b08076e..e22308aa 100644 --- a/src/dorkbox/network/aeron/AeronDriverInternal.kt +++ b/src/dorkbox/network/aeron/AeronDriverInternal.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/aeron/EventPoller.kt b/src/dorkbox/network/aeron/EventPoller.kt index 2a73649c..d98bf746 100644 --- a/src/dorkbox/network/aeron/EventPoller.kt +++ b/src/dorkbox/network/aeron/EventPoller.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/connection/EndPoint.kt b/src/dorkbox/network/connection/EndPoint.kt index 55ba3411..cf3c42be 100644 --- a/src/dorkbox/network/connection/EndPoint.kt +++ b/src/dorkbox/network/connection/EndPoint.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/handshake/ClientConnectionDriver.kt b/src/dorkbox/network/handshake/ClientConnectionDriver.kt index d7286770..d72779d7 100644 --- a/src/dorkbox/network/handshake/ClientConnectionDriver.kt +++ b/src/dorkbox/network/handshake/ClientConnectionDriver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/handshake/ClientHandshake.kt b/src/dorkbox/network/handshake/ClientHandshake.kt index 4891e950..b4cf9609 100644 --- a/src/dorkbox/network/handshake/ClientHandshake.kt +++ b/src/dorkbox/network/handshake/ClientHandshake.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/handshake/ClientHandshakeDriver.kt b/src/dorkbox/network/handshake/ClientHandshakeDriver.kt index a9c3018b..46032461 100644 --- a/src/dorkbox/network/handshake/ClientHandshakeDriver.kt +++ b/src/dorkbox/network/handshake/ClientHandshakeDriver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/dorkbox/network/handshake/ServerHandshakePollers.kt b/src/dorkbox/network/handshake/ServerHandshakePollers.kt index 9ccfb0c8..41ca1329 100644 --- a/src/dorkbox/network/handshake/ServerHandshakePollers.kt +++ b/src/dorkbox/network/handshake/ServerHandshakePollers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 dorkbox, llc + * Copyright 2024 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.