naga
public interface PacketWriter
The method setPacket(byte[])
initializes for writer for output of a new
packet. Implementing classes can assume that setPacket is never called unless
isEmpty()
returns true. The getBuffer()
method should return a ByteBuffer
containing
the next chunk of data to output on the socket.
The implementation is similar to:
while (!packetWriter.isEmpty()) channel.write(packetWriter.getBuffer());
In other words, it is ok to split a single packet into several byte buffers each are handed in
turn for every call to getBuffer()
. (See RegularPacketWriter
source code for an example.)
void setPacket(byte[] bytes)
bytes
- an array of bytes representing the next packet.boolean isEmpty()
Classes will never invoke setPacket(byte[])
unless
isEmpty
returns true.
java.nio.ByteBuffer getBuffer()
Note that the socket does no rewinding or similar of the buffer,
the only way it interacts with the buffer is by calling
SocketChannel.write(ByteBuffer)
, so the implementing
class needs to make sure that the buffer is in the right state.
This code will not be called unless isEmpty()
returns false.