Package org.omegazero.net.nio.server
Class UDPServer
java.lang.Object
org.omegazero.net.nio.util.SelectorHandler
org.omegazero.net.nio.util.ConnectionSelectorHandler
org.omegazero.net.nio.server.UDPServer
- All Implemented Interfaces:
Runnable
,NetworkApplication
,NetServer
- Direct Known Subclasses:
DTLSServer
,PlainUDPServer
UDP/IP implementation of a
NetServer
.- API Note:
- Before version 2.1.0, this class was in package
org.omegazero.net.server
.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Collection
<InetAddress> protected final Collection
<Integer> protected final Function
<SocketConnection, Consumer<Runnable>> -
Constructor Summary
ConstructorsConstructorDescriptionUDPServer
(Collection<InetAddress> bindAddresses, Collection<Integer> ports, Function<SocketConnection, Consumer<Runnable>> workerCreator, long idleTimeout, int receiveBufferSize) Constructs a newUDPServer
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.long
protected abstract ChannelConnection
handleConnection
(SelectionKey serverKey, SocketAddress remote) 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.protected void
Notify thisConnectionSelectorHandler
that aChannelConnection
was closed locally by a call toSocketConnection.close()
.void
setConnectionCallback
(Consumer<SocketConnection> handler) Sets the callback for a new incoming connection.void
setIdleTimeout
(long idleTimeout) void
start()
Runs the main loop of this instance.protected final void
writeBacklogStarted
(ChannelConnection connection) Methods inherited from class org.omegazero.net.nio.util.ConnectionSelectorHandler
handleConnectionClosed, loopIteration
Methods inherited from class org.omegazero.net.nio.util.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
-
Constructor Details
-
UDPServer
public UDPServer(Collection<InetAddress> bindAddresses, Collection<Integer> ports, Function<SocketConnection, Consumer<Runnable>> workerCreator, long idleTimeout, int receiveBufferSize) Constructs a newUDPServer
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 and outgoing data.- Parameters:
bindAddresses
- A collection of local addresses to bind to (seeinvalid reference
ServerSocketChannel#bind(java.net.SocketAddress, int)
null
to use an automatically assigned addressports
- The list of ports to listen onworkerCreator
- The worker creatoridleTimeout
- The time in milliseconds to keep connections that had no traffic. Metadata for UDP connections not closed usingSocketConnection.close()
or by this idle timeout will be stored indefinitely, which should be avoidedreceiveBufferSize
- The size of the receive buffer. Should be set to the maximum expected packet size
-
-
Method Details
-
handleConnection
protected abstract ChannelConnection handleConnection(SelectionKey serverKey, SocketAddress remote) throws IOException - Throws:
IOException
-
handleConnectionPost
-
writeBacklogStarted
-
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, 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:
start
in interfaceNetworkApplication
- Throws:
IOException
- If a fatal IO error occurs during any networking operation
-
setConnectionCallback
Description copied from interface:NetServer
Sets the callback for a new incoming connection.The first parameter of this callback is an
SocketConnection
instance representing the new connection from the client.This method must be called before
NetworkApplication.start()
, otherwise, behavior is undefined.- Specified by:
setConnectionCallback
in interfaceNetServer
- Parameters:
handler
- The connection callback
-
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
-
onConnectionClosed
Description copied from class:ConnectionSelectorHandler
Notify thisConnectionSelectorHandler
that aChannelConnection
was closed locally by a call toSocketConnection.close()
.- Overrides:
onConnectionClosed
in classConnectionSelectorHandler
- Parameters:
conn
- The connection that closed
-
getIdleTimeout
public long getIdleTimeout() -
setIdleTimeout
public void setIdleTimeout(long idleTimeout)
-