Class CommandAPIMessenger<InputChannel, OutputChannel>

java.lang.Object
dev.jorel.commandapi.network.CommandAPIMessenger<InputChannel, OutputChannel>
Type Parameters:
InputChannel - The type of objects that might send a packet to this plugin
OutputChannel - The type of objects this plugin might send messages to

public abstract class CommandAPIMessenger<InputChannel, OutputChannel> extends Object
Handles sending and receiving CommandAPIPackets between instances of the CommandAPI plugin on different servers. This class should be extended on each platform to hook into its specific plugin-messaging scheme.
  • Constructor Details

  • Method Details

    • close

      public abstract void close()
      Disables this messenger, unregistering any events and such that let it send/receive packets before the plugin disables.
    • getConnectionProtocolVersion

      public abstract int getConnectionProtocolVersion(OutputChannel target)
      Gets the CommandAPIProtocol.PROTOCOL_VERSION on the other side of an outgoing connection. This is determined by the value in the SetVersionPacket sent by this connection, or 0 if they never sent that packet.
      Parameters:
      target - The OutputChannel to get the version for.
      Returns:
      The CommandAPIProtocol.PROTOCOL_VERSION supported by this target.
    • messageReceived

      protected void messageReceived(CommandAPIProtocol protocol, InputChannel sender, byte[] input)
      Handles a message sent to this plugin. The given byte array will be decoded into a CommandAPIPacket according to the given CommandAPIProtocol, then handled appropriately. Nothing happens if the given byte array is empty.
      Parameters:
      protocol - The protocol to handle the message with.
      sender - The source of the message.
      input - The byte array of the message.
      Throws:
      IllegalStateException - if the packet is malformed and cannot be read correctly.
    • sendPacket

      public void sendPacket(OutputChannel target, CommandAPIPacket packet)
      Sends a packet to the given target. The given packet will be converted into a byte array and sent using sendRawBytes(CommandAPIProtocol, Object, byte[]). The packet must be registered in CommandAPIProtocol to be sent.
      Parameters:
      target - The connection to send the packet to.
      packet - The CommandAPIPacket to send.
      Throws:
      IllegalStateException - if the given packet is not registered in CommandAPIProtocol.
    • sendRawBytes

      protected abstract void sendRawBytes(CommandAPIProtocol protocol, OutputChannel target, byte[] bytes)
      Sends an array of bytes to the given target.
      Parameters:
      protocol - The CommandAPIProtocol to use to send this packet.
      target - The connection to send this packet to.
      bytes - The array of bytes to send.
    • handlePacketException

      protected abstract void handlePacketException(RuntimeException exception, InputChannel source)
      Called when there is an exception while sending or receiving a packet. May rethrow the exception or log it in another manner.
      Parameters:
      exception - The exception that was thrown.
      source - The connection that sent the packet which caused the exception during receiving. This parameter will be null if an exception happens while sending.