init
commit
7ca3afd60e
|
@ -0,0 +1,75 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>earth.krakatoa</groupId>
|
||||||
|
<artifactId>KraSocketClient</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>14</maven.compiler.source>
|
||||||
|
<maven.compiler.target>14</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<defaultGoal>clean install</defaultGoal>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
<id>make-assembly</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>9</source>
|
||||||
|
<target>9</target>
|
||||||
|
</configuration>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
<version>1.18.22</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<artifactId>Java-WebSocket</artifactId>
|
||||||
|
<groupId>org.java-websocket</groupId>
|
||||||
|
<version>1.5.2</version> <!-- 1.5.1 -->
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<!-- https://github.com/TooTallNate/Java-WebSocket -->
|
||||||
|
<repository>
|
||||||
|
<id>sonatype-nexus-snapshots</id>
|
||||||
|
<releases>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</releases>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,29 @@
|
||||||
|
package earth.krakatao;
|
||||||
|
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInitiater;
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInterface;
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocol;
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
public class KraSocketClient {
|
||||||
|
|
||||||
|
private KraSocketClientConfig kraSocketClientConfig;
|
||||||
|
private final SocketClient socketClient;
|
||||||
|
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||||
|
|
||||||
|
public KraSocketClient(KraSocketClientConfig kraSocketClientConfig, KraSocketClientEventInterface kraSocketClientEventInterface, KraSocketClientProtocol kraSocketClientProtocol) throws URISyntaxException {
|
||||||
|
this.kraSocketClientConfig = kraSocketClientConfig;
|
||||||
|
|
||||||
|
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
||||||
|
this.kraSocketClientEventInitiater.addListener(kraSocketClientEventInterface);
|
||||||
|
|
||||||
|
this.socketClient = new SocketClient(new URI(this.kraSocketClientConfig.getWebSocketProtocol() +"://" + this.kraSocketClientConfig.getWebSocketHost() + ":" + this.kraSocketClientConfig.getWebSocketPort() + "/ws?t=" + this.kraSocketClientConfig.getWebSocketToken() + "&s="+ this.kraSocketClientConfig.getWebSocketServerName()), kraSocketClientEventInitiater, kraSocketClientProtocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMsg(KraSocketClientProtocolMessage kraProtocolMessage) {
|
||||||
|
this.socketClient.send("");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package earth.krakatao;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class KraSocketClientConfig {
|
||||||
|
|
||||||
|
private final String WebSocketProtocol;
|
||||||
|
private final String WebSocketHost;
|
||||||
|
private final int WebSocketPort;
|
||||||
|
private final String WebSocketToken;
|
||||||
|
private final String WebSocketAccessKey;
|
||||||
|
private final String WebSocketServerName;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package earth.krakatao;
|
||||||
|
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocol;
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInitiater;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import org.java_websocket.client.WebSocketClient;
|
||||||
|
import org.java_websocket.handshake.ServerHandshake;
|
||||||
|
|
||||||
|
public class SocketClient extends WebSocketClient {
|
||||||
|
|
||||||
|
private final KraSocketClientProtocol kraProtocol;
|
||||||
|
private final KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||||
|
|
||||||
|
public SocketClient(URI serverUri, KraSocketClientEventInitiater kraSocketClientEventInitiater, KraSocketClientProtocol kraProtocol) {
|
||||||
|
super(serverUri);
|
||||||
|
|
||||||
|
this.kraSocketClientEventInitiater = kraSocketClientEventInitiater;
|
||||||
|
this.kraProtocol = kraProtocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendMessage(KraSocketClientProtocolMessage kraProtocolMessage) {
|
||||||
|
if (isOpen()) {
|
||||||
|
send(this.kraProtocol.EncodeMessage(kraProtocolMessage));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(ServerHandshake serverHandshake) {
|
||||||
|
this.kraSocketClientEventInitiater.callOnOpen(serverHandshake);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(String s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(ByteBuffer byteBuffer) {
|
||||||
|
KraSocketClientProtocolMessage kraProtocolMessage = new KraSocketClientProtocol().DecodeMessage(byteBuffer.array());
|
||||||
|
|
||||||
|
this.kraSocketClientEventInitiater.callOnMessage(kraProtocolMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClose(int code, String reason, boolean remote) {
|
||||||
|
this.kraSocketClientEventInitiater.callOnClose(code, reason, remote);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Exception e) {
|
||||||
|
System.out.println("Error: " + e);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package earth.krakatao.events;
|
||||||
|
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.java_websocket.handshake.ServerHandshake;
|
||||||
|
|
||||||
|
public class KraSocketClientEventInitiater {
|
||||||
|
|
||||||
|
private final List<KraSocketClientEventInterface> listenerList = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addListener(KraSocketClientEventInterface testListener) {
|
||||||
|
listenerList.add(testListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void callOnOpen(ServerHandshake serverHandshake) {
|
||||||
|
for (KraSocketClientEventInterface testListener : listenerList) {
|
||||||
|
testListener.kraSocketClientOnOpen(serverHandshake);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void callOnMessage(KraSocketClientProtocolMessage kraProtocolMessage) {
|
||||||
|
for (KraSocketClientEventInterface testListener : listenerList) {
|
||||||
|
testListener.kraSocketClientOnMessage(kraProtocolMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void callOnClose(int code, String reason, boolean remote) {
|
||||||
|
for (KraSocketClientEventInterface testListener : listenerList) {
|
||||||
|
testListener.kraSocketClientOnClose(code, reason, remote);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package earth.krakatao.events;
|
||||||
|
|
||||||
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
|
import org.java_websocket.handshake.ServerHandshake;
|
||||||
|
|
||||||
|
public interface KraSocketClientEventInterface {
|
||||||
|
|
||||||
|
void kraSocketClientOnMessage(KraSocketClientProtocolMessage kraProtocolMessage);
|
||||||
|
|
||||||
|
void kraSocketClientOnOpen(ServerHandshake serverHandshake);
|
||||||
|
|
||||||
|
void kraSocketClientOnClose(int code, String reason, boolean remote);
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
package earth.krakatao.protocol;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public class KraSocketClientProtocol {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
//ArrayList<Integer> cmdIDs = Lists.newArrayList();
|
||||||
|
ArrayList<Integer> cmdIDs = new ArrayList<>();
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
HashMap<Integer, Consumer<KraSocketClientProtocolMessage>> consumerHashMap = new HashMap<>();
|
||||||
|
|
||||||
|
public int getCmdID() {
|
||||||
|
if (cmdIDs.isEmpty()) {
|
||||||
|
cmdIDs.add(10);
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmdID = cmdIDs.get(cmdIDs.size() - 1) + 1;
|
||||||
|
|
||||||
|
cmdIDs.add(cmdID);
|
||||||
|
|
||||||
|
// TODO: check max int value 2^32
|
||||||
|
|
||||||
|
return cmdID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] EncodeMessage(KraSocketClientProtocolMessage kraProtocolMessage) {
|
||||||
|
return EncodeMessage(kraProtocolMessage.getStatus(), kraProtocolMessage.getCmdID(),
|
||||||
|
kraProtocolMessage.getDest(), kraProtocolMessage.getUuid(),
|
||||||
|
kraProtocolMessage.getCmdNumber(), kraProtocolMessage.getArgs());
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] EncodeMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
||||||
|
String args) {
|
||||||
|
System.out.println("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
||||||
|
|
||||||
|
int argLen = args.length();
|
||||||
|
|
||||||
|
byte[] raw = new byte[40 + argLen];
|
||||||
|
|
||||||
|
raw[0] = (byte) status;
|
||||||
|
|
||||||
|
raw[1] = (byte) cmdID;
|
||||||
|
raw[2] = (byte) (cmdID >> 8);
|
||||||
|
raw[3] = (byte) (cmdID >> 16);
|
||||||
|
raw[4] = (byte) (cmdID >> 24);
|
||||||
|
|
||||||
|
raw[5] = (byte) dest;
|
||||||
|
|
||||||
|
byte[] uuidBytes = uuid.getBytes();
|
||||||
|
|
||||||
|
/* for (int i = 0; i < 32; i++) {
|
||||||
|
raw[6 + i] = uuidBytes[i];
|
||||||
|
}*/
|
||||||
|
|
||||||
|
System.arraycopy(uuidBytes, 0, raw, 6, 32);
|
||||||
|
|
||||||
|
raw[38] = (byte) cmdNumber;
|
||||||
|
raw[39] = (byte) (cmdNumber >> 8);
|
||||||
|
|
||||||
|
if (argLen > 0) {
|
||||||
|
for (int i = 0; i < argLen; i++) {
|
||||||
|
raw[40 + i] = (byte) args.charAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return raw;
|
||||||
|
//ProxySystem.getInstance().getSocketClient().SendMessage(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
public KraSocketClientProtocolMessage DecodeMessage(byte[] data) {
|
||||||
|
System.out.println("start decode");
|
||||||
|
|
||||||
|
byte status = data[0];
|
||||||
|
|
||||||
|
int cmdID = Byte.toUnsignedInt(data[1]);
|
||||||
|
|
||||||
|
cmdID += Byte.toUnsignedInt(data[2]) * 256;
|
||||||
|
cmdID += Byte.toUnsignedInt(data[3]) * 256 * 256;
|
||||||
|
cmdID += Byte.toUnsignedInt(data[4]) * 256 * 256 * 256;
|
||||||
|
|
||||||
|
int dest = Byte.toUnsignedInt(data[5]);
|
||||||
|
|
||||||
|
byte[] playerUuidBytes = new byte[32];
|
||||||
|
|
||||||
|
System.arraycopy(data, 6, playerUuidBytes, 0, 32);
|
||||||
|
|
||||||
|
/* for (int i = 0; i < 32; i++) {
|
||||||
|
playerUuidBytes[i] = data[6 + i];
|
||||||
|
} */
|
||||||
|
|
||||||
|
String playerUuid = new String(playerUuidBytes, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
short cmdNumber = (short) (Byte.toUnsignedInt(data[38]) + (Byte.toUnsignedInt(data[39]) * 256));
|
||||||
|
|
||||||
|
int argLen = data.length - 40;
|
||||||
|
byte[] argsBytes = new byte[argLen];
|
||||||
|
|
||||||
|
/* for (int i = 0; i < argLen; i++) {
|
||||||
|
argsBytes[i] = data[40 + i];
|
||||||
|
} */
|
||||||
|
|
||||||
|
System.arraycopy(data, 40, argsBytes, 0, argLen);
|
||||||
|
|
||||||
|
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
||||||
|
+ cmdNumber + " " + args + " ");
|
||||||
|
|
||||||
|
return new KraSocketClientProtocolMessage(status, cmdID, dest, playerUuid, cmdNumber, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package earth.krakatao.protocol;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public class KraSocketClientProtocolMessage {
|
||||||
|
|
||||||
|
private final byte status;
|
||||||
|
private final int cmdID; // long
|
||||||
|
private final int dest;
|
||||||
|
private final String uuid;
|
||||||
|
private final short cmdNumber;
|
||||||
|
private final String args;
|
||||||
|
|
||||||
|
public int getStatus() {
|
||||||
|
return Byte.toUnsignedInt(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCmdNumber() {
|
||||||
|
return Short.toUnsignedInt(cmdNumber);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package earth.krakatao.protocol;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public enum KraSocketClientProtocolStatus {
|
||||||
|
|
||||||
|
SEND(10),
|
||||||
|
GET(11),
|
||||||
|
MESSAGE_ALREADY_IN_QUEUE(12),
|
||||||
|
ERROR_NO_PERMS(13),
|
||||||
|
ERROR_TRY_AGAIN(14),
|
||||||
|
ERROR_ARG_LEN_TOO_BIG(15),
|
||||||
|
REPLY(16);
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final int status;
|
||||||
|
|
||||||
|
KraSocketClientProtocolStatus(int status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
#Generated by Maven
|
||||||
|
#Fri Dec 17 19:53:24 CET 2021
|
||||||
|
groupId=earth.krakatoa
|
||||||
|
artifactId=KraSocketClient
|
||||||
|
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,7 @@
|
||||||
|
earth/krakatao/KraSocketClientConfig.class
|
||||||
|
earth/krakatao/protocol/KraSocketClientProtocolStatus.class
|
||||||
|
earth/krakatao/protocol/KraSocketClientProtocolMessage.class
|
||||||
|
earth/krakatao/SocketClient.class
|
||||||
|
earth/krakatao/events/KraSocketClientEventInitiater.class
|
||||||
|
earth/krakatao/protocol/KraSocketClientProtocol.class
|
||||||
|
earth/krakatao/events/KraSocketClientEventInterface.class
|
|
@ -0,0 +1,8 @@
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/KraSocketClient.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/SocketClient.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/events/KraSocketClientEventInterface.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/protocol/KraSocketClientProtocol.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/protocol/KraSocketClientProtocolStatus.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/KraSocketClientConfig.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/protocol/KraSocketClientProtocolMessage.java
|
||||||
|
/home/alex/IdeaProjects/KraSocketClient/src/main/java/earth/krakatao/events/KraSocketClientEventInitiater.java
|
Loading…
Reference in New Issue