naga.packetwriter
public class RegularPacketWriter extends java.lang.Object implements PacketWriter
[header 1-4 bytes] => content size
[content] => 0-255/0-65535/0-16777215/0-2147483646
Note that the maximum size for 4 bytes is a signed 32 bit int, not unsigned.
The packet writer will not validate outgoing packets, so make sure that the packet content size will fit in the header. I.e. make sure that if you have a 1 byte header, you do not send packets larger than 255 bytes, if two bytes, larger than 65535 and so on.
Constructor and Description |
---|
RegularPacketWriter(int headerSize,
boolean bigEndian)
Creates a regular packet writer with the given header size.
|
public RegularPacketWriter(int headerSize, boolean bigEndian)
headerSize
- the header size, 1 - 4 bytes.bigEndian
- big endian (largest byte first) or little endian (smallest byte first)public java.nio.ByteBuffer getBuffer()
PacketWriter
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 PacketWriter.isEmpty()
returns false.
getBuffer
in interface PacketWriter
public boolean isEmpty()
PacketWriter
Classes will never invoke PacketWriter.setPacket(byte[])
unless
isEmpty
returns true.
isEmpty
in interface PacketWriter
public void setPacket(byte[] bytes)
PacketWriter
setPacket
in interface PacketWriter
bytes
- an array of bytes representing the next packet.