Skip to content

Commit

Permalink
refactor: rewrite using byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 5, 2024
1 parent ae993dd commit 777c3d8
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 206 deletions.
242 changes: 95 additions & 147 deletions app/src/main/java/xtr/keymapper/server/RemoteServiceSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.os.Parcel;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.function.Consumer;

import xtr.keymapper.IRemoteService;
import xtr.keymapper.IRemoteServiceCallback;
Expand All @@ -14,43 +17,76 @@

public class RemoteServiceSocketClient implements IRemoteService {

private final LocalSocket socket;
// Socket should stay alive
private static LocalSocket socket;

public RemoteServiceSocketClient() {
socket = new LocalSocket();
try {
socket.connect(new LocalSocketAddress("xtmapper-a3e11694"));
} catch (IOException e) {
throw new RuntimeException(e);
if (socket == null) {
socket = new LocalSocket();
try {
socket.connect(new LocalSocketAddress("xtmapper-a3e11694"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static class ParcelableByteArray {
final byte[][] data;
int i = 0;

public ParcelableByteArray(int size) {
data = new byte[size][];
}

public void foreach(Consumer<byte []> action) {
for (byte[] bytes : data) action.accept(bytes);
}

static private <T extends android.os.Parcelable> void writeTypedObject(
android.os.Parcel parcel, T value, int parcelableFlags) {
if (value != null) {
parcel.writeInt(1);
value.writeToParcel(parcel, parcelableFlags);
} else {
parcel.writeInt(0);
private <T extends android.os.Parcelable> void writeTypedObject(T value) {
Parcel parcel = Parcel.obtain();
value.writeToParcel(parcel, 0);
data[i] = parcel.marshall();
parcel.recycle();
i++;
}

public void writeStrongInterface(Object o) {
data[i] = new byte[0];
i++;
}

public void writeInt(int x) {
data[i] = ByteBuffer.allocate(4).putInt(x).array();
i++;
}
}

private boolean transactRemote(int code, Parcel data, Parcel reply, int flags) {
public int readInt() {
int i;
try {
byte[] b = data.marshall();
byte[] r = reply.marshall();
socket.getOutputStream().write(code);
socket.getOutputStream().write(b.length);
socket.getOutputStream().write(b);
socket.getOutputStream().write(r.length);
socket.getOutputStream().write(r);
InputStream inputStream = socket.getInputStream();
int length = inputStream.read();
byte[] bytes = new byte[length];
inputStream.read(bytes);
i = ByteBuffer.wrap(bytes).getInt();
} catch (IOException e) {
throw new RuntimeException(e);
}
return i;
}

int length = socket.getInputStream().read();
r = new byte[length];
socket.getInputStream().read(r);
reply.unmarshall(r, 0, length);
reply.setDataPosition(0);
private boolean transactRemote(int code, ParcelableByteArray data) {
try {
socket.getOutputStream().write(code);

data.foreach(bytes -> {
try {
socket.getOutputStream().write(bytes.length);
socket.getOutputStream().write(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -59,147 +95,59 @@ private boolean transactRemote(int code, Parcel data, Parcel reply, int flags) {

@Override public boolean isRoot()
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
boolean _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = transactRemote(TRANSACTION_isRoot, _data, _reply, 0);
_reply.readException();
_result = (0!=_reply.readInt());
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
boolean _status = transactRemote(TRANSACTION_isRoot, _data);

boolean _result = (0!=readInt());

return _result;
}

@Override public void startServer(KeymapProfile profile, KeymapConfig keymapConfig, IRemoteServiceCallback cb, int screenWidth, int screenHeight) {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
writeTypedObject(_data, profile, 0);
writeTypedObject(_data, keymapConfig, 0);
_data.writeStrongInterface(cb);
_data.writeInt(screenWidth);
_data.writeInt(screenHeight);
boolean _status = transactRemote(TRANSACTION_startServer, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
_data.writeTypedObject(profile);
_data.writeTypedObject(keymapConfig);
_data.writeStrongInterface(null);
_data.writeInt(screenWidth);
_data.writeInt(screenHeight);
boolean _status = transactRemote(TRANSACTION_startServer, _data);
}
@Override public void stopServer() {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = transactRemote(TRANSACTION_stopServer, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
boolean _status = transactRemote(TRANSACTION_stopServer, _data);
}
@Override public void registerOnKeyEventListener(xtr.keymapper.OnKeyEventListener l) {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongInterface(l);
boolean _status = transactRemote(TRANSACTION_registerOnKeyEventListener, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
_data.writeStrongInterface(null);
boolean _status = transactRemote(TRANSACTION_registerOnKeyEventListener, _data);

}
@Override public void unregisterOnKeyEventListener(xtr.keymapper.OnKeyEventListener l) {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongInterface(l);
boolean _status = transactRemote(TRANSACTION_unregisterOnKeyEventListener, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
_data.writeStrongInterface(null);
boolean _status = transactRemote(TRANSACTION_unregisterOnKeyEventListener, _data);
}
@Override public void registerActivityObserver(xtr.keymapper.ActivityObserver callback) {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongInterface(callback);
boolean _status = transactRemote(TRANSACTION_registerActivityObserver, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
_data.writeStrongInterface(null);
boolean _status = transactRemote(TRANSACTION_registerActivityObserver, _data);
}
@Override public void unregisterActivityObserver(xtr.keymapper.ActivityObserver callback) {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongInterface(callback);
boolean _status = transactRemote(TRANSACTION_unregisterActivityObserver, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
_data.writeStrongInterface(null);
boolean _status = transactRemote(TRANSACTION_unregisterActivityObserver, _data);
}
@Override public void resumeMouse() {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = transactRemote(TRANSACTION_resumeMouse, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
boolean _status = transactRemote(TRANSACTION_resumeMouse, _data);
}
@Override public void pauseMouse() {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = transactRemote(TRANSACTION_pauseMouse, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
boolean _status = transactRemote(TRANSACTION_pauseMouse, _data);
}
@Override public void reloadKeymap() {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = transactRemote(TRANSACTION_reloadKeymap, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
ParcelableByteArray _data = new ParcelableByteArray(5);
boolean _status = transactRemote(TRANSACTION_reloadKeymap, _data);
}

static final int TRANSACTION_isRoot = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
Expand Down
Loading

0 comments on commit 777c3d8

Please sign in to comment.