Package io.github.joealisson.mmocore
Class ConnectionBuilder<T extends Client<Connection<T>>>
- java.lang.Object
-
- io.github.joealisson.mmocore.ConnectionBuilder<T>
-
public class ConnectionBuilder<T extends Client<Connection<T>>> extends java.lang.ObjectBuilds theConnectionHandlerresponsible to manage all incoming connections.
-
-
Constructor Summary
Constructors Constructor Description ConnectionBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ConnectionBuilder<T>addBufferPool(int size, int bufferSize)Add a newByteBuffergrouping poolConnectionBuilder<T>bufferSegmentSize(int size)Define the size of dynamic buffer's segment.ConnectionHandler<T>build()Builds a new ConnectionHandler based on the options configured.static <T extends Client<Connection<T>>>
ConnectionBuilder<T>create(java.net.InetSocketAddress address, ClientFactory<T> clientFactory, PacketHandler<T> packetHandler, PacketExecutor<T> executor)Creates a ConnectionBuilder holding the minimum requirements to create a ConnectionHandler.ConnectionBuilder<T>dropPacketThreshold(int threshold)Define the threshold to allow the client to drop disposable packets.ConnectionBuilder<T>filter(ConnectionFilter filter)Sets a filter to be used on incoming connections.ConnectionBuilder<T>initBufferPoolFactor(float factor)Define the factor of pre-initializedByteBufferinside a pool.ConnectionBuilder<T>maxCachedThreads(int size)Set the size of max threads allowed in the cached thread pool.ConnectionBuilder<T>shutdownWaitTime(long waitTime)Sets the shutdown wait time in milliseconds.ConnectionBuilder<T>threadPoolSize(int size)Set the size of the threadPool used to manage the connections and data sending.ConnectionBuilder<T>useCachedThreadPool(boolean cached)Configures the connection to use CachedThreadPool as defined inAsynchronousChannelGroup.withCachedThreadPool(java.util.concurrent.ExecutorService, int).ConnectionBuilder<T>useNagle(boolean useNagle)Defines if small outgoing packets must be combined to be sent all at once.
-
-
-
Method Detail
-
create
public static <T extends Client<Connection<T>>> ConnectionBuilder<T> create(java.net.InetSocketAddress address, ClientFactory<T> clientFactory, PacketHandler<T> packetHandler, PacketExecutor<T> executor)
Creates a ConnectionBuilder holding the minimum requirements to create a ConnectionHandler.- Type Parameters:
T- - The Type of client that ConnectionBuilder will handle.- Parameters:
address- - The socket address to listen the incoming connections.clientFactory- - The factory responsible to create a new Client when a new connection is accepted.packetHandler- - The handle responsible to convert the data received into aReadablePacketexecutor- - The responsible to execute the incoming packets.- Returns:
- A ConnectionBuilder with default configuration.
-
filter
public ConnectionBuilder<T> filter(ConnectionFilter filter)
Sets a filter to be used on incoming connections. The filter must decide if a connection is acceptable or not.- Parameters:
filter- - theConnectionFilterto be set.- Returns:
- this.
-
useCachedThreadPool
public ConnectionBuilder<T> useCachedThreadPool(boolean cached)
Configures the connection to use CachedThreadPool as defined inAsynchronousChannelGroup.withCachedThreadPool(java.util.concurrent.ExecutorService, int). The default behaviour is to use a fixed thread poll as defined inAsynchronousChannelGroup.withFixedThreadPool(int, java.util.concurrent.ThreadFactory).- Parameters:
cached- use a cached thread pool if true, otherwise use fixed thread pool- Returns:
- this
-
threadPoolSize
public ConnectionBuilder<T> threadPoolSize(int size)
Set the size of the threadPool used to manage the connections and data sending. If the thread pool is cached this method defines the corePoolSize of (ThreadPoolExecutor) If the thread pool is fixed this method defines the amount of threads The min accepted value is 1. The default value is the quantity of available processors minus 2.- Parameters:
size- - the size of thread pool to be Set- Returns:
- this
-
maxCachedThreads
public ConnectionBuilder<T> maxCachedThreads(int size)
Set the size of max threads allowed in the cached thread pool. The execution we be rejected when all the threads in the cached thread pool is busy after reaching the max thread allowed. This config is ignored when a fixed thread pool is used.- Parameters:
size- the max cached threads in the cached thread pool.- Returns:
- this
-
useNagle
public ConnectionBuilder<T> useNagle(boolean useNagle)
Defines if small outgoing packets must be combined to be sent all at once. This improves the network performance, but can cause lags on clients waiting for the packet. The default value is false.- Parameters:
useNagle- - true if the Nagle's algorithm must be used.- Returns:
- this.
-
shutdownWaitTime
public ConnectionBuilder<T> shutdownWaitTime(long waitTime)
Sets the shutdown wait time in milliseconds. The default value is 5 seconds.- Parameters:
waitTime- - the wait time to close all connections resources after aConnectionHandler.shutdown()is called.- Returns:
- this
-
addBufferPool
public ConnectionBuilder<T> addBufferPool(int size, int bufferSize)
Add a newByteBuffergrouping pool- Parameters:
size- the max amount ofByteBuffersupportedbufferSize- theByteBuffer's size inside the pool.- Returns:
- this
-
initBufferPoolFactor
public ConnectionBuilder<T> initBufferPoolFactor(float factor)
Define the factor of pre-initializedByteBufferinside a pool.- Parameters:
factor- the factor of initialized buffers- Returns:
- this
-
bufferSegmentSize
public ConnectionBuilder<T> bufferSegmentSize(int size)
Define the size of dynamic buffer's segment. A segment is used to increase the Buffer when needed.- Parameters:
size- of dynamic buffer segment- Returns:
- this
-
dropPacketThreshold
public ConnectionBuilder<T> dropPacketThreshold(int threshold)
Define the threshold to allow the client to drop disposable packets. When the client has queued more thanthresholddisposable packets will be disposed.- Parameters:
threshold- the minimum value to drop packets. The default value is 250- Returns:
- this
-
build
public ConnectionHandler<T> build() throws java.io.IOException
Builds a new ConnectionHandler based on the options configured.- Returns:
- a ConnectionHandler
- Throws:
java.io.IOException- - If the Socket Address configured can't be used.
-
-