com.esotericsoftware.kryonet
Class Client

java.lang.Object
  extended by com.esotericsoftware.kryonet.Connection
      extended by com.esotericsoftware.kryonet.Client
All Implemented Interfaces:
EndPoint, java.lang.Runnable

public class Client
extends Connection
implements EndPoint

Represents a TCP and optionally a UDP connection to a Server.

Author:
Nathan Sweet

Constructor Summary
Client()
          Creates a Client with a write buffer size of 8192 and an object buffer size of 2048.
Client(int writeBufferSize, int objectBufferSize)
           
Client(int writeBufferSize, int objectBufferSize, com.esotericsoftware.kryo.Kryo kryo)
           
 
Method Summary
 void addListener(Listener listener)
          If the listener already exists, it is not added again.
 void close()
           
 void connect(int timeout, java.net.InetAddress host, int tcpPort)
          Opens a TCP only client.
 void connect(int timeout, java.net.InetAddress host, int tcpPort, int udpPort)
          Opens a TCP and UDP client.
 void connect(int timeout, java.lang.String host, int tcpPort)
          Opens a TCP only client.
 void connect(int timeout, java.lang.String host, int tcpPort, int udpPort)
          Opens a TCP and UDP client.
 java.net.InetAddress discoverHost(int udpPort, int timeoutMillis)
          Broadcasts a UDP message on the LAN to discover any running servers.
 java.util.List<java.net.InetAddress> discoverHosts(int udpPort, int timeoutMillis)
          Broadcasts a UDP message on the LAN to discover any running servers.
 com.esotericsoftware.kryo.Kryo getKryo()
          Gets the Kryo instance that will be used to serialize and deserialize objects.
 java.lang.Thread getUpdateThread()
          Returns the last thread that called EndPoint.update(int) for this end point.
 void reconnect()
          Calls connect with the values last passed to connect.
 void reconnect(int timeout)
          Calls connect with the specified timeout and the other values last passed to connect.
 void removeListener(Listener listener)
           
 void run()
          Continually updates this end point until EndPoint.stop() is called.
 void setKeepAliveUDP(int keepAliveMillis)
          An empty object will be sent if the UDP connection is inactive more than the specified milliseconds.
 void start()
          Starts a new thread that calls EndPoint.run().
 void stop()
          Closes this end point and causes EndPoint.run() to return.
 void update(int timeout)
          Reads or writes any pending data for this client.
 
Methods inherited from class com.esotericsoftware.kryonet.Connection
getEndPoint, getID, getRemoteAddressTCP, getRemoteAddressUDP, getReturnTripTime, isConnected, sendTCP, sendUDP, setBufferPositionFix, setKeepAliveTCP, setName, setTimeout, toString, updateReturnTripTime
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Client

public Client()
Creates a Client with a write buffer size of 8192 and an object buffer size of 2048.


Client

public Client(int writeBufferSize,
              int objectBufferSize)
Parameters:
writeBufferSize - One buffer of this size is allocated. Objects are serialized to the write buffer where the bytes are queued until they can be written to the socket.

Normally the socket is writable and the bytes are written immediately. If the socket cannot be written to and enough serialized objects are queued to overflow the buffer, then the connection will be closed.

The write buffer should be sized at least as large as the largest object that will be sent, plus some head room to allow for some serialized objects to be queued in case the buffer is temporarily not writable. The amount of head room needed is dependent upon the size of objects being sent and how often they are sent.

objectBufferSize - Two (using only TCP) or four (using both TCP and UDP) buffers of this size are allocated. These buffers are used to hold the bytes for a single object graph until it can be sent over the network or deserialized.

The object buffers should be sized at least as large as the largest object that will be sent or received.


Client

public Client(int writeBufferSize,
              int objectBufferSize,
              com.esotericsoftware.kryo.Kryo kryo)
Method Detail

getKryo

public com.esotericsoftware.kryo.Kryo getKryo()
Description copied from interface: EndPoint
Gets the Kryo instance that will be used to serialize and deserialize objects.

Specified by:
getKryo in interface EndPoint

connect

public void connect(int timeout,
                    java.lang.String host,
                    int tcpPort)
             throws java.io.IOException
