Skip to content

Commit d0c28bd

Browse files
committed
fix(openocd): fixed the port handler behavior
1 parent 682c158 commit d0c28bd

File tree

3 files changed

+11
-42
lines changed

3 files changed

+11
-42
lines changed

bundles/com.espressif.idf.core/src/com/espressif/idf/core/util/PortChecker.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.espressif.idf.core.util;
66

77
import java.io.IOException;
8+
import java.net.ServerSocket;
89
import java.net.Socket;
910

1011
import com.espressif.idf.core.logging.Logger;
@@ -25,45 +26,16 @@ private PortChecker()
2526

2627
public static boolean isPortAvailable(int port)
2728
{
28-
int attempts = 0;
29-
int retryCount = 3;
30-
long retryDelayMillis = 200;
31-
32-
while (attempts <= retryCount)
29+
try (ServerSocket serverSocket = new ServerSocket(port))
3330
{
34-
try (Socket ignored = new Socket("localhost", port)) //$NON-NLS-1$
35-
{
36-
// If the socket opens, the port is in use
37-
return false;
38-
}
39-
catch (IOException e)
40-
{
41-
// Port is unavailable, retrying if there are attempts left
42-
if (attempts == retryCount)
43-
{
44-
// After exhausting all retries, return false
45-
Logger.log("Port " + port + " is not available after " + retryCount + " retries."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
46-
return false; // Failure, port is still not available
47-
}
48-
49-
attempts++;
50-
51-
// Log retry attempt
52-
Logger.log("Attempt " + attempts + " failed, retrying in " + retryDelayMillis + " ms..."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
53-
54-
try
55-
{
56-
Thread.sleep(retryDelayMillis);
57-
}
58-
catch (InterruptedException interruptedException)
59-
{
60-
Thread.currentThread().interrupt(); // Restore interrupt status
61-
Logger.log("Port availability check interrupted."); //$NON-NLS-1$
62-
return false; // If interrupted, assume port unavailable and stop
63-
}
64-
}
31+
serverSocket.setReuseAddress(true);
32+
return true;
33+
}
34+
catch (Exception e)
35+
{
36+
Logger.log("Port: " + port + " is not available"); //$NON-NLS-1$ //$NON-NLS-2$
37+
return false;
6538
}
66-
return true; //Fallback not reachable
6739
}
6840

6941
/**

bundles/com.espressif.idf.debug.gdbjtag.openocd/src/com/espressif/idf/debug/gdbjtag/openocd/Configuration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public static String[] getGdbServerCommandLineArray(ILaunchConfiguration configu
9393
lst.add(executable);
9494

9595
int port = PortChecker
96-
.getAvailablePort(configuration.getAttribute(ConfigurationAttributes.GDB_SERVER_GDB_PORT_NUMBER,
97-
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));
96+
.getAvailablePort(DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
9897

9998
lst.add("-c"); //$NON-NLS-1$
10099
lst.add("gdb_port " + port); //$NON-NLS-1$

bundles/com.espressif.idf.debug.gdbjtag.openocd/src/com/espressif/idf/debug/gdbjtag/openocd/dsf/Launch.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ protected void provideDefaults(ILaunchConfigurationWorkingCopy config) throws Co
149149

150150
if (Configuration.getDoStartGdbServer(config))
151151
{
152-
int availableRemotePort = PortChecker.getAvailablePort(config.getAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER,
153-
DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT));
154-
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, availableRemotePort);
152+
config.setAttribute(IGDBJtagConstants.ATTR_PORT_NUMBER, DefaultPreferences.GDB_SERVER_GDB_PORT_NUMBER_DEFAULT);
155153
}
156154

157155
config.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, CustomIdfProcessFactory.ID);

0 commit comments

Comments
 (0)