code-disaster / steamworks4j

A thin Java wrapper to access the Steamworks API
https://code-disaster.github.io/steamworks4j/
MIT License
468 stars 64 forks source link

sendLobbyChatMsg does not trigger onLobbyChatMessage #106

Closed andy98725 closed 2 years ago

andy98725 commented 2 years ago

EXPECTED Calling SteamMatchmaking.sendLobbyChatMsg would trigger SteamMatchmakingCallback.onLobbyChatMessage callbacks on other users in the same lobby. RESULT sendLobbyChatMsg seems to have no effect.

I've reproduced the issue and put the minimal code at this repo.

code-disaster commented 2 years ago

The issue is caused by your ByteBuffer in sendMessage().

In general, the Steam wrapper does respect (read) position() and remaining() markers of buffers you pass, but doesn't modify them. bb.put(data) moves both. This means the moment you call sendLobbyChatMsg(), it tries to send 0 bytes starting at the end of the actual message data.

To fix this, you could change the code to this, for example:

bb.put(data);
bb.position(0);
steam.mm.get().sendLobbyChatMsg(lobby, bb);

Note: I'd also create and reuse one ByteBuffer instance, instead of creating one with each call. Direct byte buffers can hang around for a very long time until the GC decides to collect them and free their native heap memory.

In theory the same issue arises in receiveMessage(), but the follow-up copy into the byte[] array doesn't care about those markers either, so it doesn't matter - for now.

andy98725 commented 2 years ago

Thanks for the help! That was it :)