Package arc.net
Class Server
java.lang.Object
arc.net.Server
Manages TCP and optionally UDP connections from many Clients.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ServerDiscoveryHandlerprotected arc.net.Server.DiscoveryReceiverprotected NetListenerprotected NetListener[]protected InetAddressprotected intprotected final intprotected final NetSerializerprotected final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(NetListener listener) Adds a listener to the endpoint.voidbind(int tcpPort) Opens a TCP only server.voidbind(int tcpPort, int udpPort) Opens a TCP and UDP server.voidbind(InetSocketAddress tcpPort, InetSocketAddress udpPort) voidclose()Closes all open connections and the server port(s).voiddispose()Releases the resources used by this server, which may no longer be used.Returns the current connections.Returns the last thread that calledEndPoint.update(int)for this end point.protected ConnectionAllows the connections used by the server to be subclassed.voidremoveListener(NetListener listener) voidrun()Continually updates this end point untilEndPoint.stop()is called.voidsendToAllExceptTCP(int connectionID, Object object) voidsendToAllExceptUDP(int connectionID, Object object) voidsendToAllTCP(Object object) voidsendToAllUDP(Object object) voidvoidvoidsetConnectFilter(Server.ServerConnectFilter connectFilter) voidsetDiscoveryHandler(ServerDiscoveryHandler newDiscoveryHandler) voidsetMulticast(String group, int multicastPort) voidstart()Starts a new thread that callsEndPoint.run().voidstop()Closes this end point and causesEndPoint.run()to return.voidupdate(int timeout) Accepts any new connections and reads or writes any pending data for the current connections.
-
Field Details
-
serializer
-
writeBufferSize
protected final int writeBufferSize -
objectBufferSize
protected final int objectBufferSize -
listeners
-
multicastPort
protected int multicastPort -
multicastGroup
-
discoveryReceiver
protected arc.net.Server.DiscoveryReceiver discoveryReceiver -
discoveryHandler
-
dispatchListener
-
-
Constructor Details
-
Server
- 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
-
setDiscoveryHandler
-
setConnectFilter
-
getConnectFilter
-
bind
Opens a TCP only server.- Throws:
IOException- if the server could not be opened.
-
bind
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
- Parameters:
udpPort- May be null.- Throws:
IOException
-
update
Accepts any new connections and reads or writes any pending data for the current connections.- Specified by:
updatein interfaceEndPoint- 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:EndPointContinually updates this end point untilEndPoint.stop()is called. -
start
public void start()Description copied from interface:EndPointStarts a new thread that callsEndPoint.run(). -
stop
public void stop()Description copied from interface:EndPointCloses this end point and causesEndPoint.run()to return. -
newConnection
Allows the connections used by the server to be subclassed. This can be useful for storage per connection without an additional lookup. -
sendToAllTCP
-
sendToAllExceptTCP
-
sendToTCP
-
sendToAllUDP
-
sendToAllExceptUDP
-
sendToUDP
-
addListener
Adds a listener to the endpoint. If the listener already exists, it is not added again.Should be called before connect().
- Specified by:
addListenerin interfaceEndPoint
-
removeListener
- Specified by:
removeListenerin interfaceEndPoint
-
close
public void close()Closes all open connections and the server port(s). -
dispose
Releases the resources used by this server, which may no longer be used.- Throws:
IOException
-
getUpdateThread
Description copied from interface:EndPointReturns the last thread that calledEndPoint.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:
getUpdateThreadin interfaceEndPoint
-
getConnections
Returns the current connections. The array returned should not be modified.
-