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();
}
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 };
}
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 };
}
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;
}
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;
}
Aggregations