Search in sources :

Example 1 with DataPacket

use of cn.nukkit.network.protocol.DataPacket 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)

Example 2 with DataPacket

use of cn.nukkit.network.protocol.DataPacket in project Nukkit by Nukkit.

the class BoneMealParticle method encode.

@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = LevelEventPacket.EVENT_PARTICLE_BONEMEAL;
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = 0;
    return new DataPacket[] { pk };
}
Also used : LevelEventPacket(cn.nukkit.network.protocol.LevelEventPacket) DataPacket(cn.nukkit.network.protocol.DataPacket)

Example 3 with DataPacket

use of cn.nukkit.network.protocol.DataPacket in project Nukkit by Nukkit.

the class GenericParticle method encode.

@Override
public DataPacket[] encode() {
    LevelEventPacket pk = new LevelEventPacket();
    pk.evid = (short) (LevelEventPacket.EVENT_ADD_PARTICLE_MASK | this.id);
    pk.x = (float) this.x;
    pk.y = (float) this.y;
    pk.z = (float) this.z;
    pk.data = this.data;
    return new DataPacket[] { pk };
}
Also used : LevelEventPacket(cn.nukkit.network.protocol.LevelEventPacket) DataPacket(cn.nukkit.network.protocol.DataPacket)

Example 4 with DataPacket

use of cn.nukkit.network.protocol.DataPacket in project Nukkit by Nukkit.

the class RakNetInterface method getPacket.

private DataPacket getPacket(byte[] buffer) {
    int start = 0;
    if (buffer[0] == (byte) 0xfe) {
        start++;
    }
    DataPacket data = this.network.getPacket(ProtocolInfo.BATCH_PACKET);
    if (data == null) {
        return null;
    }
    data.setBuffer(buffer, start);
    return data;
}
Also used : PING_DataPacket(cn.nukkit.raknet.protocol.packet.PING_DataPacket) DataPacket(cn.nukkit.network.protocol.DataPacket)

Example 5 with DataPacket

use of cn.nukkit.network.protocol.DataPacket in project Nukkit by Nukkit.

the class RakNetInterface method putPacket.

@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK, boolean immediate) {
    if (this.identifiers.containsKey(player.rawHashCode())) {
        byte[] buffer;
        if (packet.pid() == ProtocolInfo.BATCH_PACKET) {
            buffer = ((BatchPacket) packet).payload;
        } else if (!needACK) {
            this.server.batchPackets(new Player[] { player }, new DataPacket[] { packet }, true);
            return null;
        } else {
            if (!packet.isEncoded) {
                packet.encode();
                packet.isEncoded = true;
            }
            buffer = packet.getBuffer();
            try {
                buffer = Zlib.deflate(Binary.appendBytes(Binary.writeUnsignedVarInt(buffer.length), buffer), Server.getInstance().networkCompressionLevel);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        String identifier = this.identifiers.get(player.rawHashCode());
        EncapsulatedPacket pk = null;
        if (!needACK) {
            if (packet.encapsulatedPacket == null) {
                packet.encapsulatedPacket = new CacheEncapsulatedPacket();
                packet.encapsulatedPacket.identifierACK = null;
                packet.encapsulatedPacket.buffer = Binary.appendBytes((byte) 0xfe, buffer);
                if (packet.getChannel() != 0) {
                    packet.encapsulatedPacket.reliability = 3;
                    packet.encapsulatedPacket.orderChannel = packet.getChannel();
                    packet.encapsulatedPacket.orderIndex = 0;
                } else {
                    packet.encapsulatedPacket.reliability = 2;
                }
            }
            pk = packet.encapsulatedPacket;
        }
        if (pk == null) {
            pk = new EncapsulatedPacket();
            pk.buffer = Binary.appendBytes((byte) 0xfe, buffer);
            if (packet.getChannel() != 0) {
                packet.reliability = 3;
                packet.orderChannel = packet.getChannel();
                packet.orderIndex = 0;
            } else {
                packet.reliability = 2;
            }
            if (needACK) {
                int iACK = this.identifiersACK.get(identifier);
                iACK++;
                pk.identifierACK = iACK;
                this.identifiersACK.put(identifier, iACK);
            }
        }
        this.handler.sendEncapsulated(identifier, pk, (needACK ? RakNet.FLAG_NEED_ACK : 0) | (immediate ? RakNet.PRIORITY_IMMEDIATE : RakNet.PRIORITY_NORMAL));
        return pk.identifierACK;
    }
    return null;
}
Also used : Player(cn.nukkit.Player) EncapsulatedPacket(cn.nukkit.raknet.protocol.EncapsulatedPacket) PING_DataPacket(cn.nukkit.raknet.protocol.packet.PING_DataPacket) DataPacket(cn.nukkit.network.protocol.DataPacket) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DataPacket (cn.nukkit.network.protocol.DataPacket)11 LevelEventPacket (cn.nukkit.network.protocol.LevelEventPacket)6 PING_DataPacket (cn.nukkit.raknet.protocol.packet.PING_DataPacket)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Player (cn.nukkit.Player)1 EntityMetadata (cn.nukkit.entity.data.EntityMetadata)1 CompressBatchedTask (cn.nukkit.network.CompressBatchedTask)1 AddPlayerPacket (cn.nukkit.network.protocol.AddPlayerPacket)1 RemoveEntityPacket (cn.nukkit.network.protocol.RemoveEntityPacket)1 EncapsulatedPacket (cn.nukkit.raknet.protocol.EncapsulatedPacket)1 MainLogger (cn.nukkit.utils.MainLogger)1 ArrayList (java.util.ArrayList)1