Class 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 extending Packet should be careful with null. 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 the PacketHandler 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 the DataReader 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 the DataSender when this packet is send over the network.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Packet

        public Packet()
        A nullary constructor. Each subclass needs one (either by defining no constructor or by defining a nullary and a custom constructor) It is not required to call super() since this constructor does nothing.
    • 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 the DataReader 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 the prepare() function. All data that is written in the prepare() function should be read in this method in the same order.
        Parameters:
        in - A DataInputStream, provided by the DataReader 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 than in.available() to avoid issues with other packets. If in 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 than in.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 the DataSender when this packet is send over the network. This method/function should write data that is stored in his own attributes into a PacketWriter. 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 the handle() function. All data that is read in the handle() 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 the PacketWriter 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 a prepare() 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.