use of com.github.dirtpowered.dirtmv.data.translator.ServerProtocol in project DirtMultiversion by DirtPowered.
the class ClientSession method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, PacketData packetData) {
// server to client packets
serverSession.sendPacket(packetData, PacketDirection.TO_CLIENT, null);
// notify other sessions
if (!stateLock) {
boolean postNettyFlag = serverSession.getUserData().getProtocolState() == ProtocolState.PLAY;
boolean preNettyFlag = serverSession.getUserData().getPreNettyProtocolState() == PreNettyProtocolState.IN_GAME;
if (preNettyFlag || postNettyFlag) {
MinecraftVersion server = main.getConfiguration().getServerVersion();
MinecraftVersion client = serverSession.getUserData().getClientVersion();
// call #onConnect only in user protocols
for (ServerProtocol t : main.getTranslatorRegistry().getAllProtocolsBetween(server, client)) {
t.onConnect(serverSession);
}
stateLock = true;
}
}
}
use of com.github.dirtpowered.dirtmv.data.translator.ServerProtocol in project DirtMultiversion by DirtPowered.
the class ServerSession method sendPacket.
/**
* Translates and sends packet to server
*
* @param packet {@link PacketData} Packet
* @param direction {@link PacketDirection} sending direction (client/server)
* @param from Version to start from
*/
public void sendPacket(PacketData packet, PacketDirection direction, MinecraftVersion from) {
MinecraftVersion version = main.getConfiguration().getServerVersion();
if (from != null && from != version) {
version = from;
}
List<ServerProtocol> protocols = main.getTranslatorRegistry().findProtocol(userData, version);
boolean flag = direction == PacketDirection.TO_CLIENT;
if (!flag) {
Collections.reverse(protocols);
}
PacketData target = packet;
for (ServerProtocol protocol : protocols) {
boolean isNetty = userData.getClientVersion().isNettyProtocol();
ProtocolState state = isNetty ? userData.getProtocolState() : ProtocolState.PRE_NETTY;
if (target == null) {
return;
}
// packet queue workaround
state = packet.getNettyState() != null ? packet.getNettyState() : state;
if (!protocol.getFrom().isNettyProtocol()) {
state = ProtocolState.PRE_NETTY;
}
PacketTranslator translator = protocol.getTranslatorFor(target.getOpCode(), state, direction);
if (translator != null) {
if (from == null || from != protocol.getFrom()) {
try {
target = translator.translate(this, target);
} catch (IOException e) {
disconnect(e.getMessage());
return;
}
if (target.getOpCode() == -1) {
return;
}
}
}
}
if (flag) {
sendPacket(target);
} else {
ClientSession clientSession = getClientSession();
if (target.getOpCode() == 2 && /* handshake */
!hasServerPingProtocol()) {
connectToServer();
}
if (clientSession != null && clientSession.getChannel().isActive()) {
clientSession.sendPacket(target);
return;
}
// queued packets are always login packets
target.setNettyState(ProtocolState.LOGIN);
initialPacketQueue.add(target);
}
}
use of com.github.dirtpowered.dirtmv.data.translator.ServerProtocol in project DirtMultiversion by DirtPowered.
the class TranslatorRegistry method findProtocol.
/**
* Returns all protocols translators between client and server version
*
* @param data User data
* @param versionTo Server version
* @return {@link List<ServerProtocol> List} with ordered protocol pipeline classes
*/
public List<ServerProtocol> findProtocol(UserData data, MinecraftVersion versionTo) {
List<ServerProtocol> serverProtocols = new LinkedList<>();
MinecraftVersion from = data.getClientVersion();
Configuration c = main.getConfiguration();
GlobalProtocolHandler globalProtocolHandler = new GlobalProtocolHandler(from, versionTo);
// check if translating is needed
if (from == c.getServerVersion()) {
ServerProtocol serverProtocol;
// starting from r1.3 the whole connections is encrypted
if (from.getRegistryId() >= MinecraftVersion.R1_3_1.getRegistryId() && !from.isNettyProtocol()) {
serverProtocol = new ProtocolPassthroughEncrypted(from, versionTo);
} else if (from.isNettyProtocol()) {
serverProtocol = new ProtocolStateHandler(from, versionTo);
} else {
serverProtocol = new ProtocolPassthrough(from, versionTo);
}
return Arrays.asList(serverProtocol, globalProtocolHandler);
} else {
if (from.getRegistryId() < 39) {
serverProtocols.add(new ProtocolPassthrough(from, versionTo));
}
if ((from.getRegistryId() >= 39 && c.getServerVersion().getRegistryId() >= 39) && !from.isNettyProtocol()) {
// add encryption translators to pipeline
serverProtocols.add(new ProtocolPassthroughEncrypted(from, versionTo));
}
}
int clientProtocol = from.getRegistryId();
int serverProtocol = versionTo.getRegistryId();
for (int i = serverProtocol; i <= clientProtocol; i++) {
if (MinecraftVersion.fromRegistryId(i) != null) {
ServerProtocol target = protocols.get(i);
if (target != null && !(i <= serverProtocol)) {
serverProtocols.add(target);
}
}
}
if (from.isNettyProtocol()) {
serverProtocols.add(new ProtocolStateHandler(from, versionTo));
}
// track packets in all protocols
serverProtocols.add(globalProtocolHandler);
return serverProtocols;
}
use of com.github.dirtpowered.dirtmv.data.translator.ServerProtocol in project DirtMultiversion by DirtPowered.
the class TranslatorRegistry method getAllProtocolsBetween.
/**
* Gets all protocols between given versions
*
* @param server - server version
* @param client - client version
* @return List of {@link ServerProtocol protocol translators}
*/
public List<ServerProtocol> getAllProtocolsBetween(MinecraftVersion server, MinecraftVersion client) {
int serverProtocol = server.getRegistryId();
int clientProtocol = client.getRegistryId();
List<ServerProtocol> serverProtocols = new LinkedList<>();
for (int i = serverProtocol; i <= clientProtocol; i++) {
if (MinecraftVersion.fromRegistryId(i) != null) {
ServerProtocol target = protocols.get(i);
if (target != null && !(i <= serverProtocol)) {
serverProtocols.add(target);
}
}
}
return serverProtocols;
}
Aggregations