Package io.github.joealisson.mmocore
Class Client<T extends Connection<?>>
- java.lang.Object
-
- io.github.joealisson.mmocore.Client<T>
-
public abstract class Client<T extends Connection<?>> extends java.lang.Object
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()close the underlying Connection to the client.voidclose(WritablePacket<? extends Client<T>> packet)Sends the packet and close the underlying Connection to the client.abstract booleandecrypt(byte[] data, int offset, int size)Decrypt the data in-placeprotected voiddisconnect()abstract byte[]encrypt(byte[] data, int offset, int size)Encrypt the data in-place.abstract intencryptedSize(int dataSize)java.lang.StringgetHostAddress()booleanisConnected()abstract voidonConnected()Handles the client's connection.protected abstract voidonDisconnection()Handles the client's disconnection.protected voidwritePacket(WritablePacket<? extends Client<T>> packet)Sends a packet to this client.
-
-
-
Constructor Detail
-
Client
public Client(T connection)
Construct a new Client- Parameters:
connection- - The Connection to the client.- Throws:
java.lang.IllegalArgumentException- if the connection is null or closed.
-
-
Method Detail
-
writePacket
protected final void writePacket(WritablePacket<? extends Client<T>> packet)
Sends a packet to this client. If another packet is been sent to this client, the actual packet is put on a queue to be sent after all previous packets. Otherwise the packet is sent immediately.- Parameters:
packet- to be sent.
-
close
public void close()
close the underlying Connection to the client. All pending packets are cancelled.
-
close
public void close(WritablePacket<? extends Client<T>> packet)
Sends the packet and close the underlying Connection to the client. All others pending packets are cancelled.- Parameters:
packet- to be sent before the connection is closed.
-
disconnect
protected final void disconnect()
-
getHostAddress
public java.lang.String getHostAddress()
- Returns:
- The client's IP address.
-
isConnected
public boolean isConnected()
- Returns:
- if client still connected
-
encryptedSize
public abstract int encryptedSize(int dataSize)
- Parameters:
dataSize- the data size to be encrypted- Returns:
- the size of the data after encrypted
-
encrypt
public abstract byte[] encrypt(byte[] data, int offset, int size)Encrypt the data in-place.- Parameters:
data- - the data to be encryptedoffset- - the initial index to be encryptedsize- - the length of data to be encrypted- Returns:
- The data after encrypted
-
decrypt
public abstract boolean decrypt(byte[] data, int offset, int size)Decrypt the data in-place- Parameters:
data- - data to be decryptedoffset- - the initial index to be encrypted.size- - the length of data to be encrypted.- Returns:
- if the data was decrypted.
-
onDisconnection
protected abstract void onDisconnection()
Handles the client's disconnection. This method must save all data and release all resources related to the client. No more packet can be sent after this method is called.
-
onConnected
public abstract void onConnected()
Handles the client's connection. This method should not use blocking operations. The Packets can be sent only after this method is called.
-
-