Custom binary packet support

This commit is contained in:
Anuken 2024-10-28 17:17:29 -04:00
parent a9331d4752
commit 81282edce9
2 changed files with 52 additions and 2 deletions

View File

@ -57,6 +57,8 @@ public class NetClient implements ApplicationListener{
private DataInputStream dataStream = new DataInputStream(byteStream);
/** Packet handlers for custom types of messages. */
private ObjectMap<String, Seq<Cons<String>>> customPacketHandlers = new ObjectMap<>();
/** Packet handlers for custom types of messages, in binary. */
private ObjectMap<String, Seq<Cons<byte[]>>> customBinaryPacketHandlers = new ObjectMap<>();
public NetClient(){
@ -147,10 +149,34 @@ public class NetClient implements ApplicationListener{
return customPacketHandlers.get(type, Seq::new);
}
public void addBinaryPacketHandler(String type, Cons<byte[]> handler){
customBinaryPacketHandlers.get(type, Seq::new).add(handler);
}
public Seq<Cons<byte[]>> getBinaryPacketHandlers(String type){
return customBinaryPacketHandlers.get(type, Seq::new);
}
@Remote(targets = Loc.server, variants = Variant.both)
public static void clientBinaryPacketReliable(String type, byte[] contents){
var arr = netClient.customBinaryPacketHandlers.get(type);
if(arr != null){
for(var c : arr){
c.get(contents);
}
}
}
@Remote(targets = Loc.server, variants = Variant.both, unreliable = true)
public static void clientBinaryPacketUnreliable(String type, byte[] contents){
clientBinaryPacketReliable(type, contents);
}
@Remote(targets = Loc.server, variants = Variant.both)
public static void clientPacketReliable(String type, String contents){
if(netClient.customPacketHandlers.containsKey(type)){
for(Cons<String> c : netClient.customPacketHandlers.get(type)){
var arr = netClient.customPacketHandlers.get(type);
if(arr != null){
for(Cons<String> c : arr){
c.get(contents);
}
}

View File

@ -117,6 +117,8 @@ public class NetServer implements ApplicationListener{
private DataOutputStream dataStream = new DataOutputStream(syncStream);
/** Packet handlers for custom types of messages. */
private ObjectMap<String, Seq<Cons2<Player, String>>> customPacketHandlers = new ObjectMap<>();
/** Packet handlers for custom types of messages - binary version. */
private ObjectMap<String, Seq<Cons2<Player, byte[]>>> customBinaryPacketHandlers = new ObjectMap<>();
/** Packet handlers for logic client data */
private ObjectMap<String, Seq<Cons2<Player, Object>>> logicClientDataHandlers = new ObjectMap<>();
@ -517,6 +519,14 @@ public class NetServer implements ApplicationListener{
return customPacketHandlers.get(type, Seq::new);
}
public void addBinaryPacketHandler(String type, Cons2<Player, byte[]> handler){
customBinaryPacketHandlers.get(type, Seq::new).add(handler);
}
public Seq<Cons2<Player, byte[]>> getBinaryPacketHandlers(String type){
return customBinaryPacketHandlers.get(type, Seq::new);
}
public void addLogicDataHandler(String type, Cons2<Player, Object> handler){
logicClientDataHandlers.get(type, Seq::new).add(handler);
}
@ -589,6 +599,20 @@ public class NetServer implements ApplicationListener{
serverPacketReliable(player, type, contents);
}
@Remote(targets = Loc.client)
public static void serverBinaryPacketReliable(Player player, String type, byte[] contents){
if(netServer.customPacketHandlers.containsKey(type)){
for(var c : netServer.customBinaryPacketHandlers.get(type)){
c.get(player, contents);
}
}
}
@Remote(targets = Loc.client, unreliable = true)
public static void serverBinaryPacketUnreliable(Player player, String type, byte[] contents){
serverBinaryPacketReliable(player, type, contents);
}
@Remote(targets = Loc.client)
public static void clientLogicDataReliable(Player player, String channel, Object value){
Seq<Cons2<Player, Object>> handlers = netServer.logicClientDataHandlers.get(channel);