com.esotericsoftware.kryonet
Class Connection

java.lang.Object
  extended by com.esotericsoftware.kryonet.Connection
Direct Known Subclasses:
Client

public class Connection
extends java.lang.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.

Author:
Nathan Sweet

Method Summary
 void addListener(Listener listener)
          If the listener already exists, it is not added again.
 void close()
           
 EndPoint getEndPoint()
          Returns the local Client or Server to which this connection belongs.
 int getID()
          Returns the server assigned ID.
 java.net.InetSocketAddress getRemoteAddressTCP()
          Returns the IP address and port of the remote end of the TCP connection, or null if this connection is not connected.
 java.net.InetSocketAddress getRemoteAddressUDP()
          Returns the IP address and port of the remote end of the UDP connection, or null if this connection is not connected.
 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.
 boolean isConnected()
          Returns true if this connection is connected to the remote end.
 void removeListener(Listener listener)
           
 int sendTCP(java.lang.Object object)
          Sends the object over the network using TCP.
 int sendUDP(java.lang.Object object)
          Sends the object over the network using UDP.
 void setBufferPositionFix(boolean bufferPositionFix)
          Workaround for broken NIO networking on Android 1.6.
 void setKeepAliveTCP(int keepAliveMillis)
          An empty object will be sent if the TCP connection has not sent an object within the specified milliseconds.
 void setName(java.lang.String name)
          Sets the friendly name of this connection.
 void setTimeout(int timeoutMillis)
          If the specified amount of time passes without receiving an object over TCP, the connection is considered closed.
 java.lang.String toString()
           
 void updateReturnTripTime()
          Requests the connection to communicate with the remote computer to determine a new value for the return trip time.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

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.


sendTCP

public int sendTCP(java.lang.Object object)
Sends the object over the network using TCP.

Returns:
The number of bytes sent.
See Also:
Kryo.register(Class, com.esotericsoftware.kryo.Serializer)

sendUDP

public int sendUDP(java.lang.Object object)
Sends the object over the network using UDP.

Returns:
The number of bytes sent.
Throws:
java.lang.IllegalStateException - if this connection was not opened with both TCP and UDP.
See Also:
Kryo.register(Class, com.esotericsoftware.kryo.Serializer)

close

public void close()

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), KryoNet 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(Listener listener)
If the listener already exists, it is not added again.


removeListener

public void removeListener(Listener listener)

getEndPoint

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


getRemoteAddressTCP

public java.net.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 java.net.InetSocketAddress getRemoteAddressUDP()
Returns the IP address and port of the remote end of the UDP connection, or null if this connection is not connected.


setBufferPositionFix

public void setBufferPositionFix(boolean bufferPositionFix)
Workaround for broken NIO networking on Android 1.6. If true, the underlying NIO buffer is always copied to the beginning of the buffer before being given to the SocketChannel for sending. The Harmony SocketChannel implementation in Android 1.6 ignores the buffer position, always copying from the beginning of the buffer. This is fixed in Android 2.0+.


setName

public void setName(java.lang.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.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object