Реализована функции отправки пакетов клиенту

This commit is contained in:
RoyceDa
2026-02-03 00:18:53 +02:00
parent f4be7b4e00
commit c22d2de4be
6 changed files with 55 additions and 8 deletions

View File

@@ -6,7 +6,11 @@ import java.util.Set;
import org.java_websocket.WebSocket;
import io.orprotocol.BaseFailures;
import io.orprotocol.ProtocolException;
import io.orprotocol.ServerFailures;
import io.orprotocol.Stream;
import io.orprotocol.packet.Packet;
import io.orprotocol.packet.PacketManager;
import io.orprotocol.util.StringUtil;
/**
@@ -29,18 +33,21 @@ public class Client {
*/
private volatile long lastHeartbeatTime;
private PacketManager packetManager;
/**
* Создает нового клиента с указанным сокетом.
* Этот метод используется внутри протокола для управления подключениями клиентов.
* @param socket Веб-сокет клиента.
*
*/
public Client(WebSocket socket, long heartbeatInterval) {
public Client(WebSocket socket, long heartbeatInterval, PacketManager packetManager) {
this.socket = socket;
this.clientId = StringUtil.randomString(32);
this.eciTags = new HashSet<ECITag>();
this.heartbeatInterval = heartbeatInterval;
this.lastHeartbeatTime = System.currentTimeMillis();
this.packetManager = new PacketManager();
}
/**
@@ -160,4 +167,21 @@ public class Client {
this.disconnect(ServerFailures.UNKNOWN_FAILURE);
}
/**
* Отправляет пакет клиенту.
* @param packet Пакет для отправки.
*/
public void send(Packet packet) throws ProtocolException {
Integer packetId = this.packetManager.getPacketIdByClass(packet.getClass());
if(packetId == null) {
throw new ProtocolException("Unknown packet class: " + packet.getClass().getName());
}
packet.packetId = packetId;
/**
* Записываем пакет в поток и отправляем его через сокет.
*/
Stream stream = packet.write();
this.socket.send(stream.getBuffer());
}
}