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>bufferDefaultSize(int bufferSize)Sets the size limit of the data buffer sent/received.ConnectionBuilder<T>bufferLargePoolSize(int bufferPoolSize)Sets the maximum amount of buffer with large size that can be holder on the BufferPool.ConnectionBuilder<T>bufferLargeSize(int bufferSize)Sets the large size of the data buffer sent/received.ConnectionBuilder<T>bufferMediumPoolSize(int bufferPoolSize)Sets the maximum amount of buffer with medium size that can be holder on the BufferPool.ConnectionBuilder<T>bufferMediumSize(int bufferSize)Sets the medium size of the data buffer sent/received.ConnectionBuilder<T>bufferPoolSize(int bufferPoolSize)Sets the maximum amount of buffer with default size that can be holder on the BufferPool.ConnectionBuilder<T>bufferSmallPoolSize(int bufferPoolSize)Sets the maximum amount of buffer with small size that can be holder on the BufferPool.ConnectionBuilder<T>bufferSmallSize(int bufferSize)Sets the small size of the data buffer sent/received.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>filter(ConnectionFilter filter)Sets a filter to be used on incoming connections.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>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.
-
threadPoolSize
public ConnectionBuilder<T> threadPoolSize(int size)
Set the size of the threadPool used to manage the connections and data sending. If the size is less than or equal to zero or greater thanShort.MAX_VALUEthen a cachedThreadPool is used. Otherwise a FixedThreadPool with the size set is used. The default value is the quantity of available processors minus 2.- Parameters:
size- - the size to be Set- 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
-
bufferDefaultSize
public ConnectionBuilder<T> bufferDefaultSize(int bufferSize)
Sets the size limit of the data buffer sent/received. The size must be as bigger as the biggest packet received. The default value is 8KB.- Parameters:
bufferSize- - the buffer size to be set- Returns:
- this.
-
bufferSmallSize
public ConnectionBuilder<T> bufferSmallSize(int bufferSize)
Sets the small size of the data buffer sent/received. The default value is 1KB.- Parameters:
bufferSize- - the buffer size to be set- Returns:
- this.
-
bufferLargeSize
public ConnectionBuilder<T> bufferLargeSize(int bufferSize)
Sets the large size of the data buffer sent/received. The default value is 4KB.- Parameters:
bufferSize- - the buffer size to be set- Returns:
- this.
-
bufferMediumSize
public ConnectionBuilder<T> bufferMediumSize(int bufferSize)
Sets the medium size of the data buffer sent/received. The default value is 2KB.- Parameters:
bufferSize- - the buffer size to be set- Returns:
- this.
-
bufferPoolSize
public ConnectionBuilder<T> bufferPoolSize(int bufferPoolSize)
Sets the maximum amount of buffer with default size that can be holder on the BufferPool. A small value can be lead to buffer overhead creation. Otherwise a too big size can be lead to unwanted memory usage. The size must be restricted related to the number of expected clients and taking considerations of system resources. The default value is 100.- Parameters:
bufferPoolSize- - the size of the buffer pool size.- Returns:
- this
-
bufferSmallPoolSize
public ConnectionBuilder<T> bufferSmallPoolSize(int bufferPoolSize)
Sets the maximum amount of buffer with small size that can be holder on the BufferPool. A small value can be lead to buffer overhead creation. Otherwise a too big size can be lead to unwanted memory usage. The size must be restricted related to the number of expected clients and taking considerations of system resources. The default value is 100.- Parameters:
bufferPoolSize- - the size of the buffer pool size.- Returns:
- this
-
bufferMediumPoolSize
public ConnectionBuilder<T> bufferMediumPoolSize(int bufferPoolSize)
Sets the maximum amount of buffer with medium size that can be holder on the BufferPool. A small value can be lead to buffer overhead creation. Otherwise a too big size can be lead to unwanted memory usage. The size must be restricted related to the number of expected clients and taking considerations of system resources. The default value is 50.- Parameters:
bufferPoolSize- - the size of the buffer pool size.- Returns:
- this
-
bufferLargePoolSize
public ConnectionBuilder<T> bufferLargePoolSize(int bufferPoolSize)
Sets the maximum amount of buffer with large size that can be holder on the BufferPool. A small value can be lead to buffer overhead creation. Otherwise a too big size can be lead to unwanted memory usage. The size must be restricted related to the number of expected clients and taking considerations of system resources. The default value is 25.- Parameters:
bufferPoolSize- - the size of the buffer pool size.- 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.
-
-