/
PacketRecieveMessage.java
73 lines (65 loc) · 3.39 KB
/
PacketRecieveMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package net.ME1312.SubData.Client.Protocol.Internal;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubData.Client.DataProtocol;
import net.ME1312.SubData.Client.Library.Exception.IllegalMessageException;
import net.ME1312.SubData.Client.Library.Exception.IllegalSenderException;
import net.ME1312.SubData.Client.Library.ForwardedDataSender;
import net.ME1312.SubData.Client.Protocol.*;
import net.ME1312.SubData.Client.SubDataClient;
import net.ME1312.SubData.Client.SubDataSender;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
/**
* Packet Message Retrieval Class
*/
public class PacketRecieveMessage implements Forwardable, PacketStreamIn {
@Override
public void receive(SubDataSender sender, InputStream data) throws Throwable {
ByteArrayOutputStream pending = new ByteArrayOutputStream();
String channel = null, handle = null;
// Parse Message Metadata
boolean escaped = false;
int b, state = 0;
while (state < 2 && (b = data.read()) != -1) {
if (escaped) {
switch (b) {
case '\u001B': // [ESC] (Escape character)
pending.write('\u001B');
break;
case '\u0003': // [ETX] (End of String character)
switch (state) {
case 0:
channel = new String(pending.toByteArray(), StandardCharsets.UTF_8);
break;
case 1:
handle = new String(pending.toByteArray(), StandardCharsets.UTF_8);
break;
}
pending.reset();
state++;
break;
default:
pending.write('\u001B');
pending.write(b);
break;
}
escaped = false;
} else if (b == '\u001B') {
escaped = true;
} else {
pending.write(b);
}
}
HashMap<String, HashMap<String, MessageIn>> mIn = Util.reflect(DataProtocol.class.getDeclaredField("mIn"), sender.getProtocol());
if (Util.isNull(channel, handle)) throw new IllegalMessageException("Incomplete Message Metadata: [" + ((channel == null)?"null":"\""+channel+"\"") + ", " + ((handle == null)?"null":"\""+handle+"\"") + "]");
if (!mIn.keySet().contains(channel) || !mIn.get(channel).keySet().contains(handle)) throw new IllegalMessageException("Could not find handler for message: [\"" + channel + "\", \"" + handle + "\"]");
MessageIn message = mIn.get(channel).get(handle);
if (sender instanceof ForwardedDataSender && !(message instanceof Forwardable)) throw new IllegalSenderException("This handler does not support forwarded messages: [" + message.getClass().getTypeName() + "]");
if (sender instanceof SubDataClient && message instanceof ForwardOnly) throw new IllegalSenderException("This handler does not support non-forwarded messages: [" + message.getClass().getTypeName() + "]");
message.receive(sender);
if (message instanceof MessageStreamIn) ((MessageStreamIn) message).receive(sender, data);
else data.close();
}
}