Skip to content

Commit

Permalink
Cleanup some old code
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jan 30, 2021
1 parent 4fa91db commit 6532137
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.util.Map;

/**
* Message Pack Handler Class
* MessagePack Data Conversion Class
*/
public class MessagePackHandler {
public class MessageData {

/**
* Convert an ObjectMap to a MessagePack Map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Client.DataSender;
import net.ME1312.SubData.Client.Library.MessagePackHandler;
import net.ME1312.SubData.Client.Library.MessageData;

import org.msgpack.core.MessageInsufficientBufferException;
import org.msgpack.core.MessagePack;
Expand All @@ -29,7 +29,7 @@ public interface MessageObjectIn<K> extends MessageStreamIn {
@Override
default void receive(DataSender sender, InputStream data) throws Throwable {
try (MessageUnpacker msg = MessagePack.newDefaultUnpacker(data)) {
receive(sender, MessagePackHandler.unpack(msg.unpackValue().asMapValue()));
receive(sender, MessageData.unpack(msg.unpackValue().asMapValue()));
} catch (MessageInsufficientBufferException e) {
receive(sender, (ObjectMap<K>) null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Client.DataSender;
import net.ME1312.SubData.Client.Library.MessagePackHandler;
import net.ME1312.SubData.Client.Library.MessageData;

import org.msgpack.core.MessagePack;

Expand All @@ -26,7 +26,7 @@ public interface MessageObjectOut<K> extends MessageStreamOut {
@Override
default void send(DataSender sender, OutputStream data) throws Throwable {
ObjectMap<K> output = send(sender);
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessagePackHandler.pack(output)).close();
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessageData.pack(output)).close();
else data.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package net.ME1312.SubData.Client.Library;

/**
* Unsigned Number Handler Class
* Unsigned Data Conversion Class
*/
public class UnsignedDataHandler {
public class UnsignedData {

/**
* To Unsigned Bytes
* Convert Signed Number to Unsigned Bytes
*
* @param number Number
* @param length Array Length
* @return Byte Array
*/
public static byte[] toUnsigned(long number, int length) {
public static byte[] unsign(long number, int length) {
if (number < 0) throw new IllegalArgumentException("Unsigned numbers may not be less than zero: " + number);
if (length <= 0) length = 1;
byte[] unsigned = new byte[length];
Expand All @@ -23,12 +23,12 @@ public static byte[] toUnsigned(long number, int length) {
}

/**
* To Number
* Convert from Unsigned Bytes to Signed Number
*
* @param bytes Byte Array
* @return Number
*/
public static long fromUnsigned(byte... bytes) {
public static long resign(byte... bytes) {
long signed = 0;
for (int i = bytes.length; i > 0; i--) {
signed += (bytes[i - 1] & 0xFF) << ((bytes.length - i) * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ public void send(SubDataSender sender, OutputStream data) throws Throwable {

@Override
public void receive(SubDataSender sender, InputStream in) throws Throwable {
ByteArrayOutputStream pending = new ByteArrayOutputStream();
byte[] pending = new byte[8];
long id_p1 = -1, id_p2 = -1;

int b, position = 0;
while (position < 16 && (b = in.read()) != -1) {
position++;
pending.write(b);
pending[position++ % 8] = (byte) b;
switch (position) {
case 8:
id_p1 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
id_p1 = ByteBuffer.wrap(pending).order(ByteOrder.BIG_ENDIAN).getLong();
break;
case 16:
id_p2 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
id_p2 = ByteBuffer.wrap(pending).order(ByteOrder.BIG_ENDIAN).getLong();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Packet Message Sending Class
*/
public class PacketSendMessage implements Forwardable, PacketStreamOut {
private MessageOut message;
private final MessageOut message;

/**
* PacketSendMessage (Out)
Expand Down Expand Up @@ -48,6 +48,11 @@ public void send(SubDataSender sender, OutputStream data) throws Throwable {
else data.close();
}

@Override
public void sending(SubDataSender sender) throws Throwable {
message.sending(sender);
}

@Override
public int version() {
return 0x0001;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.ME1312.SubData.Client.Protocol;

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Client.Library.MessagePackHandler;
import net.ME1312.SubData.Client.Library.MessageData;
import net.ME1312.SubData.Client.SubDataSender;

import org.msgpack.core.MessageInsufficientBufferException;
Expand Down Expand Up @@ -29,7 +29,7 @@ public interface PacketObjectIn<K> extends PacketStreamIn {
@Override
default void receive(SubDataSender sender, InputStream data) throws Throwable {
try (MessageUnpacker msg = MessagePack.newDefaultUnpacker(data)) {
receive(sender, MessagePackHandler.unpack(msg.unpackValue().asMapValue()));
receive(sender, MessageData.unpack(msg.unpackValue().asMapValue()));
} catch (MessageInsufficientBufferException e) {
receive(sender, (ObjectMap<K>) null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.ME1312.SubData.Client.Protocol;

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Client.Library.MessagePackHandler;
import net.ME1312.SubData.Client.Library.MessageData;
import net.ME1312.SubData.Client.SubDataSender;

import org.msgpack.core.MessagePack;
Expand All @@ -26,7 +26,7 @@ public interface PacketObjectOut<K> extends PacketStreamOut {
@Override
default void send(SubDataSender sender, OutputStream data) throws Throwable {
ObjectMap<K> output = send(sender);
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessagePackHandler.pack(output)).close();
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessageData.pack(output)).close();
else data.close();
}
}
15 changes: 6 additions & 9 deletions Client/src/net/ME1312/SubData/Client/SubDataClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,18 @@ public void run() {
private void read(SubDataSender sender, Container<Boolean> reset, InputStream data) {
try {
// Step 3 // Read the Packet Metadata
ByteArrayOutputStream pending = new ByteArrayOutputStream();
byte[] pending = new byte[2];
int id = -1, version = -1;

int b, position = 0;
while (position < 4 && (b = data.read()) != -1) {
position++;
pending.write(b);
pending[position++ % 2] = (byte) b;
switch (position) {
case 2:
id = (int) UnsignedDataHandler.fromUnsigned(pending.toByteArray());
pending.reset();
id = (int) UnsignedData.resign(pending);
break;
case 4:
version = (int) UnsignedDataHandler.fromUnsigned(pending.toByteArray());
pending.reset();
version = (int) UnsignedData.resign(pending);
break;
}
}
Expand Down Expand Up @@ -268,8 +265,8 @@ public void close() throws IOException {
if (!pOut.keySet().contains(next.getClass())) throw new IllegalMessageException(address.toString() + ": Could not find ID for packet: " + next.getClass().getCanonicalName());
if (next.version() > 65535 || next.version() < 0) throw new IllegalMessageException(address.toString() + ": Packet version is not in range (0x0000 to 0xFFFF): " + next.getClass().getCanonicalName());

data.write(UnsignedDataHandler.toUnsigned((long) pOut.get(next.getClass()), 2));
data.write(UnsignedDataHandler.toUnsigned((long) next.version(), 2));
data.write(UnsignedData.unsign((long) pOut.get(next.getClass()), 2));
data.write(UnsignedData.unsign((long) next.version(), 2));
data.flush();

// Step 3 // Invoke the Packet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.util.Map;

/**
* Message Pack Handler Class
* MessagePack Data Conversion Class
*/
public class MessagePackHandler {
public class MessageData {

/**
* Convert an ObjectMap to a MessagePack Map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Server.DataClient;
import net.ME1312.SubData.Server.Library.MessagePackHandler;
import net.ME1312.SubData.Server.Library.MessageData;

import org.msgpack.core.MessageInsufficientBufferException;
import org.msgpack.core.MessagePack;
Expand All @@ -29,7 +29,7 @@ public interface MessageObjectIn<K> extends MessageStreamIn {
@Override
default void receive(DataClient client, InputStream data) throws Throwable {
try (MessageUnpacker msg = MessagePack.newDefaultUnpacker(data)) {
receive(client, MessagePackHandler.unpack(msg.unpackValue().asMapValue()));
receive(client, MessageData.unpack(msg.unpackValue().asMapValue()));
} catch (MessageInsufficientBufferException e) {
receive(client, (ObjectMap<K>) null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Server.DataClient;
import net.ME1312.SubData.Server.Library.MessagePackHandler;
import net.ME1312.SubData.Server.Library.MessageData;

import org.msgpack.core.MessagePack;

Expand All @@ -26,7 +26,7 @@ public interface MessageObjectOut<K> extends MessageStreamOut {
@Override
default void send(DataClient client, OutputStream data) throws Throwable {
ObjectMap<K> output = send(client);
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessagePackHandler.pack(output)).close();
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessageData.pack(output)).close();
else data.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package net.ME1312.SubData.Server.Library;

/**
* Unsigned Number Handler Class
* Unsigned Data Conversion Class
*/
public class UnsignedDataHandler {
public class UnsignedData {

/**
* To Unsigned Bytes
* Convert Signed Number to Unsigned Bytes
*
* @param number Number
* @param length Array Length
* @return Byte Array
*/
public static byte[] toUnsigned(long number, int length) {
public static byte[] unsign(long number, int length) {
if (number < 0) throw new IllegalArgumentException("Unsigned numbers may not be less than zero: " + number);
if (length <= 0) length = 1;
byte[] unsigned = new byte[length];
Expand All @@ -23,12 +23,12 @@ public static byte[] toUnsigned(long number, int length) {
}

/**
* To Number
* Convert from Unsigned Bytes to Signed Number
*
* @param bytes Byte Array
* @return Number
*/
public static long fromUnsigned(byte... bytes) {
public static long resign(byte... bytes) {
long signed = 0;
for (int i = bytes.length; i > 0; i--) {
signed += (bytes[i - 1] & 0xFF) << ((bytes.length - i) * 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,18 @@ public void send(SubDataClient client, OutputStream out) throws Throwable {

@Override
public void receive(SubDataClient client, InputStream in) throws Throwable {
ByteArrayOutputStream pending = new ByteArrayOutputStream();
byte[] pending = new byte[8];
long id_p1 = -1, id_p2 = -1;

int b, position = 0;
while (position < 16 && (b = in.read()) != -1) {
position++;
pending.write(b);
pending[position++ % 8] = (byte) b;
switch (position) {
case 8:
id_p1 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
id_p1 = ByteBuffer.wrap(pending).order(ByteOrder.BIG_ENDIAN).getLong();
break;
case 16:
id_p2 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
id_p2 = ByteBuffer.wrap(pending).order(ByteOrder.BIG_ENDIAN).getLong();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Packet Message Sending Class
*/
public class PacketSendMessage implements PacketStreamOut {
private MessageOut message;
private final MessageOut message;

/**
* PacketSendMessage (Out)
Expand Down Expand Up @@ -47,6 +47,11 @@ public void send(SubDataClient client, OutputStream data) throws Throwable {
else data.close();
}

@Override
public void sending(SubDataClient client) throws Throwable {
message.sending(client);
}

@Override
public int version() {
return 0x0001;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.ME1312.SubData.Server.Protocol;

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Server.Library.MessagePackHandler;
import net.ME1312.SubData.Server.Library.MessageData;
import net.ME1312.SubData.Server.SubDataClient;

import org.msgpack.core.MessageInsufficientBufferException;
Expand All @@ -27,7 +27,7 @@ public interface PacketObjectIn<K> extends PacketStreamIn {
@Override
default void receive(SubDataClient client, InputStream data) throws Throwable {
try (MessageUnpacker msg = MessagePack.newDefaultUnpacker(data)) {
receive(client, MessagePackHandler.unpack(msg.unpackValue().asMapValue()));
receive(client, MessageData.unpack(msg.unpackValue().asMapValue()));
} catch (MessageInsufficientBufferException e) {
receive(client, (ObjectMap<K>) null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.ME1312.SubData.Server.Protocol;

import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.SubData.Server.Library.MessagePackHandler;
import net.ME1312.SubData.Server.Library.MessageData;
import net.ME1312.SubData.Server.SubDataClient;

import org.msgpack.core.MessagePack;
Expand All @@ -24,7 +24,7 @@ public interface PacketObjectOut<K> extends PacketStreamOut {
@Override
default void send(SubDataClient client, OutputStream data) throws Throwable {
ObjectMap<K> output = send(client);
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessagePackHandler.pack(output)).close();
if (output != null) MessagePack.newDefaultPacker(data).packValue(MessageData.pack(output)).close();
else data.close();
}
}
Loading

0 comments on commit 6532137

Please sign in to comment.