mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-01-31 01:44:22 +07:00
Custom binary packet support
This commit is contained in:
parent
a9331d4752
commit
81282edce9
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user