Compare commits

...
3 changed files with 20 additions and 21 deletions

No files matched your search

@@ -47,7 +47,7 @@ public class WindowsNamedPipe implements AutoCloseable {
private final WinBase.OVERLAPPED overlappedWrite = new WinBase.OVERLAPPED();
private final WinBase.OVERLAPPED overlappedRead = new WinBase.OVERLAPPED();
private final WinBase.OVERLAPPED overlappedWait = new WinBase.OVERLAPPED();
private final ByteBuffer buf = ByteBuffer.allocate(2048);
private final ByteBuffer buf = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN);
private final IntByReference bytesWritten = new IntByReference(0);
private final IntByReference bytesAvailable = new IntByReference(0);
private final IntByReference bytesRead = new IntByReference(0);
@@ -190,14 +190,19 @@ public class WindowsNamedPipe implements AutoCloseable {
}
// Send a message to the pipe.
public boolean sendBuffer(byte[] buffer, int len) {
public boolean sendBuffer(ByteBuffer buf) {
int len = buf.limit();
overlappedWrite.clear();
overlappedWrite.hEvent = writeEvent;
boolean immediate = k32
.WriteFile(handle, buffer, len, null, overlappedWrite);
.WriteFile(handle, buf.array(), len, null, overlappedWrite);
int err = k32.GetLastError();
if (!immediate && err != WinError.ERROR_IO_PENDING) {
setError("WriteFile failed: " + err);
if (err == WinError.ERROR_BROKEN_PIPE || err == WinError.ERROR_NO_DATA) {
setError("Pipe closed");
} else {
setError("WriteFile failed: " + err);
}
return false;
}
@@ -223,13 +228,10 @@ public class WindowsNamedPipe implements AutoCloseable {
}
boolean anyReceived = false;
buf.clear();
while (k32.PeekNamedPipe(handle, buf.array(), 4, null, bytesAvailable, null)) {
if (bytesAvailable.getValue() < 4) {
return anyReceived; // Wait for more data
}
buf.position(0);
buf.order(ByteOrder.LITTLE_ENDIAN);
int messageLength = buf.getInt();
if (messageLength > 1024) { // Overflow
setError("Pipe overflow. Message length: " + messageLength);
@@ -241,7 +243,6 @@ public class WindowsNamedPipe implements AutoCloseable {
overlappedRead.clear();
overlappedRead.hEvent = readEvent;
buf.position(0);
boolean immediate = k32
.ReadFile(handle, buf.array(), messageLength, null, overlappedRead);
int err = k32.GetLastError();
@@ -264,10 +265,9 @@ public class WindowsNamedPipe implements AutoCloseable {
return anyReceived;
}
buf.clear();
buf.position(4); // skip size
buf.limit(messageLength);
reader.onMessage(this, buf);
buf.clear();
anyReceived = true;
if (state != PipeState.OPEN)
@@ -13,6 +13,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
@@ -22,7 +23,7 @@ public class WindowsNamedPipeBridge extends SteamVRBridge
protected final String bridgeSettingsKey;
protected WindowsNamedPipe pipe = null;
protected WindowsNamedPipe.PipeConnection connection = null;
private final byte[] buf = new byte[2048];
private final ByteBuffer buf = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN);
public WindowsNamedPipeBridge(
VRServer server,
@@ -122,18 +123,16 @@ public class WindowsNamedPipeBridge extends SteamVRBridge
if (connection == null || connection.getState() != PipeState.OPEN) {
return false;
}
try {
int size = message.getSerializedSize();
CodedOutputStream os = CodedOutputStream.newInstance(buf, 4, size);
buf.clear();
int wrappedSize = message.getSerializedSize() + 4;
buf.limit(wrappedSize);
buf.putInt(wrappedSize);
CodedOutputStream os = CodedOutputStream.newInstance(buf);
message.writeTo(os);
size += 4;
buf[0] = (byte) (size & 0xFF);
buf[1] = (byte) ((size >> 8) & 0xFF);
buf[2] = (byte) ((size >> 16) & 0xFF);
buf[3] = (byte) ((size >> 24) & 0xFF);
return connection.sendBuffer(buf, size);
return connection.sendBuffer(buf);
} catch (IOException e) {
e.printStackTrace();
}
@@ -56,7 +56,7 @@ public class WindowsNamedPipeRpcConnection implements GenericConnection {
src.put(bytes);
src.flip();
synchronized (this) {
this.connection.sendBuffer(bytes.array(), bytes.remaining());
this.connection.sendBuffer(bytes);
}
}
}