com.hazelcast.nio.tcp

## Class TcpIpConnection

- java.lang.Object
- - com.hazelcast.nio.tcp.TcpIpConnection

- All Implemented Interfaces:
    Connection

      public class TcpIpConnection
      extends Object
      implements Connection

  The Tcp/Ip implementation of the `Connection`.

  A `TcpIpConnection` is not responsible for reading or writing data to the socket; that is task of the `Channel`.

  See Also:
    `Networking`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TcpIpConnection(TcpIpEndpointManager endpointManager, ConnectionLifecycleListener lifecycleListener, int connectionId, com.hazelcast.internal.networking.Channel channel)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close(String reason, Throwable cause)` Closes this connection.
    `boolean` | `equals(Object o)`
    `com.hazelcast.internal.networking.Channel` | `getChannel()`
    `Throwable` | `getCloseCause()` Gets the cause this Connection was closed.
    `String` | `getCloseReason()` Gets the reason this Connection was closed.
    `int` | `getConnectionId()`
    `Address` | `getEndPoint()` Gets the `Address` of the other side of this Connection.
    `TcpIpEndpointManager` | `getEndpointManager()`
    `InetAddress` | `getInetAddress()` Returns remote address of this Connection.
    `int` | `getPort()` The remote port.
    `InetSocketAddress` | `getRemoteSocketAddress()` Returns the address of the endpoint this Connection is connected to, or `null` if it is unconnected.
    `ConnectionType` | `getType()` Returns the `ConnectionType` of this Connection.
    `int` | `hashCode()`
    `boolean` | `isAlive()` Checks if the Connection is alive.
    `boolean` | `isClient()` Checks if it is a client connection.
    `long` | `lastReadTimeMillis()` Returns the clock time in milliseconds of the most recent read using this connection.
    `long` | `lastWriteTimeMillis()` Returns the clock time in milliseconds of the most recent write using this connection.
    `boolean` | `setBinding()`
    `void` | `setEndPoint(Address endPoint)`
    `void` | `setErrorHandler(TcpIpConnectionErrorHandler errorHandler)`
    `void` | `setType(ConnectionType type)` Sets the type of the connection
    `String` | `toString()`
    `boolean` | `write(com.hazelcast.internal.networking.OutboundFrame frame)` Writes a outbound frame so it can be received by the other side of the connection.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### TcpIpConnection

          public TcpIpConnection(TcpIpEndpointManager endpointManager,
                                 ConnectionLifecycleListener lifecycleListener,
                                 int connectionId,
                                 com.hazelcast.internal.networking.Channel channel)

  - ### Method Detail

    - #### getChannel

          public com.hazelcast.internal.networking.Channel getChannel()

    - #### getType

          public ConnectionType getType()

      Description copied from interface: `Connection`
      Returns the `ConnectionType` of this Connection.
      Specified by:
        `getType` in interface `Connection`
      Returns:
        the ConnectionType. It could be that `null` is returned.

    - #### setType

          public void setType(ConnectionType type)

      Description copied from interface: `Connection`
      Sets the type of the connection
      Specified by:
        `setType` in interface `Connection`
      Parameters:
        `type` - to be set

    - #### getEndpointManager

          public TcpIpEndpointManager getEndpointManager()

      Specified by:
        `getEndpointManager` in interface `Connection`

    - #### getInetAddress

          public InetAddress getInetAddress()

      Description copied from interface: `Connection`
      Returns remote address of this Connection.
      Specified by:
        `getInetAddress` in interface `Connection`
      Returns:
        the remote address. The returned value could be `null` if the connection is not alive.

    - #### getPort

          public int getPort()

      Description copied from interface: `Connection`
      The remote port.

      todo: rename to getRemotePort? And do we need it because we already have getEndPoint which returns an address which includes port. It is only used in testing

      Specified by:
        `getPort` in interface `Connection`
      Returns:
        the remote port number to which this Connection is connected, or 0 if the socket is not connected yet.

    - #### getRemoteSocketAddress

          public InetSocketAddress getRemoteSocketAddress()

      Description copied from interface: `Connection`
      Returns the address of the endpoint this Connection is connected to, or `null` if it is unconnected.
      Specified by:
        `getRemoteSocketAddress` in interface `Connection`
      Returns:
        address of the endpoint.

        todo: do we really need this method because we have getInetAddress, InetSocketAddress and getEndPoint.

    - #### isAlive

          public boolean isAlive()

      Description copied from interface: `Connection`
      Checks if the Connection is alive.
      Specified by:
        `isAlive` in interface `Connection`
      Returns:
        true if alive, false otherwise.

    - #### lastWriteTimeMillis

          public long lastWriteTimeMillis()

      Description copied from interface: `Connection`
      Returns the clock time in milliseconds of the most recent write using this connection.
      Specified by:
        `lastWriteTimeMillis` in interface `Connection`
      Returns:
        the clock time of the most recent write.

    - #### lastReadTimeMillis

          public long lastReadTimeMillis()

      Description copied from interface: `Connection`
      Returns the clock time in milliseconds of the most recent read using this connection.
      Specified by:
        `lastReadTimeMillis` in interface `Connection`
      Returns:
        the clock time of the most recent read

    - #### getEndPoint

          public Address getEndPoint()

      Description copied from interface: `Connection`
      Gets the `Address` of the other side of this Connection.

      todo: rename to get remoteAddress?

      Specified by:
        `getEndPoint` in interface `Connection`
      Returns:
        the Address.

    - #### setEndPoint

          public void setEndPoint(Address endPoint)

    - #### setErrorHandler

          public void setErrorHandler(TcpIpConnectionErrorHandler errorHandler)

    - #### getConnectionId

          public int getConnectionId()

    - #### isClient

          public boolean isClient()

      Description copied from interface: `Connection`
      Checks if it is a client connection.
      Specified by:
        `isClient` in interface `Connection`
      Returns:
        true if client connection, false otherwise.

    - #### write

          public boolean write(com.hazelcast.internal.networking.OutboundFrame frame)

      Description copied from interface: `Connection`
      Writes a outbound frame so it can be received by the other side of the connection. No guarantees are made that the frame is going to be received on the other side.

      The frame could be stored in an internal queue before it actually is written, so this call does not need to be a synchronous call.

      Specified by:
        `write` in interface `Connection`
      Parameters:
        `frame` - the frame to write.
      Returns:
        false if the frame was not accepted to be written, e.g. because the Connection was not alive.

    - #### equals

          public boolean equals(Object o)

      Overrides:
        `equals` in class `Object`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### close

          public void close(String reason,
                            Throwable cause)

      Description copied from interface: `Connection`
      Closes this connection.

      Pending packets on this connection are discarded

      If the Connection is already closed, the call is ignored. So it can safely be called multiple times.

      Specified by:
        `close` in interface `Connection`
      Parameters:
        `reason` - the reason this connection is going to be closed. Is allowed to be null.
        `cause` - the Throwable responsible for closing this connection. Is allowed to be null.

    - #### setBinding

          public boolean setBinding()

    - #### getCloseCause

          public Throwable getCloseCause()

      Description copied from interface: `Connection`
      Gets the cause this Connection was closed. Can be null if no cause was given or if the connection is still active. It is purely meant for debugging to shed some light on why connections are closed. This method is thread-safe and can be called at any moment.
      Specified by:
        `getCloseCause` in interface `Connection`
      Returns:
        the cause of closing this connection.
      See Also:
        `()`, `Connection.close(String, Throwable)`

    - #### getCloseReason

          public String getCloseReason()

      Description copied from interface: `Connection`
      Gets the reason this Connection was closed. Can be null if no reason was given or if the connection is still active. It is purely meant for debugging to shed some light on why connections are closed. This method is thread-safe and can be called at any moment. If the connection is closed and no reason is available, it is very likely that the close cause does contain the reason of closing.
      Specified by:
        `getCloseReason` in interface `Connection`
      Returns:
        the reason this connection was closed.
      See Also:
        `Connection.getCloseCause()`, `Connection.close(String, Throwable)`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

Copyright © 2020. All rights reserved.