public abstract class VariableLengthPacketWorker extends AbstractPacketWorker
AbstractPacketWorker
to support variable length
packets. For the default implementation, packets should extend
PacketIF
and have a 3-byte header: 1
byte for packet designation and 2 bytes for message length. This results in
255 packet types, with maximum packet length of 32,767 bytes if using java
shorts or 65,535 bytes if using an unsigned length implementation. NOTE:
packet length excludes the 3-byte header, i.e. is payload length. If more
packet types or a larger packet (payload) size is needed, use the argument
constructor. Note that the largest header size the constructor can take is 2
bytes, and the length variable can either be 2 or 4 bytes, representing java
shorts and ints respectively.
Application code should extend the singular method
assemblePacket(ch.dermitza.securenio.socket.SocketIF, short, byte[])
to assemble the reconstructed packet and subsequently
AbstractPacketWorker.fireListeners(SocketIF, ch.dermitza.securenio.packet.PacketIF)
with
the reconstructed packet.
Modifier and Type | Field and Description |
---|---|
static int |
HEADER_LENGTH
The default header length
|
static int |
SIZE_LENGTH
The default packet size length
|
pendingData, pendingSockets
Constructor and Description |
---|
VariableLengthPacketWorker()
Initializes this packet worker to work with packets having a 1 byte
header and 2 bytes payload length.
|
VariableLengthPacketWorker(boolean singleByte,
boolean shortSize)
Initializes this packet worker to work with packets having a custom
header and payload length.
|
Modifier and Type | Method and Description |
---|---|
protected abstract void |
assemblePacket(SocketIF socket,
short head,
byte[] data)
This method is called from the packet worker when a complete packet has
been received.
|
protected void |
processData()
Process data received on pending sockets, in a FIFO fashion.
|
addData, addListener, fireListeners, isRunning, removeListener, run, setRunning
public static final int HEADER_LENGTH
public static final int SIZE_LENGTH
public VariableLengthPacketWorker(boolean singleByte, boolean shortSize)
singleByte
- true if the header is one byte, false if it is 2 bytes
longshortSize
- true if the payload length is 2 bytes, false if it is 4
bytes longpublic VariableLengthPacketWorker()
protected void processData()
processData
in class AbstractPacketWorker
protected abstract void assemblePacket(SocketIF socket, short head, byte[] data)
socket
- The socket on which a complete packet was receivedhead
- The head of the packetdata
- The packet's payloadAbstractPacketWorker.fireListeners(SocketIF, ch.dermitza.securenio.packet.PacketIF)