Class HandshakePacket
- java.lang.Object
-
- com.germancoding.packetapi.Packet
-
- com.germancoding.packetapi.defaultpackets.HandshakePacket
-
- All Implemented Interfaces:
DefaultPacket
public class HandshakePacket extends Packet implements DefaultPacket
-
-
Constructor Summary
Constructors Constructor Description HandshakePacket()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getHandshakeID()
short
getId()
int
getProtocolVersion()
void
handle(java.io.DataInputStream in)
Called by theDataReader
when a packet with this id is read.boolean
isCritical()
Defines whether this packet type is critical.
Critical means that the handle() function should never fail.PacketWriter
prepare()
Called by theDataSender
when this packet is send over the network.void
setHandshakeID(int handshakeID)
void
setProtocolVersion(int protocolVersion)
-
-
-
Method Detail
-
handle
public void handle(java.io.DataInputStream in) throws java.io.IOException
Description copied from class:Packet
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.- Specified by:
handle
in classPacket
- 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:
Packet.prepare()
-
prepare
public PacketWriter prepare() throws java.io.IOException
Description copied from class:Packet
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.- Specified by:
prepare
in classPacket
- 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:
Packet.handle(DataInputStream)
-
getHandshakeID
public int getHandshakeID()
-
setHandshakeID
public void setHandshakeID(int handshakeID)
-
getProtocolVersion
public int getProtocolVersion()
-
setProtocolVersion
public void setProtocolVersion(int protocolVersion)
-
getId
public short getId()
-
isCritical
public boolean isCritical()
Description copied from class:Packet
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- Specified by:
isCritical
in classPacket
- Returns:
- Whether this packet type is a critical packet that has to be received properly or not.
-
-