diff options
author | dweller <dweller@cabin.digital> | 2024-03-09 00:55:36 +0200 |
---|---|---|
committer | dweller <dweller@cabin.digital> | 2024-03-09 00:55:36 +0200 |
commit | 86d3f93ee338b28ab7d40aa83c129cf6b97ef4b7 (patch) | |
tree | 507a8d66932e6dea9b121dfcbf980f7925575c9f /java/sources/digital/cabin/ezipc/EzIPC.java |
Initial commit, 2 years later
Diffstat (limited to '')
-rw-r--r-- | java/sources/digital/cabin/ezipc/EzIPC.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/java/sources/digital/cabin/ezipc/EzIPC.java b/java/sources/digital/cabin/ezipc/EzIPC.java new file mode 100644 index 0000000..7c9943d --- /dev/null +++ b/java/sources/digital/cabin/ezipc/EzIPC.java @@ -0,0 +1,106 @@ +package digital.cabin.ezipc; + + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.platform.linux.ErrNo; + + +public abstract class EzIPC +{ + protected interface LibraryNamedFIFO extends Library + { + static LibraryNamedFIFO INSTANCE = + Native.load("c", LibraryNamedFIFO.class); + + public int mkfifo(String pathname, int mode); + } + + + protected LibraryNamedFIFO lib; + + protected String connPath; + + protected FileInputStream in_pipe; + protected FileOutputStream out_pipe; + + + public EzIPC(String connPath) throws IOException + { + this.lib = LibraryNamedFIFO.INSTANCE; + this.connPath = connPath; + + if(lib.mkfifo(connPath+"_c2s", 0660) != 0) + if(Native.getLastError() != ErrNo.EEXIST) + throw new IOException("Could not create client->server pipe"); + + if(lib.mkfifo(connPath+"_s2c", 0660) != 0) + if(Native.getLastError() != ErrNo.EEXIST) + throw new IOException("Could not create server->client pipe"); + + this.in_pipe = null; + this.out_pipe = null; + } + + public abstract void open() throws IOException; + + public void close() throws IOException + { + if(in_pipe != null) in_pipe.close(); + if(out_pipe != null) out_pipe.close(); + } + + + // FIXME: check write() return val + public boolean send(byte[] data) throws IOException + { + if(out_pipe == null) return false; + if(data.length == 0) return true; + + // FIXME: JAVA DOES NOT HAVE FUCKING UNSIGNED BRO + ByteBuffer bb = ByteBuffer.allocate(8); + bb.order(ByteOrder.nativeOrder()); // Imagine not being native by def. + bb.putLong(data.length); + + out_pipe.write(bb.array()); + out_pipe.write(data); + + return true; + } + + // FIXME: check write() return val + public boolean receive(byte[] data) throws IOException + { + if(in_pipe == null) return false; + if(data.length == 0) return true; + + byte[] bytes = new byte[8]; + + // FIXME: yikes + while(in_pipe.available() < 8); + in_pipe.read(bytes); + + ByteBuffer bb = ByteBuffer.wrap(bytes); + bb.order(ByteOrder.nativeOrder()); // Imagine not being native by def. + + // FIXME: JAVA DOES NOT HAVE FUCKING UNSIGNED BRO + long in_size = bb.getLong(); + long read_size = in_size > data.length ? data.length : in_size; + + // FIXME: yikes + while(in_pipe.available() < read_size); + long read = in_pipe.read(data); + + // truncate + for(long i = 0; i < (in_size - read_size); i++) in_pipe.read(); + + return true; + } +} |