steamworks4j icon indicating copy to clipboard operation
steamworks4j copied to clipboard

SteamNetworking.isP2PPacketAvailable generates error in native code

Open andy98725 opened this issue 3 years ago • 3 comments

In one use case, the mentioned function consistently errors out.

Here's my relevant code. Let me know if there's an alternative way I should be using the method.

private Thread startListener() {
	Thread listen = new Thread(new Runnable() {
		@Override
		public void run() {
			SteamNetworking net = steam.net.get();
			SteamID sender = new SteamID();
			int msgSize;

			try {
				while (!Thread.interrupted()) {
					try {
						while ((msgSize = net.isP2PPacketAvailable(0)) > 0) {
							if (msgSize > recBuffer.capacity())
							recBuffer = ByteBuffer.allocateDirect(msgSize);

							synchronized (recBuffer) {
									recBuffer.clear();
									net.readP2PPacket(sender, recBuffer, 0);
									byte[] bytes = new byte[msgSize];
									recBuffer.get(bytes);
									recieveMessage(FileUtils.bytesToObj(bytes), sender);
							}
						}
					} catch (Exception e) {
						Game.getLogger().errorLog(e, "Steam Game Listener");
					}
					Thread.sleep((long) (1000.0 / Game.FRAME_RATE));
				}
			} catch (InterruptedException e) {
			}
		}
	}, "Steam Game Listener");
	listen.setDaemon(true);
	listen.start();
	return listen;
}

Here's the error log, since I'm on Windows: hs_err_pid18664.log

andy98725 avatar Jan 12 '22 22:01 andy98725

I don't know why it would crash inside the isP2PPacketAvailable() function, but I agree that the Java API could do a better job here. I believe there's also a bug with readP2PPacket() - it should check the return value and limit() recBuffer accordingly afterwards. (The same bug exists in SteamNetworkingTest)

code-disaster avatar Jan 14 '22 09:01 code-disaster

Also, not sure what your steam.net.get() does, and how many of those listeners you create, but you may run into trouble here. Try to not share that SteamNetworking instance, but just create a local one.

code-disaster avatar Jan 14 '22 10:01 code-disaster

Ah, that line just lazy-loads the interface. There's only the one interface and it's disposed of properly, so no issues there.

I found the presence of the error strange due to the simplicity of the C method. I have no idea what could cause it. Doesn't help that the windows error reporting for the native code is borderline unreadable to me.

andy98725 avatar Jan 14 '22 15:01 andy98725