one more principal question:
I need quite often to broadcast exactly same message to many connected clients. So “intuitive” solution is to create ByteBuffer, populate it with data - and write it to several SocketChannel-s. Of course it will not work, because each SocketChannel::write will change buffer’s read-position to unpredictable value. (Also it may happen that SocketChannel::write has to be called several times, before buffer’s whole content is sent)
Second solution is: simply create as many buffers as necessary, and fill them all with same data. No problem, but it is sure very ineffective.
Would it be possible to have just one ByteBuffer shared by many Channels, and remember/restore current write position according to current SocketChannel? Isn’t it too crazy idea? Roughly something like:
class Connection {
private SocketChannel channel;
private ByteBuffer buffer; // shared by more Connection-s
private pos;
public send() {
buffer.position(pos);
channel.write(buffer);
pos = buffer.position();
}
}
Or have do you broadcast same messages?