public abstract class Client
extends java.lang.Object
implements java.io.Closeable
This is the base Client object for client communication. Anything that reads or writes data will use this object. The clients work by having buffered reads and writes for the socket. They tell the SocketExecuter when a read can be added or a write is ready for the socket.
All reads are issued on a callback on a Reader in a single threaded manor. All writes will be sent in the order they are received. In general its better to write full protocol parsable packets with each write where possible. If not possible your own locking/ordering will have to be ensured. Close events will happen on there own Closer callback but it uses the same ThreadKey as the Reader thread. All Reads should be received before a close event happens.
The client object can not function with out being in a SocketExecuter. Writes can be added before its put in the executer, but it will not write to the socket until added to the executer.| Modifier and Type | Class and Description |
|---|---|
static interface |
Client.ClientCloseListener
Used to notify when a Client is closed.
|
static interface |
Client.ClientOptions
ClientOptions that can be changed depending on what kind of client you want.
|
static interface |
Client.Reader
Used to notify when a Client there is data to Read for a Client.
|
| Constructor and Description |
|---|
Client(org.threadly.litesockets.SocketExecuterCommonBase se,
boolean statsEnabled) |
| Modifier and Type | Method and Description |
|---|---|
void |
addCloseListener(Client.ClientCloseListener closer)
This adds a
Client.ClientCloseListener for this client. |
boolean |
canRead()
Returns true if this client can have reads added to it or false if its read buffers are full.
|
abstract boolean |
canWrite()
Returns true if this client has data pending in its write buffers.
|
abstract Client.ClientOptions |
clientOptions()
This allows you to set/get client options for this client connection.
|
void |
close()
Closes this client.
|
abstract void |
close(java.lang.Throwable error)
Closes this client.
|
abstract org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> |
connect()
Called to connect this client to a host.
|
SocketExecuter |
getClientsSocketExecuter()
This is used to get the clients
SocketExecuter. |
org.threadly.concurrent.SubmitterExecutor |
getClientsThreadExecutor()
This returns this clients
SubmitterExecutor. |
abstract java.net.SocketAddress |
getLocalSocketAddress() |
abstract WireProtocol |
getProtocol()
This is used by the
SocketExecuter to help understand how to manage this client. |
ReuseableMergedByteBuffers |
getRead()
Whenever a the
Client.Reader Interfaces Client.Reader.onRead(Client) is called the
getRead() should be called from the client. |
int |
getReadBufferSize()
This is used to get the current size of the readBuffers pending reads.
|
abstract java.net.SocketAddress |
getRemoteSocketAddress() |
SimpleByteStats |
getStats()
Returns the
SimpleByteStats for this client. |
abstract int |
getTimeout()
Used to get this clients connection timeout information.
|
abstract int |
getWriteBufferSize()
This is used to get the current size of the unWriten writeBuffer.
|
abstract boolean |
hasConnectionTimedOut()
This tells us if the client has timed out before it has been connected to the socket.
|
boolean |
isClosed()
Returns if this client is closed or not.
|
abstract org.threadly.concurrent.future.ListenableFuture<?> |
lastWriteFuture() |
abstract void |
setConnectionTimeout(int timeout)
Sets the connection timeout value for this client.
|
void |
setReader(Client.Reader reader)
This sets the Reader for the client.
|
void |
setStatsEnabled(boolean enabled)
Can be invoked with
false to disable stat collection on client. |
abstract org.threadly.concurrent.future.ListenableFuture<?> |
write(java.nio.ByteBuffer bb)
This is called to write data to the clients socket.
|
abstract org.threadly.concurrent.future.ListenableFuture<?> |
write(MergedByteBuffers mbb)
This is called to write data to the clients socket.
|
public Client(org.threadly.litesockets.SocketExecuterCommonBase se,
boolean statsEnabled)
public abstract java.net.SocketAddress getRemoteSocketAddress()
SocketAddress this client is connected to.public abstract java.net.SocketAddress getLocalSocketAddress()
SocketAddress this client is using.public abstract boolean canWrite()
public abstract Client.ClientOptions clientOptions()
Client.ClientOptions object to set/get options for this client.public abstract org.threadly.concurrent.future.ListenableFuture<java.lang.Boolean> connect()
Called to connect this client to a host. This is done non-blocking.
If there is an error connecting close() will also be called on the client.
ListenableFuture that will complete when the socket is connected, or fail if we can't connect.public abstract void setConnectionTimeout(int timeout)
connect() has called on this client.timeout - the time in milliseconds to wait for the client to connect.public abstract boolean hasConnectionTimedOut()
This tells us if the client has timed out before it has been connected to the socket. If the client has connected fully this will return false from that point on (even on a closed connection).
public abstract int getTimeout()
Used to get this clients connection timeout information.
public abstract int getWriteBufferSize()
This is used to get the current size of the unWriten writeBuffer.
public abstract WireProtocol getProtocol()
This is used by the SocketExecuter to help understand how to manage this client.
Currently only UDP and TCP exist.
public abstract org.threadly.concurrent.future.ListenableFuture<?> write(java.nio.ByteBuffer bb)
This is called to write data to the clients socket. Its important to note that there is no back
pressure when adding writes so care should be taken to now allow the clients getWriteBufferSize() to get
to big.
bb - The ByteBuffer to write onto the clients socket.ListenableFuture that will be completed once the data has been fully written to the socket.public abstract org.threadly.concurrent.future.ListenableFuture<?> write(MergedByteBuffers mbb)
This is called to write data to the clients socket. Its important to note that there is no back
pressure when adding writes so care should be taken to now allow the clients getWriteBufferSize() to get
to big.
mbb - The MergedByteBuffers to write onto the clients socket.ListenableFuture that will be completed once the data has been fully written to the socket.public abstract org.threadly.concurrent.future.ListenableFuture<?> lastWriteFuture()
public void close()
Closes this client. Reads can still occur after this it called. Client.ClientCloseListener.onClose(Client) will still be
called (if set) once all reads are done.
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablepublic abstract void close(java.lang.Throwable error)
Closes this client. Reads can still occur after this it called. Client.ClientCloseListener.onClose(Client) will still be
called (if set) once all reads are done.
error - The error that resulted in us closing this client, or null if closing normallypublic boolean canRead()
public int getReadBufferSize()
This is used to get the current size of the readBuffers pending reads.
public org.threadly.concurrent.SubmitterExecutor getClientsThreadExecutor()
This returns this clients SubmitterExecutor.
Its worth noting that operations done on this SubmitterExecutor can/will block Read callbacks on the
client, but it does provide you the ability to execute things on the clients read thread.
SubmitterExecutor for the client.public SocketExecuter getClientsSocketExecuter()
This is used to get the clients SocketExecuter.
SocketExecuter set for this client. if none, null is returned.public void addCloseListener(Client.ClientCloseListener closer)
This adds a Client.ClientCloseListener for this client. Once set the client will call .onClose
on it once it a socket close is detected.
closer - sets this clients Client.ClientCloseListener callback.public void setReader(Client.Reader reader)
This sets the Reader for the client. Once set data received on the socket will be callback on this Reader to be processed. If no reader is set before connecting the client read data will just queue up till we hit the the max buffer size
reader - the Client.Reader callback to set for this client.public ReuseableMergedByteBuffers getRead()
Whenever a the Client.Reader Interfaces Client.Reader.onRead(Client) is called the
getRead() should be called from the client.
ReuseableMergedByteBuffers of the current read data for this client.public boolean isClosed()
Returns if this client is closed or not. Once a client is marked closed there is no way to reOpen it. You must just make a new client. Just because this returns false does not mean the client is connected. Before a client connects, but has not yet closed this will be false.
public SimpleByteStats getStats()
SimpleByteStats for this client.public void setStatsEnabled(boolean enabled)
false to disable stat collection on client. This can reduce
minor memory and performance overheads.enabled - false to disable stat collection, or true to enable / reset stats