From 052b17c8643a5fde94b0fe3ba7fe6a6c1ae2296f Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sat, 12 Mar 2022 19:59:39 +0100 Subject: [PATCH] add server sockets and make stream api thread safe --- bootstrap/ISBPL.java | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/bootstrap/ISBPL.java b/bootstrap/ISBPL.java index c2d0479..48729ca 100644 --- a/bootstrap/ISBPL.java +++ b/bootstrap/ISBPL.java @@ -22,7 +22,7 @@ public class ISBPL { HashMap vars = new HashMap<>(); final ISBPLThreadLocal> lastWords = ISBPLThreadLocal.withInitial(() -> new ArrayList<>(16)); int exitCode; - private ISBPLStreamer streamer = new ISBPLStreamer(this); + private final ISBPLStreamer streamer = new ISBPLStreamer(this); ArrayList included = new ArrayList<>(); public ISBPL() { @@ -1069,7 +1069,7 @@ public class ISBPL { ; } - public static void main(String[] args) throws IOException { + public static void main(String[] args) { Stack stack = new Stack<>(); ISBPL isbpl = new ISBPL(); isbpl.debuggerIPC.stack.put(Thread.currentThread().getId(), stack); @@ -1512,6 +1512,7 @@ class ISBPLStreamer { public static final int WRITE = 5; public static final int AREAD = 6; public static final int AWRITE = 7; + public static final int CREATE_SERVER = 9; static class ISBPLStream { final InputStream in; @@ -1538,7 +1539,7 @@ class ISBPLStreamer { public ArrayList streams = new ArrayList<>(); - public void action(Stack stack, int action) throws IOException { + public synchronized void action(Stack stack, int action) throws IOException { ISBPLStream stream; ISBPLObject s, i; File f; @@ -1579,6 +1580,26 @@ class ISBPLStreamer { streams.add(stream); stack.push(new ISBPLObject(isbpl.getType("int"), stream.id)); break; + case CREATE_SERVER: + i = stack.pop(); + i.checkType(isbpl.getType("int")); + ServerSocket server = new ServerSocket(((int) i.object)); + stream = new ISBPLStream(new InputStream() { + @Override + public int read() throws IOException { + Socket socket = server.accept(); + ISBPLStream stream = new ISBPLStream(socket.getInputStream(), socket.getOutputStream()); + return stream.id; + } + }, new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new ISBPLError("IllegalArgument", "Can't write to a SERVER stream!"); + } + }); + streams.add(stream); + stack.push(new ISBPLObject(isbpl.getType("int"), stream.id)); + break; case READ: i = stack.pop(); i.checkType(isbpl.getType("int"));