Высокопроизводительная индексация клиентов внутри протокола

This commit is contained in:
RoyceDa
2026-02-04 06:01:12 +02:00
parent 6a5c01bf25
commit cd1e6e6b14
5 changed files with 301 additions and 16 deletions

View File

@@ -6,8 +6,10 @@ import org.java_websocket.WebSocket;
import io.orprotocol.BaseFailures;
import io.orprotocol.ProtocolException;
import io.orprotocol.Server;
import io.orprotocol.ServerFailures;
import io.orprotocol.Stream;
import io.orprotocol.index.ClientIndexer;
import io.orprotocol.packet.Packet;
import io.orprotocol.packet.PacketManager;
import io.orprotocol.util.StringUtil;
@@ -32,6 +34,7 @@ public class Client {
*/
private volatile long lastHeartbeatTime;
private ClientIndexer clientIndexer;
private PacketManager packetManager;
/**
@@ -40,13 +43,14 @@ public class Client {
* @param socket Веб-сокет клиента.
*
*/
public Client(WebSocket socket, long heartbeatInterval, PacketManager packetManager) {
public Client(WebSocket socket, long heartbeatInterval, Server server) {
this.socket = socket;
this.clientId = StringUtil.randomString(32);
this.eciTags = new HashMap<Class<? extends ECITag>, ECITag>();
this.heartbeatInterval = heartbeatInterval;
this.lastHeartbeatTime = System.currentTimeMillis();
this.packetManager = packetManager;
this.clientIndexer = server.getClientIndexer();
this.packetManager = server.getPacketManager();
}
/**
@@ -96,16 +100,18 @@ public class Client {
* @param key Ключ данных.
* @param value Значение данных.
*/
public <T extends ECITag> void addTag(T eciTag) {
this.eciTags.put(eciTag.getClass(), eciTag);
}
/**
* Устанавливает данные клиента.
* @param data Данные клиента.
*/
public void setTags(HashMap<Class<? extends ECITag>, ECITag> eciTags) {
this.eciTags = eciTags;
public <T extends ECITag> void addTag(Class<T> tagClass, T eciTag) {
if (eciTag == null) {
this.eciTags.remove(tagClass);
if (this.clientIndexer != null) {
this.clientIndexer.removeTagIndex(this, tagClass);
}
} else {
this.eciTags.put(tagClass, eciTag);
if (this.clientIndexer != null) {
this.clientIndexer.indexTag(this, tagClass, eciTag);
}
}
}
/**
@@ -174,4 +180,22 @@ public class Client {
this.socket.send(stream.getBuffer());
}
/**
* Проверяем схожесть двух Client
* @param client клиент
* @return true если это один и тот же клиент, false если нет
*/
public boolean equals(Client client) {
if(client == null){
return false;
}
if(!(client instanceof Client)){
return false;
}
if(!client.getClientId().equals(this.clientId)){
return false;
}
return true;
}
}