Skip to content

Commit eee71e8

Browse files
feat: improvements
1 parent 35cda61 commit eee71e8

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/plugins/terminal/src/android/Executor.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.net.ServerSocket;
3333
import java.io.IOException;
34+
import java.net.InetSocketAddress;
3435

3536
public class Executor extends CordovaPlugin {
3637

@@ -256,26 +257,29 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
256257
return true;
257258
}
258259

259-
if(actions.equals("spawn")){
260-
try{
260+
if (action.equals("spawn")) {
261+
try {
261262
JSONArray cmdArr = args.getJSONArray(0);
262263
String[] cmd = new String[cmdArr.length()];
263264
for (int i = 0; i < cmdArr.length(); i++) {
264265
cmd[i] = cmdArr.getString(i);
265266
}
266267

267-
ServerSocket socket = findFreePort();
268-
int port = socket.getLocalPort();
268+
int port;
269+
try (ServerSocket socket = new ServerSocket(0)) {
270+
socket.setReuseAddress(true);
271+
port = socket.getLocalPort();
272+
}
269273

270-
ProcessServer server = new ProcessServer(socket, cmd);
274+
ProcessServer server = new ProcessServer(port, cmd);
271275
server.start();
272276

273277
callbackContext.success(port);
274-
}catch(Exception e){
278+
} catch (Exception e) {
275279
e.printStackTrace();
276280
callbackContext.error("Failed to spawn process: " + e.getMessage());
277281
}
278-
282+
279283
return true;
280284
}
281285

@@ -318,12 +322,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
318322
}
319323
}
320324

321-
private ServerSocket findFreePort() throws IOException {
322-
ServerSocket socket = new ServerSocket(0);
323-
socket.setReuseAddress(true);
324-
return socket;
325-
}
326-
327325
private void stopServiceNow() {
328326
if (isServiceBound) {
329327
try {

src/plugins/terminal/src/android/ProcessServer.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66

77
import java.io.InputStream;
88
import java.io.OutputStream;
9-
import java.net.InetSocketAddress;
9+
import java.net.ServerSocket;
1010
import java.nio.ByteBuffer;
11+
import java.net.InetSocketAddress;
1112

1213
class ProcessServer extends WebSocketServer {
1314

1415
private final String[] cmd;
1516

16-
private record ConnState(Process process, OutputStream stdin) {}
17+
private static final class ConnState {
18+
final Process process;
19+
final OutputStream stdin;
20+
21+
ConnState(Process process, OutputStream stdin) {
22+
this.process = process;
23+
this.stdin = stdin;
24+
}
25+
}
1726

18-
ProcessServer(ServerSocket socket, String[] cmd) {
19-
super(socket);
27+
ProcessServer(int port, String[] cmd) {
28+
super(new InetSocketAddress(port));
2029
this.cmd = cmd;
2130
}
2231

@@ -46,17 +55,17 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
4655
public void onMessage(WebSocket conn, ByteBuffer msg) {
4756
try {
4857
ConnState state = conn.getAttachment();
49-
state.stdin().write(msg.array(), msg.position(), msg.remaining());
50-
state.stdin().flush();
58+
state.stdin.write(msg.array(), msg.position(), msg.remaining());
59+
state.stdin.flush();
5160
} catch (Exception ignored) {}
5261
}
5362

5463
@Override
5564
public void onMessage(WebSocket conn, String message) {
5665
try {
5766
ConnState state = conn.getAttachment();
58-
state.stdin().write(message.getBytes());
59-
state.stdin().flush();
67+
state.stdin.write(message.getBytes());
68+
state.stdin.flush();
6069
} catch (Exception ignored) {}
6170
}
6271

@@ -76,7 +85,7 @@ public void onStart() {}
7685
private void stopProcess(WebSocket conn) {
7786
try {
7887
ConnState state = conn.getAttachment();
79-
if (state != null) state.process().destroy();
88+
if (state != null) state.process.destroy();
8089
stop();
8190
} catch (Exception ignored) {}
8291
}

0 commit comments

Comments
 (0)