Search in sources :

Example 1 with CompressBatchedTask

use of cn.nukkit.network.CompressBatchedTask in project Nukkit by Nukkit.

the class Server method batchPackets.

public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSync) {
    if (players == null || packets == null || players.length == 0 || packets.length == 0) {
        return;
    }
    Timings.playerNetworkSendTimer.startTiming();
    byte[][] payload = new byte[packets.length * 2][];
    int size = 0;
    for (int i = 0; i < packets.length; i++) {
        DataPacket p = packets[i];
        if (!p.isEncoded) {
            p.encode();
        }
        byte[] buf = p.getBuffer();
        payload[i * 2] = Binary.writeUnsignedVarInt(buf.length);
        payload[i * 2 + 1] = buf;
        packets[i] = null;
        size += payload[i * 2].length;
        size += payload[i * 2 + 1].length;
    }
    List<String> targets = new ArrayList<>();
    for (Player p : players) {
        if (p.isConnected()) {
            targets.add(this.identifier.get(p.rawHashCode()));
        }
    }
    if (!forceSync && this.networkCompressionAsync) {
        this.getScheduler().scheduleAsyncTask(new CompressBatchedTask(payload, targets, this.networkCompressionLevel));
    } else {
        try {
            byte[] data = Binary.appendBytes(payload);
            this.broadcastPacketsCallback(Zlib.deflate(data, this.networkCompressionLevel), targets);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    Timings.playerNetworkSendTimer.stopTiming();
}
Also used : CompressBatchedTask(cn.nukkit.network.CompressBatchedTask) DataPacket(cn.nukkit.network.protocol.DataPacket)

Aggregations

CompressBatchedTask (cn.nukkit.network.CompressBatchedTask)1 DataPacket (cn.nukkit.network.protocol.DataPacket)1