From cae491bc202835ee8a10c427e0b57751dc04f232 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 22 Nov 2016 21:32:18 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20nouveau=20packet=20pour=20Pandac?= =?UTF-8?q?ubeCore=20+=20Gestion=20d'octet=20bit=20=C3=A0=20bit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../network/packet/bytebuffer/Array8Bit.java | 58 +++++++++++++++++++ .../packets/global/PacketServerCommand.java | 30 ++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/fr/pandacube/java/util/network/packet/bytebuffer/Array8Bit.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCommand.java diff --git a/src/fr/pandacube/java/util/network/packet/bytebuffer/Array8Bit.java b/src/fr/pandacube/java/util/network/packet/bytebuffer/Array8Bit.java new file mode 100644 index 0000000..07232f4 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/bytebuffer/Array8Bit.java @@ -0,0 +1,58 @@ +package fr.pandacube.java.util.network.packet.bytebuffer; + +import java.util.Arrays; + +public class Array8Bit { + + public static final int BIT_COUNT = 8; + + private boolean[] values = new boolean[BIT_COUNT]; + + /** + * + * @param b unsigned integer value. Lowest significant bit will be used. + */ + public Array8Bit(int b) { + for (int i = 0; i>= 1; + } + } + + + public Array8Bit(boolean[] bits) { + if (bits == null || bits.length != BIT_COUNT) + throw new IllegalArgumentException("bits is null or bits.length != "+BIT_COUNT); + values = Arrays.copyOf(bits, BIT_COUNT); + } + + + /** + * i = 0 is the lowest significant bit + * @param i + * @return + */ + public boolean getValue(int i) { + return values[i]; + } + + /** + * i = 0 is the lowest significant bit + * @param i + * @param b + */ + public void setValue(int i, boolean b) { + values[i] = b; + } + + + public byte getValuesAsByte() { + byte b = 0; + for (int i=BIT_COUNT-1; i>=0; i--) { + b <<= 1; + if (values[i]) b |= 1; + } + return b; + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCommand.java b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCommand.java new file mode 100644 index 0000000..f74f888 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCommand.java @@ -0,0 +1,30 @@ +package fr.pandacube.java.util.network.packet.packets.global; + +import fr.pandacube.java.util.network.packet.PacketServer; +import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; + +public class PacketServerCommand extends PacketServer { + + private String command; + private boolean async; + private boolean returnResult; + + public PacketServerCommand() { + super((byte)0xD2); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putString(command); + buffer.putByte((byte) (async ? 1 : 0)); + buffer.putByte((byte) (returnResult ? 1 : 0)); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + command = buffer.getString(); + async = buffer.getByte() != 0; + returnResult = buffer.getByte() != 0; + } + +}