views:

525

answers:

2

I'm working on an online game and I've hit a little snag while working on the server side of things.

When using nonblocking sockets in Java, what is the best course of action to handle complete packet data sets that cannot be processed until all the data is available? For example, sending a large 2D tiled map over a socket.

I can think of two ways to handle it:

  1. Allocate the ByteBuffer large enough to handle the complete data set needed to process a large 2D tiled map from my example. Continue add read data to the buffer until it's all been received and process from there.

  2. If the ByteBuffer is a smaller size (perhaps 1500), subsequent reads can be done and put out to a file until it can be processed completely from the file. This would prevent having to have large ByteBuffers, but degrades performance because of disk I/O.

I'm using a dedicated ByteBuffer for every SocketChannel so that I can keep reading in data until it's complete for processing. The problem is if my 2D Tiled Map amounts to 2MB in size, is it really wise to use 1000 2MB ByteBuffers (assuming 1000 is a client connection limit and they are all in use)? There must be a better way that I'm not thinking of.

I'd prefer to keep things simple, but I'm open to any suggestions and appreciate the help. Thanks!

+1  A: 

Probably, the best solution for now is to use the full 2MB ByteBuffer and let the OS take care of paging to disk (virtual memory) if that's necessary. You probably won't have 1000 concurrent users right away, and when you do, you can optimize. You may be surprised what your real performance issues are.

Matthew Flaschen
Is it acceptable to adjust the Java heap size to such a large size? Even 100 connections would easily blow away it's default limit, and my server will crash with an OutOfMemory exception.
Are you really getting 100 concurrent connections? If so, perhaps a better solution is to optimize your tile map format/protocol so it is more concise. Otherwise, why not experiment with increasing the heap size?
Matthew Flaschen
A: 

I decided the best course of action was to simply reduce the size of my massive dataset and send tile updates instead of an entire map update. That way I can simply send a list of tiles that have changed on a map instead of the entire map over again. This reduces the need for such a large buffer and I'm back on track. Thanks.