Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Fix frame drops in SteamVR/Windows #607

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase;
import com.sun.jna.platform.win32.WinError;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;
import dev.slimevr.Main;
import dev.slimevr.VRServer;
Expand All @@ -23,6 +24,8 @@ public class WindowsNamedPipeBridge extends SteamVRBridge {
protected final String pipeName;
private final byte[] buffArray = new byte[2048];
protected WindowsPipe pipe;
protected WinNT.HANDLE dataEvent = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
protected WinBase.OVERLAPPED overlapped = new WinBase.OVERLAPPED();

public WindowsNamedPipeBridge(
VRServer server,
Expand All @@ -34,6 +37,7 @@ public WindowsNamedPipeBridge(
) {
super(server, hmd, "Named pipe thread", bridgeName, bridgeSettingsKey, shareableTrackers);
this.pipeName = pipeName;
overlapped.hEvent = dataEvent;
}

@Override
Expand All @@ -54,10 +58,14 @@ public void run() {
resetPipe();
}
if (!pipesUpdated) {
try {
Thread.sleep(5); // Up to 200Hz
} catch (InterruptedException e) {
e.printStackTrace();
if (pipe.state == PipeState.OPEN) {
waitForData();
} else {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Expand Down Expand Up @@ -93,6 +101,14 @@ protected boolean sendMessageReal(ProtobufMessage message) {
return false;
}

private void waitForData() {
if (pipe.state != PipeState.OPEN)
return;
// async request for data, overlapped contains the event handle
Kernel32.INSTANCE.ReadFile(pipe.pipeHandle, null, 0, null, overlapped);
Kernel32.INSTANCE.WaitForSingleObject(dataEvent, 20);
}

private boolean updatePipe() throws IOException {
if (pipe.state == PipeState.OPEN) {
boolean readAnything = false;
Expand Down