Package lu.kbra.p4j.socket.client
Class P4JClient
java.lang.Object
lu.kbra.p4j.socket.client.P4JClient
- All Implemented Interfaces:
Closeable,AutoCloseable,Runnable,P4JInstance,P4JInstance.P4JClientInstance,EventDispatcher
public class P4JClient
extends Object
implements P4JInstance.P4JClientInstance, EventDispatcher, Closeable, Runnable
This class represents the client-side Client connecting to the server.
-
Nested Class Summary
Nested classes/interfaces inherited from interface lu.kbra.p4j.socket.P4JInstance
P4JInstance.P4JClientInstance, P4JInstance.P4JClientServerInstance, P4JInstance.P4JServerClientInstance, P4JInstance.P4JServerInstance -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionP4JClient(CodecManager codecManager, EncryptionManager encryptionManager, CompressionManager compressionManager) -
Method Summary
Modifier and TypeMethodDescriptionvoidbind()Bind to a random available port on the local machine.voidbind(int port) Bind to the specified port on the local machine.voidbind(InetSocketAddress inetSocketAddress) Bind to the specified port on the local machine.voidclose()Closes the client socket.
The client' socket will be closed and the port will be released.
Doesn't dispatch aP4JConnectionEvent.ClientDisconnectedEvent.voidconnect(InetAddress remote, int port) Connect to the specified address and port on the remote machine.voidconnect(InetSocketAddress inetSocketAddress) Connect to the specified address and port on the remote machine.voidDisconnects & closes the client socket
And dispatches aP4JConnectionEvent.ClientDisconnectedEvent.voiddispatchEvent(P4JEvent event) getCodec()intfinal P4JEndPointintgetPort()getState()final booleanisAlive()booleanisBound()booleanfinal voidjoin()final voidjoin(long millis) final voidjoin(long millis, int nanos) protected voidread()protected voidread_handleRawPacket(int id, ByteBuffer content) voidregisterPacket(Class<?> p, int id) voidrun()voidsetClientServerSupplier(BiFunction<P4JClient, InetSocketAddress, ClientServer> clientServerSupplier) voidsetCodec(CodecManager codec) voidsetCompression(CompressionManager compression) voidsetConnectionTimeout(int connectionTimeout) voidsetEncryption(EncryptionManager encryption) voidsetEventManager(EventManager eventManager) voidsetPackets(PacketManager packets) booleanboolean
-
Field Details
-
MAX_PACKET_SIZE
public static int MAX_PACKET_SIZE
-
-
Constructor Details
-
P4JClient
public P4JClient(CodecManager codecManager, EncryptionManager encryptionManager, CompressionManager compressionManager) - Parameters:
codecManager- the client's codec managerencryptionManager- the client's encryption managercompressionManager- the client's compression manager
-
-
Method Details
-
bind
Bind to a random available port on the local machine. -
bind
Bind to the specified port on the local machine. If the port is 0, a random available port is chosen.- Parameters:
inetSocketAddress- the local address to bind to- Throws:
IOException- if theSocketcannot be created or bound
-
bind
Bind to the specified port on the local machine. If the port is 0, a random available port is chosen.- Parameters:
port- the port to bind to- Throws:
P4JClientExceptionUnknownHostException
-
close
public void close()Closes the client socket.
The client' socket will be closed and the port will be released.
Doesn't dispatch aP4JConnectionEvent.ClientDisconnectedEvent.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
P4JClientException- if the client isn't started
-
connect
Connect to the specified address and port on the remote machine.- Parameters:
remote- the remote addressport- the remote port- Throws:
IOException- if theSocketcannot be connected
-
connect
Connect to the specified address and port on the remote machine. -
disconnect
public void disconnect()Disconnects & closes the client socket
And dispatches aP4JConnectionEvent.ClientDisconnectedEvent.- Throws:
P4JClientException- if the client isn't started
-
dispatchEvent
-
getClientServer
-
getClientServerSupplier
-
getClientStatus
-
getCodec
-
getCompression
-
getConnectionTimeout
public int getConnectionTimeout() -
getEncryption
-
getEndPoint
- Specified by:
getEndPointin interfaceP4JInstance- Specified by:
getEndPointin interfaceP4JInstance.P4JClientInstance
-
getEventManager
-
getLocalInetSocketAddress
-
getPackets
-
getPort
public int getPort()- Returns:
- the current port the client is connected to or -1 if it isn't bound
-
getRemoteInetSocketAddress
-
getState
-
isAlive
public final boolean isAlive() -
isBound
public boolean isBound() -
isConnected
public boolean isConnected() -
join
- Throws:
InterruptedException
-
join
- Throws:
InterruptedException
-
join
- Throws:
InterruptedException
-
registerPacket
-
run
public void run() -
setClientServerSupplier
public void setClientServerSupplier(BiFunction<P4JClient, InetSocketAddress, ClientServer> clientServerSupplier) -
setCodec
-
setCompression
-
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout) -
setEncryption
-
setEventManager
-
setPackets
-
testConnection
public boolean testConnection() -
write
-
read
protected void read() -
read_handleRawPacket
-