Package arc.net

Class Client

All Implemented Interfaces:
EndPoint, Runnable

public class Client extends Connection implements EndPoint
Represents a TCP and optionally a UDP connection to a Server.
  • Constructor Details

    • Client

      public Client(int writeBufferSize, int objectBufferSize, NetSerializer serialization)
      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 TCP 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 - One (using only TCP) or three (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.

  • Method Details

    • setDiscoveryPacket

      public void setDiscoveryPacket(Prov<DatagramPacket> discoveryPacket)
    • connect

      public void connect(int timeout, String host, int tcpPort) throws IOException
      Opens a TCP only client.
      Throws:
      IOException
      See Also:
    • connect

      public void connect(int timeout, String host, int tcpPort, int udpPort) throws IOException
      Opens a TCP and UDP client.
      Throws:
      IOException
      See Also:
    • connect

      public void connect(int timeout, InetAddress host, int tcpPort) throws IOException
      Opens a TCP only client.
      Throws:
      IOException
      See Also:
    • connect

      public void connect(int timeout, InetAddress host, int tcpPort, int udpPort) throws 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:
      IllegalStateException - if called from the connection's update thread.
      IOException - if the client could not be opened or connecting times out.
    • reconnect

      public void reconnect() throws IOException
      Calls connect with the values last passed to connect.
      Throws:
      IllegalStateException - if connect has never been called.
      IOException
    • reconnect

      public void reconnect(int timeout) throws IOException
      Calls connect with the specified timeout and the other values last passed to connect.
      Throws:
      IllegalStateException - if connect has never been called.
      IOException
    • update

      public void update(int timeout) throws 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:
      IOException
      See Also:
    • 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 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
      See Also:
    • dispose

      public void dispose() throws IOException
      Releases the resources used by this client, which may no longer be used.
      Throws:
      IOException
    • 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 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
    • getSerialization

      public NetSerializer getSerialization()
    • discoverHosts

      public void discoverHosts(int udpPort, String multicastGroup, int multicastPort, int timeoutMillis, Cons<DatagramPacket> handler, Runnable done)
      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.