Package arc.net

Class Connection

java.lang.Object
arc.net.Connection
Direct Known Subclasses:
Client

public class Connection extends Object
Represents a TCP and optionally a UDP connection between a Client and a Server. If either underlying connection is closed or errors, both connections are closed.
  • Constructor Details

    • Connection

      protected Connection()
  • Method Details

    • getID

      public int getID()
      Returns the server assigned ID. Will return -1 if this connection has never been connected or the last assigned ID if this connection has been disconnected.
    • isConnected

      public boolean isConnected()
      Returns true if this connection is connected to the remote end. Note that a connection can become disconnected at any time.
    • getLastProtocolError

      public ArcNetException getLastProtocolError()
      Returns the last protocol error that occured on the connection.
      Returns:
      The last protocol error or null if none error occured.
    • sendTCP

      public int sendTCP(Object object)
      Sends the object over the network using TCP.
      Returns:
      The number of bytes sent.
    • sendUDP

      public int sendUDP(Object object)
      Sends the object over the network using UDP.
      Returns:
      The number of bytes sent.
      Throws:
      IllegalStateException - if this connection was not opened with both TCP and UDP.
    • close

      public void close(DcReason reason)
    • updateReturnTripTime

      public void updateReturnTripTime()
      Requests the connection to communicate with the remote computer to determine a new value for the return trip time. When the connection receives a FrameworkMessage.Ping object with isReply set to true, the new return trip time is available.
    • getReturnTripTime

      public int getReturnTripTime()
      Returns the last calculated TCP return trip time, or -1 if updateReturnTripTime() has never been called or the FrameworkMessage.Ping response has not yet been received.
    • setKeepAliveTCP

      public void setKeepAliveTCP(int keepAliveMillis)
      An empty object will be sent if the TCP connection has not sent an object within the specified milliseconds. Periodically sending a keep alive ensures that an abnormal close is detected in a reasonable amount of time (see setTimeout(int) ). Also, some network hardware will close a TCP connection that ceases to transmit for a period of time (typically 1+ minutes). Set to zero to disable. Defaults to 8000.
    • setTimeout

      public void setTimeout(int timeoutMillis)
      If the specified amount of time passes without receiving an object over TCP, the connection is considered closed. When a TCP socket is closed normally, the remote end is notified immediately and this timeout is not needed. However, if a socket is closed abnormally (eg, power loss), ArcNet uses this timeout to detect the problem. The timeout should be set higher than the TCP keep alive for the remote end of the connection. The keep alive ensures that the remote end of the connection will be constantly sending objects, and setting the timeout higher than the keep alive allows for network latency. Set to zero to disable. Defaults to 12000.
    • addListener

      public void addListener(NetListener listener)
      Adds a listener to the connection. If the listener already exists, it is not added again.
      Parameters:
      listener - The listener to add.
    • removeListener

      public void removeListener(NetListener listener)
    • getEndPoint

      public EndPoint getEndPoint()
      Returns the local Client or Server to which this connection belongs.
    • getRemoteAddressTCP

      public InetSocketAddress getRemoteAddressTCP()
      Returns the IP address and port of the remote end of the TCP connection, or null if this connection is not connected.
    • getRemoteAddressUDP

      public InetSocketAddress getRemoteAddressUDP()
      Returns the IP address and port of the remote end of the UDP connection, or null if this connection is not connected.
    • setName

      public void setName(String name)
      Sets the friendly name of this connection. This is returned by toString() and is useful for providing application specific identifying information in the logging. May be null for the default name of "Connection X", where X is the connection ID.
    • getTcpWriteBufferSize

      public int getTcpWriteBufferSize()
      Returns the number of bytes that are waiting to be written to the TCP socket, if any.
    • isIdle

      public boolean isIdle()
      See Also:
    • setIdleThreshold

      public void setIdleThreshold(float idleThreshold)
      If the percent of the TCP write buffer that is filled is less than the specified threshold, NetListener.idle(Connection) will be called for each network thread update. Default is 0.1.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getArbitraryData

      public Object getArbitraryData()
    • setArbitraryData

      public void setArbitraryData(Object arbitraryData)