Opens a TCP only client.

Throws:
java.io.IOException
See Also:
connect(int, InetAddress, int, int)

connect

public void connect(int timeout,
                    java.lang.String host,
                    int tcpPort,
                    int udpPort)
             throws java.io.IOException
Opens a TCP and UDP client.

Throws:
java.io.IOException
See Also:
connect(int, InetAddress, int, int)

connect

public void connect(int timeout,
                    java.net.InetAddress host,
                    int tcpPort)
             throws java.io.IOException
Opens a TCP only client.

Throws:
java.io.IOException
See Also:
connect(int, InetAddress, int, int)

connect

public void connect(int timeout,
                    java.net.InetAddress host,
                    int tcpPort,
                    int udpPort)
             throws java.io.IOException
Opens a TCP and UDP client. Blocks until the connection is complete or the timeout is reached.

Because the framework must perform some minimal communication before the connection is considered successful, update(int) must be called on a separate thread during the connection process.

Throws:
java.lang.IllegalStateException - if called from the connection's update thread.
java.io.IOException - if the client could not be opened or connecting times out.

reconnect

public void reconnect()
               throws java.io.IOException
Calls connect with the values last passed to connect.

Throws:
java.lang.IllegalStateException - if connect has never been called.
java.io.IOException

reconnect

public void reconnect(int timeout)
               throws java.io.IOException
Calls connect with the specified timeout and the other values last passed to connect.

Throws:
java.lang.IllegalStateException - if connect has never been called.
java.io.IOException

update

public void update(int timeout)
            throws java.io.IOException
Reads or writes any pending data for this client. Multiple threads should not call this method at the same time.

Specified by:
update in interface EndPoint
Parameters:
timeout - Wait for up to the specified milliseconds for data to be ready to process. May be zero to return immediately if there is no data to process.
Throws:
java.io.IOException
See Also:
update(int), Server.update(int)

run

public void run()
Description copied from interface: EndPoint
Continually updates this end point until EndPoint.stop() is called.

Specified by:
run in interface EndPoint
Specified by:
run in interface java.lang.Runnable

start

public void start()
Description copied from interface: EndPoint
Starts a new thread that calls EndPoint.run().

Specified by:
start in interface EndPoint

stop

public void stop()
Description copied from interface: EndPoint
Closes this end point and causes EndPoint.run() to return.

Specified by:
stop in interface EndPoint

close

public void close()
Specified by:
close in interface EndPoint
Overrides:
close in class Connection
See Also:
Client, Server

addListener

public void addListener(Listener listener)
Description copied from class: Connection
If the listener already exists, it is not added again.

Specified by:
addListener in interface EndPoint
Overrides:
addListener in class Connection

removeListener

public void removeListener(Listener listener)
Specified by:
removeListener in interface EndPoint
Overrides:
removeListener in class Connection

setKeepAliveUDP

public void setKeepAliveUDP(int keepAliveMillis)
An empty object will be sent if the UDP connection is inactive more than the specified milliseconds. Network hardware may keep a translation table of inside to outside IP addresses and a UDP keep alive keeps this table entry from expiring. Set to zero to disable. Defaults to 19000.


getUpdateThread

public java.lang.Thread getUpdateThread()
Description copied from interface: EndPoint
Returns the last thread that called EndPoint.update(int) for this end point. This can be useful to detect when long running code will be run on the update thread.

Specified by:
getUpdateThread in interface EndPoint

discoverHost

public java.net.InetAddress discoverHost(int udpPort,
                                         int timeoutMillis)
Broadcasts a UDP message on the LAN to discover any running servers. The address of the first server to respond is returned.

Parameters:
udpPort - The UDP port of the server.
timeoutMillis - The number of milliseconds to wait for a response.
Returns:
the first server found, or null if no server responded.

discoverHosts

public java.util.List<java.net.InetAddress> discoverHosts(int udpPort,
                                                          int timeoutMillis)
Broadcasts a UDP message on the LAN to discover any running servers.

Parameters:
udpPort - The UDP port of the server.
timeoutMillis - The number of milliseconds to wait for a response.