3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 08:10:09 +01:00

Merge pull request #1005 from creeper123123321/sendpackettoserver

PacketWrapper#sendToServer changes
Dieser Commit ist enthalten in:
Myles 2018-09-23 18:18:00 +01:00 committet von GitHub
Commit c09a9c88b4
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
4 geänderte Dateien mit 105 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -5,6 +5,7 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.MovementTracker;
@ -24,7 +25,7 @@ public class BungeeMovementTransmitter extends MovementTransmitterProvider {
PacketWrapper wrapper = new PacketWrapper(0x03, null, userConnection); PacketWrapper wrapper = new PacketWrapper(0x03, null, userConnection);
wrapper.write(Type.BOOLEAN, userConnection.get(MovementTracker.class).isGround()); wrapper.write(Type.BOOLEAN, userConnection.get(MovementTracker.class).isGround());
try { try {
wrapper.sendToServer(); wrapper.sendToServer(Protocol1_9TO1_8.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

Datei anzeigen

@ -298,7 +298,7 @@ public class PacketWrapper {
*/ */
public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
if (!isCancelled()) { if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline); ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.OUTGOING);
user().sendRawPacket(output, currentThread); user().sendRawPacket(output, currentThread);
} }
} }
@ -311,11 +311,13 @@ public class PacketWrapper {
* @return Packet buffer * @return Packet buffer
* @throws Exception if it fails to write * @throws Exception if it fails to write
*/ */
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception { private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
// Apply current pipeline // Apply current pipeline
List<Protocol> protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes()); List<Protocol> protocols = new ArrayList<>(user().get(ProtocolInfo.class).getPipeline().pipes());
if (direction == Direction.OUTGOING) {
// Other way if outgoing // Other way if outgoing
Collections.reverse(protocols); Collections.reverse(protocols);
}
int index = 0; int index = 0;
for (int i = 0; i < protocols.size(); i++) { for (int i = 0; i < protocols.size(); i++) {
if (protocols.get(i).getClass().equals(packetProtocol)) { if (protocols.get(i).getClass().equals(packetProtocol)) {
@ -360,7 +362,7 @@ public class PacketWrapper {
*/ */
public ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception { public ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception {
if (!isCancelled()) { if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, true); ByteBuf output = constructPacket(packetProtocol, true, Direction.OUTGOING);
return user().sendRawPacketFuture(output); return user().sendRawPacketFuture(output);
} }
return user().getChannel().newFailedFuture(new Exception("Cancelled packet")); return user().getChannel().newFailedFuture(new Exception("Cancelled packet"));
@ -471,15 +473,40 @@ public class PacketWrapper {
* *
* @throws Exception If it failed to write * @throws Exception If it failed to write
*/ */
@Deprecated
public void sendToServer() throws Exception { public void sendToServer() throws Exception {
if (!isCancelled()) { if (!isCancelled()) {
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
writeToBuffer(output); writeToBuffer(output);
user().getChannel().pipeline().context(Via.getManager().getInjector().getDecoderName()).fireChannelRead(output); user().sendRawPacketToServer(output, true);
} }
} }
/**
* Send this packet to the server.
*
* @param packetProtocol - The protocol version of the packet.
* @param skipCurrentPipeline - Skip the current pipeline
* @param currentThread - Run in the same thread
* @throws Exception if it fails to write
*/
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
if (!isCancelled()) {
ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING);
user().sendRawPacketToServer(output, currentThread);
}
}
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception {
sendToServer(packetProtocol, skipCurrentPipeline, false);
}
public void sendToServer(Class<? extends Protocol> packetProtocol) throws Exception {
sendToServer(packetProtocol, true);
}
@Override @Override
public String toString() { public String toString() {
return "PacketWrapper{" + return "PacketWrapper{" +

Datei anzeigen

@ -3,12 +3,17 @@ package us.myles.ViaVersion.api.data;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import lombok.Data; import lombok.Data;
import lombok.NonNull;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.ViaVersionConfig;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -16,6 +21,13 @@ import java.util.concurrent.ConcurrentHashMap;
@Data @Data
public class UserConnection { public class UserConnection {
/**
* The channel of the user.
* /!\ In some unofficial platforms this is a client channel
*
* TODO - Weak this field to {@link io.netty.channel.Channel}?
*/
@NonNull
private final SocketChannel channel; private final SocketChannel channel;
Map<Class, StoredObject> storedObjects = new ConcurrentHashMap<>(); Map<Class, StoredObject> storedObjects = new ConcurrentHashMap<>();
private boolean active = true; private boolean active = true;
@ -128,7 +140,7 @@ public class UserConnection {
*/ */
public boolean incrementReceived() { public boolean incrementReceived() {
// handle stats // handle stats
Long diff = System.currentTimeMillis() - startTime; long diff = System.currentTimeMillis() - startTime;
if (diff >= 1000) { if (diff >= 1000) {
packetsPerSecond = intervalPackets; packetsPerSecond = intervalPackets;
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
@ -147,7 +159,7 @@ public class UserConnection {
// Max PPS Checker // Max PPS Checker
if (conf.getMaxPPS() > 0) { if (conf.getMaxPPS() > 0) {
if (getPacketsPerSecond() >= conf.getMaxPPS()) { if (getPacketsPerSecond() >= conf.getMaxPPS()) {
disconnect(conf.getMaxPPSKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + "")); disconnect(conf.getMaxPPSKickMessage().replace("%pps", Long.toString(getPacketsPerSecond())));
return true; // don't send current packet return true; // don't send current packet
} }
} }
@ -165,7 +177,7 @@ public class UserConnection {
} }
if (getWarnings() >= conf.getMaxWarnings()) { if (getWarnings() >= conf.getMaxWarnings()) {
disconnect(conf.getMaxWarningsKickMessage().replace("%pps", ((Long) getPacketsPerSecond()).intValue() + "")); disconnect(conf.getMaxWarningsKickMessage().replace("%pps", Long.toString(getPacketsPerSecond())));
return true; // don't send current packet return true; // don't send current packet
} }
} }
@ -195,4 +207,48 @@ public class UserConnection {
} }
} }
/**
* Sends a raw packet to the server
*
* @param packet Raw packet to be sent
* @param currentThread If {@code true} executes immediately, {@code false} submits a task to EventLoop
*/
public void sendRawPacketToServer(final ByteBuf packet, boolean currentThread) {
final ByteBuf buf = packet.alloc().buffer();
try {
Type.VAR_INT.write(buf, PacketWrapper.PASSTHROUGH_ID);
} catch (Exception e) {
// Should not happen
Via.getPlatform().getLogger().warning("Type.VAR_INT.write thrown an exception: " + e);
}
buf.writeBytes(packet);
packet.release();
final ChannelHandlerContext context = PipelineUtil.getPreviousContext(Via.getManager().getInjector().getDecoderName(), getChannel().pipeline());
if (currentThread) {
if (context != null) {
context.fireChannelRead(buf);
} else {
getChannel().pipeline().fireChannelRead(buf);
}
} else {
channel.eventLoop().submit(new Runnable() {
@Override
public void run() {
if (context != null) {
context.fireChannelRead(buf);
} else {
getChannel().pipeline().fireChannelRead(buf);
}
}
});
}
}
/**
* Sends a raw packet to the server. It will submit a task to EventLoop
*
* @param packet Raw packet to be sent
*/
public void sendRawPacketToServer(ByteBuf packet) { sendRawPacketToServer(packet, false); }
} }

Datei anzeigen

@ -100,4 +100,15 @@ public class PipelineUtil {
} }
return null; return null;
} }
public static ChannelHandlerContext getPreviousContext(String name, ChannelPipeline pipeline) {
String previous = null;
for (String entry : pipeline.toMap().keySet()) {
if (entry.equals(name)) {
return pipeline.context(previous);
}
previous = entry;
}
return null;
}
} }