Netcode updates
This commit is contained in:
@@ -30,6 +30,9 @@ public class CallGenerator{
|
|||||||
TypeSpec.Builder packet = TypeSpec.classBuilder(ent.packetClassName)
|
TypeSpec.Builder packet = TypeSpec.classBuilder(ent.packetClassName)
|
||||||
.addModifiers(Modifier.PUBLIC);
|
.addModifiers(Modifier.PUBLIC);
|
||||||
|
|
||||||
|
//temporary data to deserialize later
|
||||||
|
packet.addField(byte[].class, "DATA", Modifier.PRIVATE);
|
||||||
|
|
||||||
packet.superclass(tname("mindustry.net.Packet"));
|
packet.superclass(tname("mindustry.net.Packet"));
|
||||||
|
|
||||||
//return the correct priority
|
//return the correct priority
|
||||||
@@ -41,8 +44,8 @@ public class CallGenerator{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//implement read & write methods
|
//implement read & write methods
|
||||||
packet.addMethod(makeWriter(ent, serializer));
|
makeWriter(packet, ent, serializer);
|
||||||
packet.addMethod(makeReader(ent, serializer));
|
makeReader(packet, ent, serializer);
|
||||||
|
|
||||||
//generate handlers
|
//generate handlers
|
||||||
if(ent.where.isClient){
|
if(ent.where.isClient){
|
||||||
@@ -87,7 +90,7 @@ public class CallGenerator{
|
|||||||
JavaFile.builder(packageName, spec).build().writeTo(BaseProcessor.filer);
|
JavaFile.builder(packageName, spec).build().writeTo(BaseProcessor.filer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MethodSpec makeWriter(MethodEntry ent, ClassSerializer serializer){
|
private static void makeWriter(TypeSpec.Builder typespec, MethodEntry ent, ClassSerializer serializer){
|
||||||
MethodSpec.Builder builder = MethodSpec.methodBuilder("write")
|
MethodSpec.Builder builder = MethodSpec.methodBuilder("write")
|
||||||
.addParameter(Writes.class, "WRITE")
|
.addParameter(Writes.class, "WRITE")
|
||||||
.addModifiers(Modifier.PUBLIC).addAnnotation(Override.class);
|
.addModifiers(Modifier.PUBLIC).addAnnotation(Override.class);
|
||||||
@@ -132,13 +135,27 @@ public class CallGenerator{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
typespec.addMethod(builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MethodSpec makeReader(MethodEntry ent, ClassSerializer serializer){
|
private static void makeReader(TypeSpec.Builder typespec, MethodEntry ent, ClassSerializer serializer){
|
||||||
MethodSpec.Builder builder = MethodSpec.methodBuilder("read")
|
MethodSpec.Builder readbuilder = MethodSpec.methodBuilder("read")
|
||||||
.addParameter(Reads.class, "READ")
|
.addParameter(Reads.class, "READ")
|
||||||
|
.addParameter(int.class, "LENGTH")
|
||||||
.addModifiers(Modifier.PUBLIC).addAnnotation(Override.class);
|
.addModifiers(Modifier.PUBLIC).addAnnotation(Override.class);
|
||||||
|
|
||||||
|
//read only into temporary data buffer
|
||||||
|
readbuilder.addStatement("DATA = READ.b(LENGTH)");
|
||||||
|
|
||||||
|
typespec.addMethod(readbuilder.build());
|
||||||
|
|
||||||
|
MethodSpec.Builder builder = MethodSpec.methodBuilder("check")
|
||||||
|
.addModifiers(Modifier.PRIVATE);
|
||||||
|
|
||||||
|
//make sure data is present, begin reading it if so
|
||||||
|
builder.beginControlFlow("if(DATA != null)");
|
||||||
|
builder.addStatement("BAIS.setBytes(DATA)");
|
||||||
|
|
||||||
Seq<Svar> params = ent.element.params();
|
Seq<Svar> params = ent.element.params();
|
||||||
|
|
||||||
//go through each parameter
|
//go through each parameter
|
||||||
@@ -185,7 +202,9 @@ public class CallGenerator{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
builder.endControlFlow();
|
||||||
|
|
||||||
|
typespec.addMethod(builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates a specific variant for a method entry. */
|
/** Creates a specific variant for a method entry. */
|
||||||
@@ -332,6 +351,8 @@ public class CallGenerator{
|
|||||||
.addAnnotation(Override.class)
|
.addAnnotation(Override.class)
|
||||||
.returns(void.class);
|
.returns(void.class);
|
||||||
|
|
||||||
|
builder.addStatement("check()");
|
||||||
|
|
||||||
Smethod elem = ent.element;
|
Smethod elem = ent.element;
|
||||||
Seq<Svar> params = elem.params();
|
Seq<Svar> params = elem.params();
|
||||||
|
|
||||||
|
|||||||
@@ -385,7 +385,6 @@ public class ArcNetProvider implements NetProvider{
|
|||||||
return readFramework(byteBuffer);
|
return readFramework(byteBuffer);
|
||||||
}else{
|
}else{
|
||||||
//read length int, followed by compressed lz4 data
|
//read length int, followed by compressed lz4 data
|
||||||
//TODO not thread safe!!!
|
|
||||||
Packet packet = Net.newPacket(id);
|
Packet packet = Net.newPacket(id);
|
||||||
var buffer = decompressBuffer.get();
|
var buffer = decompressBuffer.get();
|
||||||
int length = byteBuffer.getShort() & 0xffff;
|
int length = byteBuffer.getShort() & 0xffff;
|
||||||
@@ -396,7 +395,7 @@ public class ArcNetProvider implements NetProvider{
|
|||||||
buffer.position(0).limit(length);
|
buffer.position(0).limit(length);
|
||||||
buffer.put(byteBuffer.array(), byteBuffer.position(), length);
|
buffer.put(byteBuffer.array(), byteBuffer.position(), length);
|
||||||
buffer.position(0);
|
buffer.position(0);
|
||||||
packet.read(reads.get());
|
packet.read(reads.get(), length);
|
||||||
//move read packets forward
|
//move read packets forward
|
||||||
byteBuffer.position(byteBuffer.position() + buffer.position());
|
byteBuffer.position(byteBuffer.position() + buffer.position());
|
||||||
}else{
|
}else{
|
||||||
@@ -405,7 +404,7 @@ public class ArcNetProvider implements NetProvider{
|
|||||||
|
|
||||||
buffer.position(0);
|
buffer.position(0);
|
||||||
buffer.limit(length);
|
buffer.limit(length);
|
||||||
packet.read(reads.get());
|
packet.read(reads.get(), length);
|
||||||
//move buffer forward based on bytes read by decompressor
|
//move buffer forward based on bytes read by decompressor
|
||||||
byteBuffer.position(byteBuffer.position() + read);
|
byteBuffer.position(byteBuffer.position() + read);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,17 @@ package mindustry.net;
|
|||||||
|
|
||||||
import arc.util.io.*;
|
import arc.util.io.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public abstract class Packet{
|
public abstract class Packet{
|
||||||
|
//internally used by generated code
|
||||||
|
//TODO intermediate buffers should ONLY be needed for:
|
||||||
|
//readObject
|
||||||
|
//readBuilding
|
||||||
|
//readUnit (possibly)
|
||||||
|
protected static final ReusableByteInStream BAIS = new ReusableByteInStream();
|
||||||
|
protected static final Reads READ = new Reads(new DataInputStream(BAIS));
|
||||||
|
|
||||||
//these are constants because I don't want to bother making an enum to mirror the annotation enum
|
//these are constants because I don't want to bother making an enum to mirror the annotation enum
|
||||||
|
|
||||||
/** Does not get handled unless client is connected. */
|
/** Does not get handled unless client is connected. */
|
||||||
@@ -15,6 +25,10 @@ public abstract class Packet{
|
|||||||
public void read(Reads read){}
|
public void read(Reads read){}
|
||||||
public void write(Writes write){}
|
public void write(Writes write){}
|
||||||
|
|
||||||
|
public void read(Reads read, int length){
|
||||||
|
read(read);
|
||||||
|
}
|
||||||
|
|
||||||
public int getPriority(){
|
public int getPriority(){
|
||||||
return priorityNormal;
|
return priorityNormal;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package mindustry.net;
|
|||||||
|
|
||||||
import arc.*;
|
import arc.*;
|
||||||
import arc.struct.*;
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
|
||||||
import arc.util.io.*;
|
import arc.util.io.*;
|
||||||
import arc.util.serialization.*;
|
import arc.util.serialization.*;
|
||||||
import mindustry.core.*;
|
import mindustry.core.*;
|
||||||
@@ -131,8 +130,6 @@ public class Packets{
|
|||||||
crc.update(Base64Coder.decode(uuid), 0, b.length);
|
crc.update(Base64Coder.decode(uuid), 0, b.length);
|
||||||
buffer.l(crc.getValue());
|
buffer.l(crc.getValue());
|
||||||
|
|
||||||
Log.info("CRC value sent: @", Long.toHexString(crc.getValue()));
|
|
||||||
|
|
||||||
buffer.b(mobile ? (byte)1 : 0);
|
buffer.b(mobile ? (byte)1 : 0);
|
||||||
buffer.i(color);
|
buffer.i(color);
|
||||||
buffer.b((byte)mods.size);
|
buffer.b((byte)mods.size);
|
||||||
|
|||||||
Reference in New Issue
Block a user