Package com.germancoding.packetapi
Class Packet
- java.lang.Object
-
- com.germancoding.packetapi.Packet
-
- Direct Known Subclasses:
ClosePacket
,HandshakePacket
,KeepAlivePacket
public abstract class Packet extends java.lang.Object
Represents a data packet that can be send and received over a network.
Note: Each subclass of this class has to define a nullary constructor (Constructor with no arguments)
Also, every class extendingPacket
should be careful withnull
. When accessing local attributes, it is recommended to check whether they are null, because the object may be created by this library just before.
A packet can be registered at thePacketHandler
class- Author:
- Max/Nummer378/GermanCoding
- See Also:
PacketHandler
-
-
Constructor Summary
Constructors Constructor Description Packet()
A nullary constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract short
getId()
abstract void
handle(java.io.DataInputStream in)
Called by theDataReader
when a packet with this id is read.abstract boolean
isCritical()
Defines whether this packet type is critical.
Critical means that the handle() function should never fail.abstract PacketWriter
prepare()
Called by theDataSender
when this packet is send over the network.
-
-
-
Method Detail
-
getId
public abstract short getId()
- Returns:
- The id of this packet. Should always return the same value. Note: Please use only positive numbers
-
handle
public abstract void handle(java.io.DataInputStream in) throws java.io.IOException
Called by theDataReader
when a packet with this id is read. This method/function should read data and store the values in his own attributes.
Example:
this.message = in.readUTF();
This method is connected with theprepare()
function. All data that is written in theprepare()
function should be read in this method in the same order.- Parameters:
in
- A DataInputStream, provided by theDataReader
containing all the data (if nothing failed) that is needed to handle this packet.
The DataInputStream has a fixed length so you can't read more bytes thanin.available()
to avoid issues with other packets. Ifin
has not enough bytes to fill this packet with data, simply throw an IOException. The DataInputStream will also throw a EOFException when you try to read more bytes thanin.available()
- Throws:
java.io.IOException
- If reading fails, e.g there are not enough bytes or the bytes are wrong encoded (e.g you want to read UTF but there is no UTF-String encoded in bytes)- See Also:
prepare()
-
prepare
public abstract PacketWriter prepare() throws java.io.IOException
Called by theDataSender
when this packet is send over the network. This method/function should write data that is stored in his own attributes into aPacketWriter
. You have to create a new PacketWriter instance for that.
Example:
PacketWriter writer = new PacketWriter(getId());
writer.writeUTF(this.message);
return writer;
This method is connected with thehandle()
function. All data that is read in thehandle()
function should be written in this method in the same order.- Returns:
- A new
PacketWriter
instance where all the data of this packet is stored - Throws:
java.io.IOException
- If a PacketWriter call fails. Should never happen (since thePacketWriter
only stores data in the memory)- See Also:
handle(DataInputStream)
-
isCritical
public abstract boolean isCritical()
Defines whether this packet type is critical.
Critical means that the handle() function should never fail. If it fails though the connection will be terminated
Note: If aprepare()
call fails the connection is also terminated (and marked as failed) no matter if the packet is critical or not- Returns:
- Whether this packet type is a critical packet that has to be received properly or not.
-
-