cn.nukkit.network.RakNetInterface Maven / Gradle / Ivy
package cn.nukkit.network;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.event.player.PlayerCreationEvent;
import cn.nukkit.event.server.QueryRegenerateEvent;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.network.session.NetworkPlayerSession;
import cn.nukkit.network.session.RakNetPlayerSession;
import cn.nukkit.utils.Utils;
import com.google.common.base.Strings;
import com.nukkitx.network.raknet.RakNetServer;
import com.nukkitx.network.raknet.RakNetServerListener;
import com.nukkitx.network.raknet.RakNetServerSession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.internal.PlatformDependent;
import lombok.extern.log4j.Log4j2;
import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author MagicDroidX (Nukkit Project)
*/
@Log4j2
public class RakNetInterface implements RakNetServerListener, AdvancedSourceInterface {
private final Server server;
private Network network;
private final RakNetServer raknet;
private final Map sessions = new HashMap<>();
private final Queue sessionCreationQueue = PlatformDependent.newMpscQueue();
private byte[] advertisement;
public RakNetInterface(Server server) {
this.server = server;
InetSocketAddress bindAddress = new InetSocketAddress(Strings.isNullOrEmpty(this.server.getIp()) ? "0.0.0.0" : this.server.getIp(), this.server.getPort());
this.raknet = new RakNetServer(bindAddress, Runtime.getRuntime().availableProcessors());
this.raknet.setProtocolVersion(11);
this.raknet.bind().join();
this.raknet.setListener(this);
}
@Override
public void setNetwork(Network network) {
this.network = network;
}
@Override
public boolean process() {
RakNetPlayerSession session;
while ((session = this.sessionCreationQueue.poll()) != null) {
InetSocketAddress address = session.getRakNetSession().getAddress();
try {
PlayerCreationEvent event = new PlayerCreationEvent(this, Player.class, Player.class, null, address);
this.server.getPluginManager().callEvent(event);
this.sessions.put(event.getSocketAddress(), session);
Constructor extends Player> constructor = event.getPlayerClass().getConstructor(SourceInterface.class, Long.class, InetSocketAddress.class);
Player player = constructor.newInstance(this, event.getClientId(), event.getSocketAddress());
this.server.addPlayer(address, player);
session.setPlayer(player);
} catch (Exception e) {
Server.getInstance().getLogger().error("Failed to create player", e);
session.disconnect("Internal error");
this.sessions.remove(address);
}
}
Iterator iterator = this.sessions.values().iterator();
while (iterator.hasNext()) {
RakNetPlayerSession nukkitSession = iterator.next();
Player player = nukkitSession.getPlayer();
if (nukkitSession.getDisconnectReason() != null) {
player.close(player.getLeaveMessage(), nukkitSession.getDisconnectReason(), false);
iterator.remove();
} else {
nukkitSession.serverTick();
}
}
return true;
}
@Override
public int getNetworkLatency(Player player) {
RakNetServerSession session = this.raknet.getSession(player.getRawSocketAddress());
return session == null ? -1 : (int) session.getPing();
}
@Override
public NetworkPlayerSession getSession(InetSocketAddress address) {
return this.sessions.get(address);
}
@Override
public void close(Player player) {
this.close(player, "unknown reason");
}
@Override
public void close(Player player, String reason) {
NetworkPlayerSession playerSession = this.getSession(player.getRawSocketAddress());
if (playerSession != null) {
playerSession.disconnect(reason);
}
}
@Override
public void shutdown() {
this.sessions.values().forEach(session -> session.disconnect("Shutdown"));
this.raknet.close();
}
@Override
public void emergencyShutdown() {
this.sessions.values().forEach(session -> session.disconnect("Shutdown"));
this.raknet.close();
}
@Override
public void blockAddress(InetAddress address) {
this.raknet.block(address);
}
@Override
public void blockAddress(InetAddress address, int timeout) {
this.raknet.block(address, timeout, TimeUnit.SECONDS);
}
@Override
public void unblockAddress(InetAddress address) {
this.raknet.unblock(address);
}
@Override
public void sendRawPacket(InetSocketAddress socketAddress, ByteBuf payload) {
this.raknet.send(socketAddress, payload);
}
@Override
public void setName(String name) {
QueryRegenerateEvent info = this.server.getQueryInformation();
String[] names = name.split("!@#"); //Split double names within the program
String motd = Utils.rtrim(names[0].replace(";", "\\;"), '\\');
String subMotd = names.length > 1 ? Utils.rtrim(names[1].replace(";", "\\;"), '\\') : "";
StringJoiner joiner = new StringJoiner(";")
.add("MCPE")
.add(motd)
.add(Integer.toString(ProtocolInfo.CURRENT_PROTOCOL))
.add(ProtocolInfo.MINECRAFT_VERSION_NETWORK)
.add(Integer.toString(info.getPlayerCount()))
.add(Integer.toString(info.getMaxPlayerCount()))
.add(Long.toString(this.raknet.getGuid()))
.add(subMotd)
.add(Server.getGamemodeString(this.server.getDefaultGamemode(), true))
.add("1");
this.advertisement = joiner.toString().getBytes(StandardCharsets.UTF_8);
}
@Override
public Integer putPacket(Player player, DataPacket packet) {
return this.putPacket(player, packet, false);
}
@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK) {
return this.putPacket(player, packet, needACK, false);
}
@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK, boolean immediate) {
RakNetPlayerSession session = this.sessions.get(player.getRawSocketAddress());
if (session != null) {
if (!immediate) {
session.sendPacket(packet);
} else {
packet.tryEncode();
session.sendPacketImmediately(packet);
}
}
return null;
}
@Override
public boolean onConnectionRequest(InetSocketAddress address, InetSocketAddress realAddress) {
return true;
}
@Override
public byte[] onQuery(InetSocketAddress inetSocketAddress) {
return this.advertisement;
}
@Override
public void onSessionCreation(RakNetServerSession session) {
// We need to make sure this gets put into the correct thread local hashmap
// for ticking or race conditions will occur.
session.setMaximumStaleDatagrams(server.getMaximumStaleDatagrams());
if (session.getEventLoop().inEventLoop()) {
this.onSessionCreation0(session);
} else {
session.getEventLoop().execute(() -> this.onSessionCreation0(session));
}
}
private void onSessionCreation0(RakNetServerSession session) {
RakNetPlayerSession nukkitSession = new RakNetPlayerSession(this, session);
session.setListener(nukkitSession);
this.sessionCreationQueue.offer(nukkitSession);
}
@Override
public void onUnhandledDatagram(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
this.server.handlePacket(datagramPacket.sender(), datagramPacket.content());
}
@PowerNukkitOnly
@Since("1.5.2.0-PN")
@Override
public Integer putResourcePacket(Player player, DataPacket packet) {
RakNetPlayerSession session = this.sessions.get(player.getRawSocketAddress());
if (session != null) {
packet.tryEncode();
session.sendResourcePacket(packet.clone());
}
return null;
}
public Network getNetwork() {
return this.network;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy