Class TcpSocket

java.lang.Object
io.activej.reactor.AbstractNioReactive
io.activej.net.socket.tcp.TcpSocket
All Implemented Interfaces:
AsyncCloseable, ITcpSocket, io.activej.reactor.nio.NioChannelEventHandler, io.activej.reactor.NioReactive, io.activej.reactor.Reactive

public final class TcpSocket extends io.activej.reactor.AbstractNioReactive implements ITcpSocket, io.activej.reactor.nio.NioChannelEventHandler
  • Field Details

    • DEFAULT_READ_BUFFER_SIZE

      public static final int DEFAULT_READ_BUFFER_SIZE
    • NO_TIMEOUT

      public static final int NO_TIMEOUT
      See Also:
  • Method Details

    • wrapChannel

      public static TcpSocket wrapChannel(io.activej.reactor.nio.NioReactor reactor, SocketChannel socketChannel, InetSocketAddress remoteAddress, @Nullable @Nullable io.activej.reactor.net.SocketSettings socketSettings) throws IOException
      Throws:
      IOException
    • wrapChannel

      public static TcpSocket wrapChannel(io.activej.reactor.nio.NioReactor reactor, SocketChannel socketChannel, @Nullable @Nullable io.activej.reactor.net.SocketSettings socketSettings) throws IOException
      Throws:
      IOException
    • connect

      public static Promise<TcpSocket> connect(io.activej.reactor.nio.NioReactor reactor, InetSocketAddress address)
    • connect

      public static Promise<TcpSocket> connect(io.activej.reactor.nio.NioReactor reactor, InetSocketAddress address, @Nullable @Nullable Duration duration, @Nullable @Nullable io.activej.reactor.net.SocketSettings socketSettings)
    • connect

      public static Promise<TcpSocket> connect(io.activej.reactor.nio.NioReactor reactor, InetSocketAddress address, long timeout, @Nullable @Nullable io.activej.reactor.net.SocketSettings socketSettings)
    • setInspector

      public void setInspector(@Nullable @Nullable TcpSocket.Inspector inspector)
    • getConnectionCount

      public static int getConnectionCount()
    • getRemoteAddress

      public InetSocketAddress getRemoteAddress()
    • getUserData

      @Nullable public @Nullable Object getUserData()
    • setUserData

      public void setUserData(@Nullable @Nullable Object userData)
      Sets an arbitrary object as a user-defined context for this socket

      It may be used e.g. by socket inspector for collecting statistics per socket.

    • read

      public Promise<ByteBuf> read()
      Description copied from interface: ITcpSocket
      Operation to read some data from network. Returns a promise of a bytebuf that represents some data received from network.

      It is allowed to call read before previous read was completed. However, each consecutive call will cancel all the previous calls (they will not be completed).

      Specified by:
      read in interface ITcpSocket
      Returns:
      promise of ByteBuf that represents data received from network
    • onReadReady

      public void onReadReady()
      Specified by:
      onReadReady in interface io.activej.reactor.nio.NioChannelEventHandler
    • write

      public Promise<Void> write(@Nullable @Nullable ByteBuf buf)
      Description copied from interface: ITcpSocket
      Operation to write some data to network. Returns a promise of void that represents successful write.

      Many write operations may be called. However, when some write is successful, all the promises received from write calls before it will be completed at once.

      Specified by:
      write in interface ITcpSocket
      Parameters:
      buf - data to be sent to network
      Returns:
      promise that represents successful write operation
    • isReadAvailable

      public boolean isReadAvailable()
      Specified by:
      isReadAvailable in interface ITcpSocket
    • onWriteReady

      public void onWriteReady()
      Specified by:
      onWriteReady in interface io.activej.reactor.nio.NioChannelEventHandler
    • closeEx

      public void closeEx(Exception e)
      Specified by:
      closeEx in interface AsyncCloseable
    • isClosed

      public boolean isClosed()
      Specified by:
      isClosed in interface ITcpSocket
    • getSocketChannel

      @Nullable public @Nullable SocketChannel getSocketChannel()
    • toString

      public String toString()
      Overrides:
      toString in class Object