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 intgetHandshakeID()shortgetId()intgetProtocolVersion()voidhandle(java.io.DataInputStream in)Called by theDataReaderwhen a packet with this id is read.booleanisCritical()Defines whether this packet type is critical.
Critical means that the handle() function should never fail.PacketWriterprepare()Called by theDataSenderwhen this packet is send over the network.voidsetHandshakeID(int handshakeID)voidsetProtocolVersion(int protocolVersion)
-
-
-
Method Detail
-
handle
public void handle(java.io.DataInputStream in) throws java.io.IOExceptionDescription copied from class:PacketCalled by theDataReaderwhen 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:
handlein classPacket- Parameters:
in- A DataInputStream, provided by theDataReadercontaining 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. Ifinhas 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:PacketCalled by theDataSenderwhen 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:
preparein classPacket- Returns:
- A new
PacketWriterinstance where all the data of this packet is stored - Throws:
java.io.IOException- If a PacketWriter call fails. Should never happen (since thePacketWriteronly 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:PacketDefines 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:
isCriticalin classPacket- Returns:
- Whether this packet type is a critical packet that has to be received properly or not.
-
-