mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 08:50:52 +01:00
redo hpacket part 2
This commit is contained in:
parent
bab0d11c59
commit
13b1bd055e
@ -83,7 +83,6 @@ public class PacketStructure {
|
|||||||
}
|
}
|
||||||
return hPacket;
|
return hPacket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String toString(byte[] packet) {
|
public static String toString(byte[] packet) {
|
||||||
StringBuilder teststring = new StringBuilder();
|
StringBuilder teststring = new StringBuilder();
|
||||||
for (byte x : packet) {
|
for (byte x : packet) {
|
||||||
@ -96,37 +95,56 @@ public class PacketStructure {
|
|||||||
return teststring.toString();
|
return teststring.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean structureEquals(HPacket packet, String structure) {
|
// generates an expression for a packet from a packet structure (ex. "i(isi(b))iBd")
|
||||||
|
public static String toExpressionFromGivenStructure(HPacket packet, String struct) {
|
||||||
|
int oldReadIndex = packet.getReadIndex();
|
||||||
|
packet.resetReadIndex();
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("{l}{u:").append(packet.headerId()).append("}");
|
||||||
|
|
||||||
|
buildExpressionFromGivenStructure(packet, struct, 0, builder);
|
||||||
|
packet.setReadIndex(oldReadIndex);
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
private static void buildExpressionFromGivenStructure(HPacket p, String struct, int indexInGivenStruct, StringBuilder builder) {
|
||||||
|
int prevInt = 0;
|
||||||
|
|
||||||
|
while (indexInGivenStruct < struct.length()) {
|
||||||
|
char c = struct.charAt(indexInGivenStruct++);
|
||||||
|
if (c == '(') {
|
||||||
|
for (int i = 0; i < prevInt; i++) buildExpressionFromGivenStructure(p, struct, indexInGivenStruct, builder);
|
||||||
|
int skipping = 1;
|
||||||
|
while (skipping > 0) {
|
||||||
|
char c2 = struct.charAt(indexInGivenStruct++);
|
||||||
|
if (c2 == '(') skipping++;
|
||||||
|
else if (c2 == ')') skipping--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == 'i') builder.append("{i:").append(prevInt = p.readInteger()).append('}');
|
||||||
|
else if (c == 's') builder.append("{s:\"").append(p.readString().replace("\"", "\\\"")).append("\"}");
|
||||||
|
else if (c == 'd') builder.append("{d:").append(p.readDouble()).append('}');
|
||||||
|
else if (c == 'b') builder.append("{b:").append(p.readByte()).append('}');
|
||||||
|
else if (c == 'B') builder.append("{b:").append(p.readBoolean()).append('}');
|
||||||
|
else return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean structureEquals(HPacket packet, String struct) {
|
||||||
if (packet.isCorrupted()) return false;
|
if (packet.isCorrupted()) return false;
|
||||||
|
|
||||||
int indexbuffer = packet.getReadIndex();
|
int indexbuffer = packet.getReadIndex();
|
||||||
packet.setReadIndex(6);
|
packet.resetReadIndex();
|
||||||
|
|
||||||
String[] split = structure.split(",");
|
boolean result;
|
||||||
|
try {
|
||||||
for (int i = 0; i < split.length; i++) {
|
buildExpressionFromGivenStructure(packet, struct, 0, new StringBuilder());
|
||||||
String s = split[i];
|
result = packet.isEOF() == 1;
|
||||||
|
}
|
||||||
if (s.equals("s")) {
|
catch (Exception e) {
|
||||||
if (packet.getReadIndex() + 2 > packet.getBytesLength() ||
|
result = false;
|
||||||
packet.readUshort(packet.getReadIndex()) + 2 + packet.getReadIndex() > packet.getBytesLength()) return false;
|
|
||||||
packet.readString();
|
|
||||||
}
|
|
||||||
else if (s.equals("i")) {
|
|
||||||
if (packet.getReadIndex() + 4 > packet.getBytesLength()) return false;
|
|
||||||
packet.readInteger();
|
|
||||||
}
|
|
||||||
else if (s.equals("u")) {
|
|
||||||
if (packet.getReadIndex() + 2 > packet.getBytesLength()) return false;
|
|
||||||
packet.readUshort();
|
|
||||||
}
|
|
||||||
else if (s.equals("b")) {
|
|
||||||
if (packet.getReadIndex() + 1 > packet.getBytesLength()) return false;
|
|
||||||
packet.readBoolean();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean result = (packet.isEOF() == 1);
|
|
||||||
packet.setReadIndex(indexbuffer);
|
packet.setReadIndex(indexbuffer);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -138,6 +156,10 @@ public class PacketStructure {
|
|||||||
HPacket p2 = fromString("{l}{u:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}");
|
HPacket p2 = fromString("{l}{u:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}");
|
||||||
System.out.println(p2);
|
System.out.println(p2);
|
||||||
|
|
||||||
|
System.out.println(structureEquals(
|
||||||
|
new HPacket("{l}{u:5}{s:\"asdas\"}"),
|
||||||
|
"s"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -508,42 +508,6 @@ public class HPacket implements StringifyAble {
|
|||||||
isEdited = edited;
|
isEdited = edited;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String toExpressionFromGivenStructure(String struct) {
|
|
||||||
int oldReadIndex = readIndex;
|
|
||||||
resetReadIndex();
|
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append("{l}{u:").append(headerId()).append("}");
|
|
||||||
|
|
||||||
buildExpressionFromGivenStructure(struct, 0, builder);
|
|
||||||
readIndex = oldReadIndex;
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void buildExpressionFromGivenStructure(String struct, int indexInGivenStruct, StringBuilder builder) {
|
|
||||||
int prevInt = 0;
|
|
||||||
|
|
||||||
while (indexInGivenStruct < struct.length()) {
|
|
||||||
char c = struct.charAt(indexInGivenStruct++);
|
|
||||||
if (c == '(') {
|
|
||||||
for (int i = 0; i < prevInt; i++) buildExpressionFromGivenStructure(struct, indexInGivenStruct, builder);
|
|
||||||
int skipping = 1;
|
|
||||||
while (skipping > 0) {
|
|
||||||
char c2 = struct.charAt(indexInGivenStruct++);
|
|
||||||
if (c2 == '(') skipping++;
|
|
||||||
else if (c2 == ')') skipping--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (c == 'i') builder.append("{i:").append(prevInt = readInteger()).append('}');
|
|
||||||
else if (c == 's') builder.append("{s:\"").append(readString().replace("\"", "\\\"")).append("\"}");
|
|
||||||
else if (c == 'd') builder.append("{d:").append(readDouble()).append('}');
|
|
||||||
else if (c == 'b') builder.append("{b:").append(readByte()).append('}');
|
|
||||||
else if (c == 'B') builder.append("{b:").append(readBoolean()).append('}');
|
|
||||||
else return; // ')'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toExpression(HMessage.Direction direction) {
|
public String toExpression(HMessage.Direction direction) {
|
||||||
if (isCorrupted()) return "";
|
if (isCorrupted()) return "";
|
||||||
|
|
||||||
@ -551,7 +515,7 @@ public class HPacket implements StringifyAble {
|
|||||||
if (HarbleAPIFetcher.HARBLEAPI != null &&
|
if (HarbleAPIFetcher.HARBLEAPI != null &&
|
||||||
((msg = HarbleAPIFetcher.HARBLEAPI.getHarbleMessageFromHeaderId(direction, headerId())) != null)) {
|
((msg = HarbleAPIFetcher.HARBLEAPI.getHarbleMessageFromHeaderId(direction, headerId())) != null)) {
|
||||||
if (msg.getStructure() != null) {
|
if (msg.getStructure() != null) {
|
||||||
return toExpressionFromGivenStructure(msg.getStructure());
|
return PacketStructure.toExpressionFromGivenStructure(this, msg.getStructure());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toExpression();
|
return toExpression();
|
||||||
@ -852,7 +816,7 @@ public class HPacket implements StringifyAble {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
HPacket packet = new HPacket("{l}{u:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}{d:1.4}");
|
HPacket packet = new HPacket("{l}{u:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}{d:1.4}");
|
||||||
|
|
||||||
String str = packet.toExpressionFromGivenStructure("i(isi(b))iBd");
|
String str = PacketStructure.toExpressionFromGivenStructure(packet, "i(isi(b))iBd");
|
||||||
|
|
||||||
HPacket packetverify = new HPacket(str);
|
HPacket packetverify = new HPacket(str);
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ public class IncomingPacketHandler extends PacketHandler {
|
|||||||
TrafficListener listener = new TrafficListener() {
|
TrafficListener listener = new TrafficListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCapture(HMessage message) {
|
public void onCapture(HMessage message) {
|
||||||
if (isDataStream && message.getPacket().structureEquals("s,b") && message.getPacket().length() > 500) {
|
if (isDataStream && message.getPacket().structureEquals("sb") && message.getPacket().length() > 500) {
|
||||||
((Observable<TrafficListener>)trafficObservables[0]).removeListener(this);
|
((Observable<TrafficListener>)trafficObservables[0]).removeListener(this);
|
||||||
HPacket packet = message.getPacket();
|
HPacket packet = message.getPacket();
|
||||||
packet.readString();
|
packet.readString();
|
||||||
|
Loading…
Reference in New Issue
Block a user