Class DatagramChannelProvider

java.lang.Object
org.omegazero.net.nio.socket.provider.DatagramChannelProvider
All Implemented Interfaces:
ChannelProvider

public class DatagramChannelProvider extends Object implements ChannelProvider
A ChannelProvider for DatagramChannels.

DatagramChannelProvider for incoming connections on servers need to pass incoming data to addReadData(byte[]).

API Note:
Before version 2.1.0, this class was in package org.omegazero.net.socket.provider.
  • Constructor Details

    • DatagramChannelProvider

      public DatagramChannelProvider()
      This constructor should be used if the datagram channel is used as a client which is about to be connected to a server.
    • DatagramChannelProvider

      public DatagramChannelProvider(SocketAddress remote, Consumer<ChannelConnection> notifyWriteBacklog)
      This constructor should be used if the datagram channel is used as a server.
      Parameters:
      remote - The address of the client
      notifyWriteBacklog - Callback when the channel is experiencing write backlog
  • Method Details

    • addReadData

      public void addReadData(byte[] data)
      Used for incoming connections on servers. Data received on the server datagram socket must be passed to this method on the appropriate connection instance.
      Parameters:
      data - The data
    • init

      public void init(ChannelConnection connection, SelectionKey key)
      Description copied from interface: ChannelProvider
      Initializes this ChannelProvider.
      Specified by:
      init in interface ChannelProvider
      Parameters:
      connection - The ChannelConnection this ChannelProvider is for
      key - The selection key of the connection
    • connect

      public boolean connect(SocketAddress remote, int timeout) throws IOException
      Description copied from interface: ChannelProvider
      Connects the underlying java.nio channel to the given remote address.
      Specified by:
      connect in interface ChannelProvider
      Parameters:
      remote - The remote address
      timeout - If applicable, the connection attempt will cancel after this time in milliseconds
      Returns:
      true if the channel connected immediately
      Throws:
      IOException - If an IO error occurs
    • close

      public void close() throws IOException
      Description copied from interface: ChannelProvider
      Closes the underlying java.nio channel.
      Specified by:
      close in interface ChannelProvider
      Throws:
      IOException - If an IO error occurs
    • read

      public int read(ByteBuffer buf) throws IOException
      Description copied from interface: ChannelProvider
      Reads data from the underlying java.nio channel into the given buffer.
      Specified by:
      read in interface ChannelProvider
      Parameters:
      buf - The buffer to write data to
      Returns:
      The number of bytes read
      Throws:
      IOException - If an IO error occurs
    • write

      public int write(ByteBuffer buf) throws IOException
      Description copied from interface: ChannelProvider
      Writes data from the given buffer to the underlying java.nio channel.
      Specified by:
      write in interface ChannelProvider
      Parameters:
      buf - The buffer to read from
      Returns:
      The number of bytes written
      Throws:
      IOException - If an IO error occurs
    • writeBacklogStarted

      public void writeBacklogStarted()
      Description copied from interface: ChannelProvider
      Called by the ChannelConnection to indicate write backlog has started because the outgoing socket buffer is full.
      Specified by:
      writeBacklogStarted in interface ChannelProvider
    • writeBacklogEnded

      public void writeBacklogEnded()
      Description copied from interface: ChannelProvider
      Called by the ChannelConnection to indicate write backlog has ended.
      Specified by:
      writeBacklogEnded in interface ChannelProvider
    • setReadBlock

      public void setReadBlock(boolean block)
      Description copied from interface: ChannelProvider
      If given true, the underlying selection key should stop listening for read ops.
      Specified by:
      setReadBlock in interface ChannelProvider
      Parameters:
      block - Whether to block read events
    • isAvailable

      public boolean isAvailable()
      Description copied from interface: ChannelProvider
      Returns true if the underlying java.nio channel is connected.
      Specified by:
      isAvailable in interface ChannelProvider
      Returns:
      true if the channel is connected
    • getSelectionKey

      public SelectionKey getSelectionKey()
      Description copied from interface: ChannelProvider
      Returns the SelectionKey of this ChannelProvider.
      Specified by:
      getSelectionKey in interface ChannelProvider
      Returns:
      The SelectionKey