That’s why I asked whether you used PyroSelector.spawnNetworkThread()
or not.
Anyway, PyroNet is a really low-level API (barely higher than NIO itself) so it’s pretty hard to get it correct. Further, your code is trying to ‘workaround’ the async nature of the API by creating massive buffers when a byte is available, and using multiple buffers to hold the intermediate results.
As you see, the game is fairly simple, but your network code is already both inefficient, and apparently bugged.
As said earlier, I’d advise you to go back to a blocking I/O arch. using Data[Input/Output]Stream. All that code could be replaced with:
while(true)
{
int cmd = in.read();
int len = calcSize(cmd);
byte[] payload = new byte[len];
in.readFully(payload);
handlePacket(cmd, payload);
}
Not only is that code bug free, it is maintainable, more efficient (no pile of buffers) and thus much faster (Java blocking I/O is already a bit faster than NIO).
Network bugs are very hard to debug, so the goal is to make it simple. Do yourself a favor, focus on productivity, and ditch PyroNet.