message handling on receiving
parent
83dad1ba08
commit
d8b0156bdf
|
@ -18,17 +18,19 @@ public class KraSocketClient {
|
||||||
private final SocketClient socketClient;
|
private final SocketClient socketClient;
|
||||||
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
private KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||||
|
|
||||||
public KraSocketClient(KraSocketClientConfig kraSocketClientConfig, KraSocketClientEventInterface kraSocketClientEventInterface, KraSocketClientProtocol kraSocketClientProtocol) throws URISyntaxException {
|
public KraSocketClient(KraSocketClientConfig kraSocketClientConfig,
|
||||||
|
KraSocketClientEventInterface kraSocketClientEventInterface,
|
||||||
|
KraSocketClientProtocol kraSocketClientProtocol) throws URISyntaxException {
|
||||||
this.kraSocketClientConfig = kraSocketClientConfig;
|
this.kraSocketClientConfig = kraSocketClientConfig;
|
||||||
|
|
||||||
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
this.kraSocketClientEventInitiater = new KraSocketClientEventInitiater();
|
||||||
this.kraSocketClientEventInitiater.addListener(kraSocketClientEventInterface);
|
this.kraSocketClientEventInitiater.addListener(kraSocketClientEventInterface);
|
||||||
|
|
||||||
this.socketClient = new SocketClient(new URI(this.kraSocketClientConfig.getWebSocketProtocol()
|
this.socketClient = new SocketClient(new URI(this.kraSocketClientConfig.getWebSocketProtocol()
|
||||||
+"://" + this.kraSocketClientConfig.getWebSocketHost() + ":"
|
+ "://" + this.kraSocketClientConfig.getWebSocketHost() + ":"
|
||||||
+ this.kraSocketClientConfig.getWebSocketPort()
|
+ this.kraSocketClientConfig.getWebSocketPort()
|
||||||
+ "/ws?ak=" + this.kraSocketClientConfig.getWebSocketAccessKey()
|
+ "/ws?ak=" + this.kraSocketClientConfig.getWebSocketAccessKey()
|
||||||
+ "&s="+ this.kraSocketClientConfig.getWebSocketServerName()),
|
+ "&s=" + this.kraSocketClientConfig.getWebSocketServerName()),
|
||||||
this.kraSocketClientEventInitiater, kraSocketClientProtocol);
|
this.kraSocketClientEventInitiater, kraSocketClientProtocol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package earth.krakatao;
|
package earth.krakatao;
|
||||||
|
|
||||||
|
import earth.krakatao.events.KraSocketClientEventInitiater;
|
||||||
import earth.krakatao.protocol.KraSocketClientProtocol;
|
import earth.krakatao.protocol.KraSocketClientProtocol;
|
||||||
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
import earth.krakatao.protocol.KraSocketClientProtocolMessage;
|
||||||
import earth.krakatao.events.KraSocketClientEventInitiater;
|
import earth.krakatao.protocol.KraSocketClientProtocolStatus;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import org.java_websocket.client.WebSocketClient;
|
import org.java_websocket.client.WebSocketClient;
|
||||||
|
@ -13,7 +14,8 @@ public class SocketClient extends WebSocketClient {
|
||||||
private final KraSocketClientProtocol kraProtocol;
|
private final KraSocketClientProtocol kraProtocol;
|
||||||
private final KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
private final KraSocketClientEventInitiater kraSocketClientEventInitiater;
|
||||||
|
|
||||||
public SocketClient(URI serverUri, KraSocketClientEventInitiater kraSocketClientEventInitiater, KraSocketClientProtocol kraProtocol) {
|
public SocketClient(URI serverUri, KraSocketClientEventInitiater kraSocketClientEventInitiater,
|
||||||
|
KraSocketClientProtocol kraProtocol) {
|
||||||
super(serverUri);
|
super(serverUri);
|
||||||
|
|
||||||
this.kraSocketClientEventInitiater = kraSocketClientEventInitiater;
|
this.kraSocketClientEventInitiater = kraSocketClientEventInitiater;
|
||||||
|
@ -44,7 +46,34 @@ public class SocketClient extends WebSocketClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(ByteBuffer byteBuffer) {
|
public void onMessage(ByteBuffer byteBuffer) {
|
||||||
KraSocketClientProtocolMessage kraProtocolMessage = new KraSocketClientProtocol().DecodeMessage(byteBuffer.array());
|
KraSocketClientProtocolMessage kraProtocolMessage = new KraSocketClientProtocol().DecodeMessage(
|
||||||
|
byteBuffer.array());
|
||||||
|
|
||||||
|
if (kraProtocolMessage.getStatus() == KraSocketClientProtocolStatus.ERROR_NO_PERMS.getStatus()
|
||||||
|
|| kraProtocolMessage.getStatus()
|
||||||
|
== KraSocketClientProtocolStatus.ERROR_TRY_AGAIN.getStatus()
|
||||||
|
|| kraProtocolMessage.getStatus()
|
||||||
|
== KraSocketClientProtocolStatus.ERROR_ARG_LEN_TOO_BIG.getStatus()) {
|
||||||
|
|
||||||
|
KraSocketClient.getLogger().warning("error! status: " + kraProtocolMessage.getStatus());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kraProtocolMessage.getStatus() == KraSocketClientProtocolStatus.REPLY.getStatus()
|
||||||
|
&& this.kraProtocol.getCmdIDs().contains(kraProtocolMessage.getCmdID())) {
|
||||||
|
// TODO: uuid
|
||||||
|
kraProtocolMessage = new KraSocketClientProtocolMessage(
|
||||||
|
KraSocketClientProtocolStatus.MESSAGE_ALREADY_IN_QUEUE.getStatus(), 1, 0,
|
||||||
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", (short) kraProtocolMessage.getCmdNumber(), "");
|
||||||
|
|
||||||
|
this.SendMessage(kraProtocolMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kraProtocolMessage.getStatus() == KraSocketClientProtocolStatus.GET.getStatus()
|
||||||
|
&& this.kraProtocol.getCmdIDs().contains(kraProtocolMessage.getCmdID())) {
|
||||||
|
this.kraProtocol.getCmdIDs().remove(kraProtocolMessage.getCmdID());
|
||||||
|
}
|
||||||
|
|
||||||
this.kraSocketClientEventInitiater.callOnMessage(kraProtocolMessage);
|
this.kraSocketClientEventInitiater.callOnMessage(kraProtocolMessage);
|
||||||
}
|
}
|
||||||
|
@ -52,7 +81,8 @@ public class SocketClient extends WebSocketClient {
|
||||||
@Override
|
@Override
|
||||||
public void onClose(int code, String reason, boolean remote) {
|
public void onClose(int code, String reason, boolean remote) {
|
||||||
this.kraSocketClientEventInitiater.callOnClose(code, reason, remote);
|
this.kraSocketClientEventInitiater.callOnClose(code, reason, remote);
|
||||||
KraSocketClient.logger.warning("connection closed. Code: " + code + " reason: " + reason + " remote: " + remote);
|
KraSocketClient.logger.warning(
|
||||||
|
"connection closed. Code: " + code + " reason: " + reason + " remote: " + remote);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,7 +2,6 @@ package earth.krakatao.protocol;
|
||||||
|
|
||||||
import earth.krakatao.Formatter;
|
import earth.krakatao.Formatter;
|
||||||
import earth.krakatao.KraSocketClient;
|
import earth.krakatao.KraSocketClient;
|
||||||
import earth.krakatao.SocketClient;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -11,14 +10,21 @@ import lombok.Getter;
|
||||||
|
|
||||||
public class KraSocketClientProtocol {
|
public class KraSocketClientProtocol {
|
||||||
|
|
||||||
@Getter
|
//@Getter
|
||||||
//ArrayList<Integer> cmdIDs = Lists.newArrayList();
|
//ArrayList<Integer> cmdIDs = new ArrayList<>();
|
||||||
ArrayList<Integer> cmdIDs = new ArrayList<>();
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
HashMap<Integer, Consumer<KraSocketClientProtocolMessage>> consumerHashMap = new HashMap<>();
|
private final ArrayList<Integer> cmdIDs;
|
||||||
|
@Getter
|
||||||
|
private int currentCmdIDIndex;
|
||||||
|
|
||||||
private int currentCmdIDIndex = 10;
|
//@Getter
|
||||||
|
//HashMap<Integer, Consumer<KraSocketClientProtocolMessage>> consumerHashMap = new HashMap<>();
|
||||||
|
|
||||||
|
public KraSocketClientProtocol() {
|
||||||
|
this.cmdIDs = new ArrayList<>();
|
||||||
|
this.currentCmdIDIndex = 10;
|
||||||
|
}
|
||||||
|
|
||||||
public int generateCmdID() {
|
public int generateCmdID() {
|
||||||
// TODO: check max int value 2^32
|
// TODO: check max int value 2^32
|
||||||
|
@ -42,7 +48,8 @@ public class KraSocketClientProtocol {
|
||||||
|
|
||||||
public byte[] EncodeMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
public byte[] EncodeMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
||||||
String args) {
|
String args) {
|
||||||
KraSocketClient.getLogger().info("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
KraSocketClient.getLogger()
|
||||||
|
.info("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
||||||
|
|
||||||
int argLen = args.length();
|
int argLen = args.length();
|
||||||
|
|
||||||
|
@ -74,6 +81,8 @@ public class KraSocketClientProtocol {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.cmdIDs.add(cmdID);
|
||||||
|
|
||||||
return raw;
|
return raw;
|
||||||
//ProxySystem.getInstance().getSocketClient().SendMessage(raw);
|
//ProxySystem.getInstance().getSocketClient().SendMessage(raw);
|
||||||
}
|
}
|
||||||
|
@ -114,9 +123,11 @@ public class KraSocketClientProtocol {
|
||||||
|
|
||||||
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
KraSocketClient.getLogger().info( "decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
KraSocketClient.getLogger()
|
||||||
+ cmdNumber + " " + args + " ");
|
.info("decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
||||||
|
+ cmdNumber + " " + args + " ");
|
||||||
|
|
||||||
return new KraSocketClientProtocolMessage(status, cmdID, dest, Formatter.stringToUuid(playerUuid).toString(), cmdNumber, args);
|
return new KraSocketClientProtocolMessage(status, cmdID, dest,
|
||||||
|
Formatter.stringToUuid(playerUuid).toString(), cmdNumber, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue