Package org.omegazero.net.server
Class TCPServer
java.lang.Object
org.omegazero.net.common.SelectorHandler
org.omegazero.net.common.ConnectionSelectorHandler
org.omegazero.net.server.TCPServer
- All Implemented Interfaces:
Runnable
,NetworkApplication
,NetServer
- Direct Known Subclasses:
PlainTCPServer
,TLSServer
TCP/IP server implementation of a
NetServer
based on java.nio channels.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final int
protected final Collection<InetAddress>
protected final Collection<Integer>
-
Constructor Summary
ConstructorsConstructorDescriptionTCPServer
(Collection<Integer> ports) TCPServer
(Collection<InetAddress> bindAddresses, Collection<Integer> ports, int backlog, Consumer<Runnable> worker, long idleTimeout) Constructs a newTCPServer
instance.
To initialize the server,NetworkApplication.init()
of this object must be called. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()
to return.int
long
protected abstract ChannelConnection
handleConnection
(SelectionKey selectionKey) protected void
Called when a connection closed.protected void
handleConnectionPost
(ChannelConnection connection) protected void
Called when a key was selected in a select call in theSelectorHandler.runSelectorLoop()
method.void
init()
Initializes this application.void
setConnectionCallback
(Consumer<SocketConnection> handler) Sets the callback for a new incoming request.
The first parameter of this callback is anSocketConnection
instance representing the new connection from the client.void
setIdleTimeout
(long idleTimeout) void
start()
Runs the main loop of this instance.Methods inherited from class org.omegazero.net.common.ConnectionSelectorHandler
loopIteration, onConnectionClosed
Methods inherited from class org.omegazero.net.common.SelectorHandler
closeSelector, initSelector, isRunning, registerChannel, registerChannel, runSelectorLoop, selectorKeys, selectorWakeup
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.omegazero.net.common.NetworkApplication
run
-
Field Details
-
bindAddresses
-
ports
-
backlog
protected final int backlog -
worker
-
-
Constructor Details
-
TCPServer
- See Also:
-
TCPServer#TCPServer(String, Collection, int, Consumer, long)
-
TCPServer
public TCPServer(Collection<InetAddress> bindAddresses, Collection<Integer> ports, int backlog, Consumer<Runnable> worker, long idleTimeout) Constructs a newTCPServer
instance.
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 benull
to 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 valueworker
- A callback accepting tasks to run that may require increased processing time. May benull
to run everything using a single threadidleTimeout
- 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:NetworkApplication
Initializes this application.- Specified by:
init
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs during initialization
-
close
Description copied from interface:NetworkApplication
Closes this application, closing all bound and connected sockets and stopping the main loop, causing a call toNetworkApplication.start()
to return.- Specified by:
close
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs
-
start
Description copied from interface:NetworkApplication
Runs the main loop of this instance. This loop processes incoming or outgoing connection requests and network traffic.
Under normal circumstances, should never return beforeNetworkApplication.close()
is called. Afterclose()
is called, this function should return as soon as possible.
If this method is called beforeNetworkApplication.init()
, the behavior is undefined.- Specified by:
start
in interfaceNetworkApplication
- Throws:
IOException
- If an IO error occurs during any networking operation
-
setConnectionCallback
Description copied from interface:NetServer
Sets the callback for a new incoming request.
The first parameter of this callback is anSocketConnection
instance representing the new connection from the client.- Specified by:
setConnectionCallback
in interfaceNetServer
- Parameters:
handler
- The connection callback
-
handleConnectionClosed
Description copied from class:ConnectionSelectorHandler
Called when a connection closed.- Overrides:
handleConnectionClosed
in classConnectionSelectorHandler
- Throws:
IOException
-
handleSelectedKey
Description copied from class:SelectorHandler
Called when a key was selected in a select call in theSelectorHandler.runSelectorLoop()
method.- Specified by:
handleSelectedKey
in 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()
-