mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-20 11:33:06 +00:00
Use a single byte buffer to serialize input packets
This commit is contained in:
parent
330f40cc18
commit
c9d003ca6d
@ -69,13 +69,9 @@ public class ControllerPacket extends InputPacket {
|
||||
this.rightStickY = rightStickY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder getPayloadByteOrder() {
|
||||
return ByteOrder.LITTLE_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toWirePayload(ByteBuffer bb) {
|
||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||
bb.put(HEADER);
|
||||
bb.putShort(buttonFlags);
|
||||
bb.put(leftTrigger);
|
||||
|
@ -35,8 +35,7 @@ public class ControllerStream {
|
||||
private Thread inputThread;
|
||||
private LinkedBlockingQueue<InputPacket> inputQueue = new LinkedBlockingQueue<InputPacket>();
|
||||
|
||||
private ByteBuffer littleEndianBuffer = ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN);
|
||||
private ByteBuffer bigEndianBuffer = ByteBuffer.allocate(128).order(ByteOrder.BIG_ENDIAN);
|
||||
private ByteBuffer stagingBuffer = ByteBuffer.allocate(128);
|
||||
private ByteBuffer sendBuffer = ByteBuffer.allocate(128).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
public ControllerStream(InetAddress host, SecretKey riKey, int riKeyId, NvConnectionListener listener)
|
||||
@ -234,18 +233,8 @@ public class ControllerStream {
|
||||
}
|
||||
|
||||
private void sendPacket(InputPacket packet) throws IOException {
|
||||
ByteBuffer toWireBuffer;
|
||||
|
||||
// Choose which byte order we'll be using for converting to wire form
|
||||
if (packet.getPayloadByteOrder() == ByteOrder.BIG_ENDIAN) {
|
||||
toWireBuffer = bigEndianBuffer;
|
||||
}
|
||||
else {
|
||||
toWireBuffer = littleEndianBuffer;
|
||||
}
|
||||
|
||||
// Store the packet in wire form in the byte buffer
|
||||
packet.toWire(toWireBuffer);
|
||||
packet.toWire(stagingBuffer);
|
||||
int packetLen = packet.getPacketLength();
|
||||
|
||||
// Pad to 16 byte chunks
|
||||
@ -255,7 +244,7 @@ public class ControllerStream {
|
||||
sendBuffer.rewind();
|
||||
sendBuffer.putInt(paddedLength);
|
||||
try {
|
||||
encryptAesInputData(toWireBuffer.array(), packetLen, sendBuffer.array(), 4);
|
||||
encryptAesInputData(stagingBuffer.array(), packetLen, sendBuffer.array(), 4);
|
||||
} catch (Exception e) {
|
||||
// Should never happen
|
||||
e.printStackTrace();
|
||||
|
@ -13,20 +13,14 @@ public abstract class InputPacket {
|
||||
this.packetType = packetType;
|
||||
}
|
||||
|
||||
public abstract ByteOrder getPayloadByteOrder();
|
||||
|
||||
public abstract void toWirePayload(ByteBuffer bb);
|
||||
|
||||
public abstract int getPacketLength();
|
||||
|
||||
public void toWireHeader(ByteBuffer bb)
|
||||
{
|
||||
// We don't use putInt() here because it will be subject to the byte order
|
||||
// of the byte buffer. We just write it as a big endian int.
|
||||
bb.put((byte)(packetType >> 24));
|
||||
bb.put((byte)(packetType >> 16));
|
||||
bb.put((byte)(packetType >> 8));
|
||||
bb.put((byte)(packetType & 0xFF));
|
||||
bb.order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(packetType);
|
||||
}
|
||||
|
||||
public void toWire(ByteBuffer bb)
|
||||
|
@ -25,13 +25,9 @@ public class KeyboardPacket extends InputPacket {
|
||||
this.modifier = modifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder getPayloadByteOrder() {
|
||||
return ByteOrder.LITTLE_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toWirePayload(ByteBuffer bb) {
|
||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||
bb.put(keyDirection);
|
||||
bb.putShort((short)0);
|
||||
bb.putShort((short)0);
|
||||
|
@ -30,13 +30,9 @@ public class MouseButtonPacket extends InputPacket {
|
||||
PRESS_EVENT : RELEASE_EVENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder getPayloadByteOrder() {
|
||||
return ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toWirePayload(ByteBuffer bb) {
|
||||
bb.order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put(buttonEventType);
|
||||
bb.putInt(mouseButton);
|
||||
}
|
||||
|
@ -29,13 +29,9 @@ public class MouseMovePacket extends InputPacket {
|
||||
this.deltaY = deltaY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder getPayloadByteOrder() {
|
||||
return ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toWirePayload(ByteBuffer bb) {
|
||||
bb.order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put(HEADER);
|
||||
bb.putShort(deltaX);
|
||||
bb.putShort(deltaY);
|
||||
|
@ -17,13 +17,10 @@ public class MouseScrollPacket extends InputPacket {
|
||||
this.scroll = (short)(scrollClicks * 120);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder getPayloadByteOrder() {
|
||||
return ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toWirePayload(ByteBuffer bb) {
|
||||
bb.order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
bb.put((byte) 0x09);
|
||||
bb.put((byte) 0);
|
||||
bb.put((byte) 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user