mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 17:00:52 +01:00
replace InputStream with DataInputStream
This commit is contained in:
parent
1dce738a12
commit
2eef54600a
@ -5,6 +5,7 @@ import main.protocol.HPacket;
|
||||
import main.protocol.packethandler.PayloadBuffer;
|
||||
import main.ui.extensions.Extensions;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@ -50,22 +51,26 @@ public abstract class Extension {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Socket gEarthExtensionServer = null;
|
||||
try {
|
||||
Socket GEarthExtensionServer = new Socket("localhost", port);
|
||||
gEarthExtensionServer = new Socket("localhost", port);
|
||||
|
||||
InputStream in = GEarthExtensionServer.getInputStream();
|
||||
out = GEarthExtensionServer.getOutputStream();
|
||||
InputStream in = gEarthExtensionServer.getInputStream();
|
||||
DataInputStream dIn = new DataInputStream(in);
|
||||
out = gEarthExtensionServer.getOutputStream();
|
||||
|
||||
while (!gEarthExtensionServer.isClosed()) {
|
||||
|
||||
int length = dIn.readInt();
|
||||
byte[] headerandbody = new byte[length + 4];
|
||||
int amountRead = dIn.read(headerandbody, 4, length);
|
||||
|
||||
if (amountRead != length) break;
|
||||
|
||||
HPacket packet = new HPacket(headerandbody);
|
||||
packet.fixLength();
|
||||
|
||||
PayloadBuffer buffer = new PayloadBuffer();
|
||||
while (!GEarthExtensionServer.isClosed()) {
|
||||
if (in.available() > 0) {
|
||||
byte[] data = new byte[in.available()];
|
||||
in.read(data);
|
||||
buffer.push(data);
|
||||
|
||||
HPacket[] packets = buffer.receive();
|
||||
for (HPacket packet : packets) {
|
||||
if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) {
|
||||
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO);
|
||||
response.appendString(getTitle())
|
||||
@ -102,8 +107,13 @@ public abstract class Extension {
|
||||
onDoubleClick();
|
||||
}
|
||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) {
|
||||
HMessage habboMessage = new HMessage(packet.readString());
|
||||
String stringifiedMessage = packet.readString();
|
||||
HMessage habboMessage = new HMessage(stringifiedMessage);
|
||||
HPacket habboPacket = habboMessage.getPacket();
|
||||
//
|
||||
// System.out.println("----------");
|
||||
// System.out.println(stringifiedMessage);
|
||||
// System.out.println(habboPacket);
|
||||
|
||||
Map<Integer, List<MessageListener>> listeners =
|
||||
habboMessage.getDestination() == HMessage.Side.TOCLIENT ?
|
||||
@ -127,15 +137,22 @@ public abstract class Extension {
|
||||
writeToStream(response.toBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.sleep(1);
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
System.out.println("ERROR");
|
||||
}
|
||||
finally {
|
||||
if (gEarthExtensionServer != null && !gEarthExtensionServer.isClosed()) {
|
||||
try {
|
||||
gEarthExtensionServer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeToStream(byte[] bytes) throws IOException {
|
||||
synchronized (out) {
|
||||
|
55
src/main/extensions/SimpleTestExtension.java
Normal file
55
src/main/extensions/SimpleTestExtension.java
Normal file
@ -0,0 +1,55 @@
|
||||
package main.extensions;
|
||||
|
||||
/**
|
||||
* Created by Jonas on 24/06/18.
|
||||
*/
|
||||
public class SimpleTestExtension extends Extension {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SimpleTestExtension(args);
|
||||
}
|
||||
|
||||
private SimpleTestExtension(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
System.out.println("init");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDoubleClick() {
|
||||
System.out.println("doubleclick");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStartConnection() {
|
||||
System.out.println("connection started");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEndConnection() {
|
||||
System.out.println("connection ended");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTitle() {
|
||||
return "Simple Test!";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDescription() {
|
||||
return "But just for testing purpose";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getVersion() {
|
||||
return "0.1";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAuthor() {
|
||||
return "sirjonasxx";
|
||||
}
|
||||
}
|
@ -484,7 +484,7 @@ public class HPacket implements StringifyAble {
|
||||
return isEdited;
|
||||
}
|
||||
|
||||
private void fixLength() {
|
||||
public void fixLength() {
|
||||
boolean remember = isEdited;
|
||||
replaceInt(0, packetInBytes.length - 4);
|
||||
isEdited = remember;
|
||||
|
@ -38,6 +38,7 @@ public class IncomingHandler extends Handler {
|
||||
|
||||
if (!hMessage.isBlocked()) {
|
||||
out.write(hMessage.getPacket().toBytes());
|
||||
out.flush();
|
||||
}
|
||||
currentIndex++;
|
||||
}
|
||||
|
@ -104,6 +104,7 @@ public class OutgoingHandler extends Handler {
|
||||
currentIndex < encryptOffset ? hMessage.getPacket().toBytes() :
|
||||
servercipher.rc4(hMessage.getPacket().toBytes())
|
||||
);
|
||||
out.flush();
|
||||
}
|
||||
currentIndex ++;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import main.protocol.HMessage;
|
||||
import main.protocol.HPacket;
|
||||
import main.protocol.packethandler.PayloadBuffer;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Socket;
|
||||
@ -30,21 +31,20 @@ public class GEarthExtension {
|
||||
connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes());
|
||||
connection.getOutputStream().flush();
|
||||
|
||||
PayloadBuffer payloadBuffer = new PayloadBuffer();
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
DataInputStream dIn = new DataInputStream(inputStream);
|
||||
|
||||
outerloop:
|
||||
while (!connection.isClosed()) {
|
||||
if (inputStream.available() > 0) {
|
||||
byte[] incoming = new byte[inputStream.available()];
|
||||
inputStream.read(incoming);
|
||||
payloadBuffer.push(incoming);
|
||||
}
|
||||
|
||||
HPacket[] hPackets = payloadBuffer.receive();
|
||||
for (HPacket packet : hPackets) { // it should be only one packet
|
||||
int length = dIn.readInt();
|
||||
byte[] headerandbody = new byte[length + 4];
|
||||
int amountRead = dIn.read(headerandbody, 4, length);
|
||||
|
||||
if (amountRead != length) break;
|
||||
HPacket packet = new HPacket(headerandbody);
|
||||
packet.fixLength();
|
||||
|
||||
if (packet.headerId() == Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO) {
|
||||
|
||||
GEarthExtension gEarthExtension = new GEarthExtension(
|
||||
packet.readString(),
|
||||
packet.readString(),
|
||||
@ -54,15 +54,11 @@ public class GEarthExtension {
|
||||
onDisconnectedCallback
|
||||
);
|
||||
callback.act(gEarthExtension);
|
||||
|
||||
break outerloop;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.sleep(1);
|
||||
}
|
||||
|
||||
} catch (IOException | InterruptedException ignored) {}
|
||||
} catch (IOException ignored) {}
|
||||
}).start();
|
||||
|
||||
}
|
||||
@ -77,29 +73,35 @@ public class GEarthExtension {
|
||||
GEarthExtension selff = this;
|
||||
new Thread(() -> {
|
||||
try {
|
||||
PayloadBuffer payloadBuffer = new PayloadBuffer();
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
DataInputStream dIn = new DataInputStream(inputStream);
|
||||
|
||||
while (!connection.isClosed()) {
|
||||
if (inputStream.available() > 0) {
|
||||
byte[] incoming = new byte[inputStream.available()];
|
||||
inputStream.read(incoming);
|
||||
payloadBuffer.push(incoming);
|
||||
}
|
||||
int length = dIn.readInt();
|
||||
byte[] headerandbody = new byte[length + 4];
|
||||
int amountRead = dIn.read(headerandbody, 4, length);
|
||||
|
||||
if (amountRead != length) break;
|
||||
HPacket packet = new HPacket(headerandbody);
|
||||
packet.fixLength();
|
||||
|
||||
HPacket[] hPackets = payloadBuffer.receive();
|
||||
for (HPacket packet : hPackets) {
|
||||
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
|
||||
receiveMessageListeners.get(i).act(packet);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.sleep(1);
|
||||
}
|
||||
onDisconnectedCallback.act(selff);
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (!connection.isClosed()) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user