Class TCPServer
- All Implemented Interfaces:
Runnable,NetworkApplication,NetServer
- Direct Known Subclasses:
PlainTCPServer,TLSServer
NetServer.- API Note:
- Before version 2.1.0, this class was in package
org.omegazero.net.server.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intprotected final Collection<InetAddress> protected final Collection<Integer> protected final Function<SocketConnection, Consumer<Runnable>> -
Constructor Summary
ConstructorsConstructorDescriptionTCPServer(Collection<InetAddress> bindAddresses, Collection<Integer> ports, int backlog, Function<SocketConnection, Consumer<Runnable>> workerCreator, long idleTimeout) Constructs a newTCPServerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()to return.intlongprotected abstract ChannelConnectionhandleConnection(SelectionKey selectionKey) protected voidCalled when a connection closed.protected voidhandleConnectionPost(ChannelConnection connection) protected voidCalled when a key was selected in a select call in theSelectorHandler.runSelectorLoop()method.voidinit()Initializes this application.voidsetConnectionCallback(Consumer<SocketConnection> handler) Sets the callback for a new incoming connection.voidsetIdleTimeout(long idleTimeout) voidstart()Runs the main loop of this instance.Methods inherited from class org.omegazero.net.nio.util.ConnectionSelectorHandler
loopIteration, onConnectionClosedMethods inherited from class org.omegazero.net.nio.util.SelectorHandler
closeSelector, initSelector, isRunning, registerChannel, registerChannel, runSelectorLoop, selectorKeys, selectorWakeupMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.omegazero.net.common.NetworkApplication
run
-
Field Details
-
bindAddresses
-
ports
-
backlog
protected final int backlog -
workerCreator
-
-
Constructor Details
-
TCPServer
public TCPServer(Collection<InetAddress> bindAddresses, Collection<Integer> ports, int backlog, Function<SocketConnection, Consumer<Runnable>> workerCreator, long idleTimeout) Constructs a newTCPServerinstance.To initialize the server,
NetworkApplication.init()of this object must be called. After the call succeeded, the server will listen on the specified local address (bindAddress) on the given ports andNetworkApplication.start()must be called to start processing incoming connection requests and data.- Parameters:
bindAddresses- A collection of local addresses to bind to (seeServerSocketChannel.bind(java.net.SocketAddress, int)). May benullto use an automatically assigned addressports- The list of ports to listen onbacklog- The maximum number of pending connections (seeServerSocketChannel.bind(java.net.SocketAddress, int)). May be 0 to use a default valueworkerCreator- The worker creatoridleTimeout- The time in milliseconds to keep connections that had no traffic. May be 0 to disable closing idle connections
-
-
Method Details
-
handleConnection
- Throws:
IOException
-
handleConnectionPost
-
init
Description copied from interface:NetworkApplicationInitializes this application.- Specified by:
initin interfaceNetworkApplication- Throws:
IOException- If an IO error occurs during initialization
-
close
Description copied from interface:NetworkApplicationCloses this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()to return.- Specified by:
closein interfaceNetworkApplication- Throws:
IOException- If an IO error occurs
-
start
Description copied from interface:NetworkApplicationRuns the main loop of this instance. This loop processes incoming or outgoing connection requests and network traffic.Under normal circumstances, never returns before
NetworkApplication.close()is called. Afterclose()is called, this function should return as soon as possible.If this method is called before
NetworkApplication.init(), the behavior is undefined.- Specified by:
startin interfaceNetworkApplication- Throws:
IOException- If a fatal IO error occurs during any networking operation
-
setConnectionCallback
Description copied from interface:NetServerSets the callback for a new incoming connection.The first parameter of this callback is an
SocketConnectioninstance representing the new connection from the client.This method must be called before
NetworkApplication.start(), otherwise, behavior is undefined.- Specified by:
setConnectionCallbackin interfaceNetServer- Parameters:
handler- The connection callback
-
handleConnectionClosed
Description copied from class:ConnectionSelectorHandlerCalled when a connection closed.- Overrides:
handleConnectionClosedin classConnectionSelectorHandler- Parameters:
conn- The connection- Throws:
IOException
-
handleSelectedKey
Description copied from class:SelectorHandlerCalled when a key was selected in a select call in theSelectorHandler.runSelectorLoop()method.- Specified by:
handleSelectedKeyin classSelectorHandler- Parameters:
key- The selected key- Throws:
IOException- If an IO error occurs
-
getIdleTimeout
public long getIdleTimeout() -
setIdleTimeout
public void setIdleTimeout(long idleTimeout) -
getBacklog
public int getBacklog()
-