2016-02-28 23:44:33 +01:00
|
|
|
package us.myles.ViaVersion;
|
|
|
|
|
2016-03-04 20:24:44 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2016-03-07 23:21:32 +01:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2016-03-02 11:12:43 +01:00
|
|
|
import io.netty.channel.socket.SocketChannel;
|
2016-03-07 23:21:32 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2016-02-28 23:44:33 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import us.myles.ViaVersion.packets.State;
|
|
|
|
|
2016-03-07 23:21:32 +01:00
|
|
|
@Getter
|
|
|
|
@Setter
|
2016-02-28 23:44:33 +01:00
|
|
|
public class ConnectionInfo {
|
2016-03-09 22:16:58 +01:00
|
|
|
private static final long IDLE_PACKET_DELAY = 50L; // Update every 50ms (20tps)
|
|
|
|
private static final long IDLE_PACKET_LIMIT = 20; // Max 20 ticks behind
|
|
|
|
|
2016-03-02 11:12:43 +01:00
|
|
|
private final SocketChannel channel;
|
2016-02-28 23:44:33 +01:00
|
|
|
private Object lastPacket;
|
|
|
|
private java.util.UUID UUID;
|
2016-03-02 16:21:07 +01:00
|
|
|
private State state = State.HANDSHAKE;
|
2016-03-06 01:25:24 +01:00
|
|
|
private String openWindow;
|
2016-03-02 16:21:07 +01:00
|
|
|
private int protocol = 0;
|
|
|
|
private int compression = 0;
|
2016-03-07 23:21:32 +01:00
|
|
|
private int entityID;
|
2016-03-02 16:21:07 +01:00
|
|
|
private boolean active = true;
|
2016-03-07 00:22:45 +01:00
|
|
|
private String username;
|
2016-03-09 22:16:58 +01:00
|
|
|
private long nextIdlePacket = 0L;
|
2016-02-28 23:44:33 +01:00
|
|
|
|
2016-03-02 11:12:43 +01:00
|
|
|
public ConnectionInfo(SocketChannel socketChannel) {
|
|
|
|
this.channel = socketChannel;
|
|
|
|
}
|
|
|
|
|
2016-02-28 23:44:33 +01:00
|
|
|
public Player getPlayer() {
|
|
|
|
return UUID == null ? null : Bukkit.getPlayer(UUID);
|
|
|
|
}
|
2016-03-02 11:12:43 +01:00
|
|
|
|
2016-03-02 16:21:07 +01:00
|
|
|
public void setActive(boolean active) {
|
|
|
|
this.active = active;
|
2016-03-09 22:16:58 +01:00
|
|
|
this.nextIdlePacket = System.currentTimeMillis() + IDLE_PACKET_DELAY; // Update every 50 ticks
|
2016-03-02 16:21:07 +01:00
|
|
|
}
|
2016-03-04 20:24:44 +01:00
|
|
|
|
2016-03-07 23:21:32 +01:00
|
|
|
public void sendRawPacket(final ByteBuf packet, boolean currentThread) {
|
2016-03-04 21:03:46 +01:00
|
|
|
final ChannelHandler handler = channel.pipeline().get("encoder");
|
2016-03-07 23:21:32 +01:00
|
|
|
if (currentThread) {
|
|
|
|
channel.pipeline().context(handler).writeAndFlush(packet);
|
|
|
|
} else {
|
|
|
|
channel.eventLoop().submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
channel.pipeline().context(handler).writeAndFlush(packet);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-03-06 01:25:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-07 23:21:32 +01:00
|
|
|
public void sendRawPacket(final ByteBuf packet) {
|
|
|
|
sendRawPacket(packet, false);
|
2016-03-06 01:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void closeWindow() {
|
|
|
|
this.openWindow = null;
|
|
|
|
}
|
2016-03-09 22:16:58 +01:00
|
|
|
|
|
|
|
public void incrementIdlePacket() {
|
|
|
|
// Notify of next update
|
|
|
|
// Allow a maximum lag spike of 1 second (20 ticks/updates)
|
|
|
|
this.nextIdlePacket = Math.max(nextIdlePacket + IDLE_PACKET_DELAY, System.currentTimeMillis() - IDLE_PACKET_DELAY * IDLE_PACKET_LIMIT);
|
|
|
|
}
|
2016-02-28 23:44:33 +01:00
|
|
|
}
|