Package io.github.joealisson.mmocore
Class Connector<T extends Client<Connection<T>>>
- java.lang.Object
-
- io.github.joealisson.mmocore.Connector<T>
-
public class Connector<T extends Client<Connection<T>>> extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description Connector()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Connector<T>addBufferPool(int size, int bufferSize)Add a newByteBuffergrouping poolConnector<T>bufferSegmentSize(int size)Define the size of dynamic buffer's segment.Tconnect(java.lang.String host, int port)Connects to a host using the address and port.Tconnect(java.net.InetSocketAddress socketAddress)Connects to a host with socketAddressstatic <T extends Client<Connection<T>>>
Connector<T>create(ClientFactory<T> clientFactory, PacketHandler<T> packetHandler, PacketExecutor<T> executor)Creates a Connector holding the minimum requirements to create a Client.Connector<T>dropPacketThreshold(int threshold)Define the threshold to allow the client to drop disposable packets.Connector<T>initBufferPoolFactor(float factor)define the factor of pre-initializedByteBufferinside a pool.Connector<T>maxCachedThreads(int size)Set the size of max threads allowed in the cached thread pool.Connector<T>threadPoolSize(int size)Set the size of the threadPool used to manage the connections and data sending.Connector<T>useCachedThreadPool(boolean cached)Configures the connection to use CachedThreadPool as defined inAsynchronousChannelGroup.withCachedThreadPool(java.util.concurrent.ExecutorService, int).
-
-
-
Method Detail
-
create
public static <T extends Client<Connection<T>>> Connector<T> create(ClientFactory<T> clientFactory, PacketHandler<T> packetHandler, PacketExecutor<T> executor)
Creates a Connector holding the minimum requirements to create a Client.- Type Parameters:
T- - The Type of client that ConnectionBuilder will handle.- Parameters:
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.
-
addBufferPool
public Connector<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 Connector<T> initBufferPoolFactor(float factor)
define the factor of pre-initializedByteBufferinside a pool.- Parameters:
factor- the factor of initialized buffers- Returns:
- this
-
bufferSegmentSize
public Connector<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 Connector<T> dropPacketThreshold(int threshold)
Define the threshold to allow the client to drop disposable packets.When the client has queued more than
thresholddisposable packets will can be disposed.- Parameters:
threshold- the minimum value to drop packets. The default value is 250- Returns:
- this
-
useCachedThreadPool
public Connector<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 in
AsynchronousChannelGroup.withFixedThreadPool(int, java.util.concurrent.ThreadFactory).- Parameters:
cached- use a cached thread pool if true, otherwise use fixed thread pool- Returns:
- this
-
threadPoolSize
public Connector<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 threadsThe 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 Connector<T> maxCachedThreads(int size)
Set the size of max threads allowed in the cached thread pool. This config is ignored when a fixed thread pool is used.- Parameters:
size- the max cached threads in the cached thread pool.- Returns:
- this
-
connect
public T connect(java.lang.String host, int port) throws java.io.IOException, java.util.concurrent.ExecutionException, java.lang.InterruptedException
Connects to a host using the address and port.- Parameters:
host- the address to be connected toport- the port of the host- Returns:
- A client connected to the host and port
- Throws:
java.io.IOException- if a IO error happens during the connection.java.util.concurrent.ExecutionException- if the computation threw an exceptionjava.lang.InterruptedException- if the current thread was interrupted while waiting
-
connect
public T connect(java.net.InetSocketAddress socketAddress) throws java.io.IOException, java.util.concurrent.ExecutionException, java.lang.InterruptedException
Connects to a host with socketAddress- Parameters:
socketAddress- the address which will be connected- Returns:
- a client that represents the connection with the socketAddress
- Throws:
java.io.IOException- if a IO error happens during the connection.java.util.concurrent.ExecutionException- if the computation threw an exceptionjava.lang.InterruptedException- if the current thread was interrupted while waiting
-
-