CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/remote/RPCCodecs.java

46 lines
1.6 KiB
Java
Raw Normal View History

2022-03-02 12:34:30 +01:00
package it.cavallium.dbengine.database.remote;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
2022-03-04 01:26:18 +01:00
import it.cavallium.dbengine.rpc.current.data.BoxedRPCEvent;
2022-03-02 12:34:30 +01:00
import it.cavallium.dbengine.rpc.current.data.ClientBoundRequest;
import it.cavallium.dbengine.rpc.current.data.ClientBoundResponse;
import it.cavallium.dbengine.rpc.current.data.IBasicType;
2022-03-04 01:26:18 +01:00
import it.cavallium.dbengine.rpc.current.data.RPCEvent;
2022-03-02 12:34:30 +01:00
import it.cavallium.dbengine.rpc.current.data.ServerBoundRequest;
import it.cavallium.dbengine.rpc.current.data.ServerBoundResponse;
2022-03-04 01:26:18 +01:00
import it.cavallium.dbengine.rpc.current.serializers.BoxedRPCEventSerializer;
2022-03-02 12:34:30 +01:00
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.List;
public class RPCCodecs {
2022-03-04 01:26:18 +01:00
public static class RPCEventCodec extends ByteToMessageCodec<RPCEvent> {
2022-03-02 12:34:30 +01:00
2022-03-04 01:26:18 +01:00
public static final ChannelHandler INSTANCE = new RPCEventCodec();
2022-03-02 12:34:30 +01:00
@Override
2022-03-04 01:26:18 +01:00
protected void encode(ChannelHandlerContext ctx, RPCEvent msg, ByteBuf out) throws Exception {
2022-03-02 12:34:30 +01:00
try (var bbos = new ByteBufOutputStream(out)) {
try (var dos = new DataOutputStream(bbos)) {
2022-03-04 01:26:18 +01:00
BoxedRPCEventSerializer.INSTANCE.serialize(dos, BoxedRPCEvent.of(msg));
2022-03-02 12:34:30 +01:00
}
}
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
try (var bbis = new ByteBufInputStream(msg)) {
try (var dis = new DataInputStream(bbis)) {
2022-03-04 01:26:18 +01:00
out.add(BoxedRPCEventSerializer.INSTANCE.deserialize(dis).val());
2022-03-02 12:34:30 +01:00
}
}
}
}
}