I have the following script:
Connection con = new Connection("host",80);
ByteBuffer loginbb = ByteBuffer.allocate(300);
<snip>
loginbb.flip();
byte[]login = new byte[loginbb.remaining()];
loginbb.get(login);
ByteBuffer headerbb = ByteBuffer.allocate(7);
<snip>
headerbb.flip();
byte[]header = new byte[headerbb.remaining()];
headerbb.get(header);
con.run(login, header);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
long pid = Long.parseLong(request.getParameter("player"));
PrintWriter out = response.getWriter();
ByteBuffer bb = ByteBuffer.allocate(100);
bb.putLong(pid);
bb.putLong(pid);
bb.put((byte) 0x00);
bb.flip();
byte[] payload = new byte[bb.remaining()];
bb.get(payload);
ByteBuffer headerbb2 = ByteBuffer.allocate(7);
<snip>
headerbb2.flip();
byte[]header2 = new byte[headerbb2.remaining()];
headerbb2.get(header2);
con.send(payload, header2);
try {
Thread.sleep(700);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(Avatar.getJson().toString());
String json = gson.toJson(je);
out.flush();
out.println(json);
out.flush();
Notice the following?
try {
Thread.sleep(700);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
and this:
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
So that is pausing my script for some time but I don't want it like that as the script may take longer or less time. That would be a waste of some time D:
What I want to happen is that it just waits for the stuff to finish.
con.run basically does an initial task and then a con.send.
con.send basically runs this:
private void sendToServer(byte[] message, int length, byte[]header) {
byte[]payload = outrc4.encrypt(message,false);
byte[] data = new byte[header.length+payload.length];
System.arraycopy(header,0,data,0,header.length);
System.arraycopy(payload,0,data,header.length,payload.length);
try {
out.write(data);
} catch (IOException e) {
System.out.println("Error!");
}
}
It just sends the packet to the server.
From here, on the con.run I get multiple packets back. I can catch the ID of the packet and in the loop that receives and parses the packet I can add an if statement which checks if the loginDone packet is received.
void receive() {
try {
while (in.available() > -1) {
int type = in.readUnsignedShort();
int size = in.readUnsignedByte();
size <<= 8;
size |= in.readUnsignedByte();
size <<= 8;
size |= in.readUnsignedByte();
int version = in.readUnsignedShort();
byte array[] = new byte[7];
array[0] = (byte)((type >>> 8) & 0xFF);
array[1] = (byte)(type & 0xFF);
array[2] = (byte)((size >>> 16) & 0xFF);
array[3] = (byte)((size >>> 8) & 0xFF);
array[4] = (byte)(size & 0xFF);
array[5] = (byte)((version >>> 8) & 0xFF);
array[6] = (byte)(version & 0xFF);
final byte[] reply = new byte[size];
in.readFully(reply,0,size);
byte[] data = new byte[array.length + reply.length];
System.arraycopy(array, 0, data, 0, array.length);
System.arraycopy(reply, 0, data, array.length, reply.length);
byte[] decryptedPayload = inrc4.encrypt(reply,false);
if(type == 20000){
updateKeys(decryptedPayload);
}
if(type == 24411){
//this one means that the 1st packet that uses con.run is done
System.out.println("Logged In!");
}
if(type == 24334){
//this one means that the 2nd packet that uses con.send is done
InputStream myis = new ByteArrayInputStream(decryptedPayload);
new Avatar(myis);
t.interrupt();
}
}
} catch (Exception e) {}
}
Take not of these comments: //this one means that the 2nd packet that uses con.send is done and //this one means that the 1st packet that uses con.run is done
This is as far as I have got. Does anyone have any idea on what I should do from here?