/
PacketForwardPacket.java
71 lines (62 loc) · 2.2 KB
/
PacketForwardPacket.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
package net.ME1312.SubData.Server.Protocol.Internal;
import net.ME1312.SubData.Server.Protocol.PacketStreamIn;
import net.ME1312.SubData.Server.Protocol.PacketStreamOut;
import net.ME1312.SubData.Server.SubDataClient;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;
/**
* Packet Forward Packet
*/
public class PacketForwardPacket implements PacketStreamIn, PacketStreamOut {
private InputStream in;
/**
* New PacketForwardPacket (Out)
*
* @param in Packet stream to forward
*/
public PacketForwardPacket(InputStream in) {
this.in = in;
}
@Override
public void send(SubDataClient client, OutputStream out) throws Throwable {
int b;
while ((b = in.read()) != -1) out.write(b);
out.close();
}
@Override
public void receive(SubDataClient client, InputStream in) throws Throwable {
ByteArrayOutputStream pending = new ByteArrayOutputStream();
long id_p1 = -1, id_p2 = -1;
int b, position = 0;
while (position < 16 && (b = in.read()) != -1) {
position++;
pending.write(b);
switch (position) {
case 8:
id_p1 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
break;
case 16:
id_p2 = ByteBuffer.wrap(pending.toByteArray()).order(ByteOrder.BIG_ENDIAN).getLong();
pending.reset();
break;
}
}
if (position >= 16) {
UUID id = new UUID(id_p1, id_p2);
if (client.getServer().getClient(id) != null) {
client.getServer().getClient(id).sendPacket(new PacketForwardPacket(in));
} else throw new IllegalArgumentException("Cannot forward to invalid Client ID: [" + id.toString() + "]");
} else {
throw new IllegalArgumentException("Invalid UUID data for Client ID: [" + id_p1 + ", " + id_p2 + "]");
}
}
@Override
public int version() {
return 0x0001;
}
}