Package arc.net

Class Server

java.lang.Object
arc.net.Server
All Implemented Interfaces:
EndPoint, Runnable

public class Server extends Object implements EndPoint
Manages TCP and optionally UDP connections from many Clients.
  • Field Details

    • multicastGroup

      protected InetAddress multicastGroup
    • discoveryReceiver

      protected arc.net.Server.DiscoveryReceiver discoveryReceiver
    • discoveryHandler

      protected ServerDiscoveryHandler discoveryHandler
  • Constructor Details

    • Server

      public Server(int writeBufferSize, int objectBufferSize, NetSerializer serializer)
      Parameters:
      writeBufferSize - One buffer of this size is allocated for each connected client. 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

    • setMulticast

      public void setMulticast(String group, int multicastPort)
    • setDiscoveryHandler

      public void setDiscoveryHandler(ServerDiscoveryHandler newDiscoveryHandler)
    • setConnectFilter

      public void setConnectFilter(Server.ServerConnectFilter connectFilter)
    • bind

      public void bind(int tcpPort) throws IOException
      Opens a TCP only server.
      Throws:
      IOException - if the server could not be opened.
    • bind

      public void bind(int tcpPort, int udpPort) throws IOException
      Opens a TCP and UDP server. All clients must also have a TCP and an UDP port.
      Throws:
      IOException - if the server could not be opened.
    • bind

      public void bind(InetSocketAddress tcpPort, InetSocketAddress udpPort) throws IOException
      Parameters:
      udpPort - May be null.
      Throws:
      IOException
    • update

      public void update(int timeout) throws IOException
      Accepts any new connections and reads or writes any pending data for the current connections.
      Specified by:
      update in interface EndPoint
      Parameters:
      timeout - Wait for up to the specified milliseconds for a connection to be ready to process. May be zero to return immediately if there are no connections 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
    • newConnection

      protected Connection newConnection()
      Allows the connections used by the server to be subclassed. This can be useful for storage per connection without an additional lookup.
    • sendToAllTCP

      public void sendToAllTCP(Object object)
    • sendToAllExceptTCP

      public void sendToAllExceptTCP(int connectionID, Object object)
    • sendToTCP

      public void sendToTCP(int connectionID, Object object)
    • sendToAllUDP

      public void sendToAllUDP(Object object)
    • sendToAllExceptUDP

      public void sendToAllExceptUDP(int connectionID, Object object)
    • sendToUDP

      public void sendToUDP(int connectionID, Object object)
    • addListener

      public void addListener(NetListener listener)
      Adds a listener to the endpoint. If the listener already exists, it is not added again.

      Should be called before connect().

      Specified by:
      addListener in interface EndPoint
    • removeListener

      public void removeListener(NetListener listener)
      Specified by:
      removeListener in interface EndPoint
    • close

      public void close()
      Closes all open connections and the server port(s).
      Specified by:
      close in interface EndPoint
      See Also:
    • dispose

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

      public Connection[] getConnections()
      Returns the current connections. The array returned should not be modified.