Skip to content

Commit 30420f1

Browse files
author
jython234
committed
Client part implemented :) (And working!)
1 parent f529d8b commit 30420f1

11 files changed

Lines changed: 757 additions & 53 deletions

File tree

src/main/java/net/beaconpe/jraklib/Binary.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,11 @@ public static byte[] writeLong(long l){
213213
}
214214

215215
public static byte[] subbytes(byte[] bytes, int start, int length){
216-
ByteBuffer bb = ByteBuffer.allocate(length);
217-
int startlength = start + length;
218-
for(int i = start; i < startlength; i++){
219-
bb.put(bytes[i]);
220-
}
221-
return bb.array();
216+
ByteBuffer bb = ByteBuffer.wrap(bytes);
217+
bb.position(start);
218+
byte[] bytes2 = new byte[length];
219+
bb.get(bytes2);
220+
return bytes2;
222221
}
223222

224223
public static byte[] subbytes(byte[] bytes, int start){
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package net.beaconpe.jraklib.client;
2+
3+
import net.beaconpe.jraklib.Binary;
4+
import net.beaconpe.jraklib.JRakLib;
5+
import net.beaconpe.jraklib.protocol.EncapsulatedPacket;
6+
7+
import java.nio.ByteBuffer;
8+
import java.util.Arrays;
9+
10+
/**
11+
* A handler class for handling the client.
12+
*
13+
* @author jython234
14+
*/
15+
public class ClientHandler {
16+
protected JRakLibClient client;
17+
protected ClientInstance instance;
18+
19+
public ClientHandler(JRakLibClient client, ClientInstance instance){
20+
this.client = client;
21+
this.instance = instance;
22+
}
23+
24+
public void sendEncapsulated(EncapsulatedPacket packet){
25+
byte flags = JRakLib.PRIORITY_NORMAL;
26+
sendEncapsulated("", packet, flags);
27+
}
28+
29+
public void sendEncapsulated(String identifier, EncapsulatedPacket packet, byte flags){
30+
ByteBuffer bb = ByteBuffer.allocate(3+identifier.getBytes().length+packet.getTotalLength(true));
31+
bb.put(JRakLib.PACKET_ENCAPSULATED).put((byte) identifier.getBytes().length).put(identifier.getBytes()).put(flags).put(packet.toBinary(true));
32+
client.pushMainToThreadPacket(Arrays.copyOf(bb.array(), bb.position()));
33+
bb = null;
34+
}
35+
36+
public void sendRaw(byte[] payload){
37+
sendRaw(client.getServerIP(), (short) client.getServerPort(), payload);
38+
}
39+
40+
public void sendRaw(String address, short port, byte[] payload){
41+
ByteBuffer bb = ByteBuffer.allocate(4+address.getBytes().length+payload.length);
42+
bb.put(JRakLib.PACKET_RAW).put((byte) address.getBytes().length).put(address.getBytes()).put(Binary.writeShort(port)).put(payload);
43+
client.pushMainToThreadPacket(bb.array());
44+
}
45+
46+
public void sendOption(String name, String value){
47+
ByteBuffer bb = ByteBuffer.allocate(2+name.getBytes().length+value.getBytes().length);
48+
bb.put(JRakLib.PACKET_SET_OPTION).put((byte) name.getBytes().length).put(name.getBytes()).put(value.getBytes());
49+
client.pushMainToThreadPacket(bb.array());
50+
}
51+
52+
public void disconnectFromServer(){
53+
shutdown();
54+
}
55+
56+
public void shutdown(){
57+
client.shutdown();
58+
client.pushMainToThreadPacket(new byte[] {JRakLib.PACKET_SHUTDOWN});
59+
//TODO: Find a way to kill client after sleep.
60+
}
61+
62+
public void emergencyShutdown(){
63+
client.shutdown();
64+
client.pushMainToThreadPacket(new byte[] {0x7f}); //JRakLib::PACKET_EMERGENCY_SHUTDOWN
65+
}
66+
67+
public boolean handlePacket(){
68+
byte[] packet = client.readThreadToMainPacket();
69+
if(packet == null){
70+
return false;
71+
}
72+
if(packet.length > 0){
73+
byte id = packet[0];
74+
int offset = 1;
75+
if(id == JRakLib.PACKET_ENCAPSULATED){
76+
int len = packet[offset++];
77+
String identifier = new String(Binary.subbytes(packet, offset, len));
78+
offset += len;
79+
byte flags = packet[offset++];
80+
byte[] buffer = Binary.subbytes(packet, offset);
81+
instance.handleEncapsulated(EncapsulatedPacket.fromBinary(buffer, true), flags);
82+
} else if(id == JRakLib.PACKET_RAW){
83+
int len = packet[offset++];
84+
String address = new String(Binary.subbytes(packet, offset, len));
85+
offset += len;
86+
int port = Binary.readShort(Binary.subbytes(packet, offset, 2));
87+
offset += 2;
88+
byte[] payload = Binary.subbytes(packet, offset);
89+
instance.handleRaw(payload);
90+
} else if(id == JRakLib.PACKET_SET_OPTION){
91+
int len = packet[offset++];
92+
String name = new String(Binary.subbytes(packet, offset, len));
93+
offset += len;
94+
String value = new String(Binary.subbytes(packet, offset));
95+
instance.handleOption(name, value);
96+
} else if(id == JRakLib.PACKET_OPEN_SESSION){
97+
int len = packet[offset++];
98+
String identifier = new String(Binary.subbytes(packet, offset, len));
99+
offset += len;
100+
long serverID = Binary.readLong(Binary.subbytes(packet, offset, 8));
101+
instance.connectionOpened(serverID);
102+
} else if(id == JRakLib.PACKET_CLOSE_SESSION){
103+
int len = packet[offset++];
104+
String identifier = new String(Binary.subbytes(packet, offset, len));
105+
offset += len;
106+
len = packet[offset++];
107+
String reason = new String(Binary.subbytes(packet, offset, len));
108+
instance.connectionClosed(reason);
109+
}
110+
return true;
111+
}
112+
return false;
113+
}
114+
}

0 commit comments

Comments
 (0)