2020-10-14 01:38:44 +02:00
|
|
|
package it.tdlight.tdlibsession.td.middle;
|
|
|
|
|
2021-01-27 00:58:07 +01:00
|
|
|
import io.netty.buffer.ByteBufInputStream;
|
|
|
|
import io.netty.buffer.ByteBufOutputStream;
|
2020-10-14 01:38:44 +02:00
|
|
|
import io.vertx.core.buffer.Buffer;
|
|
|
|
import io.vertx.core.eventbus.MessageCodec;
|
|
|
|
import it.tdlight.jni.TdApi;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class TdMessageCodec<T extends TdApi.Object> implements MessageCodec<T, T> {
|
|
|
|
|
|
|
|
private final Class<T> clazz;
|
|
|
|
private final String codecName;
|
|
|
|
|
|
|
|
public TdMessageCodec(Class<T> clazz) {
|
|
|
|
super();
|
|
|
|
this.clazz = clazz;
|
|
|
|
this.codecName = clazz.getSimpleName() + "TdCodec";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void encodeToWire(Buffer buffer, T t) {
|
2021-01-27 00:58:07 +01:00
|
|
|
try (var os = new ByteBufOutputStream(buffer.getByteBuf())) {
|
|
|
|
t.serialize(os);
|
2020-10-14 01:38:44 +02:00
|
|
|
} catch (IOException ex) {
|
2021-01-27 00:58:07 +01:00
|
|
|
throw new RuntimeException(ex);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T decodeFromWire(int pos, Buffer buffer) {
|
2021-01-27 00:58:07 +01:00
|
|
|
try (var is = new ByteBufInputStream(buffer.getByteBuf(), pos)) {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (T) TdApi.Deserializer.deserialize(is);
|
2020-10-14 01:38:44 +02:00
|
|
|
} catch (IOException ex) {
|
2021-01-27 00:58:07 +01:00
|
|
|
throw new RuntimeException(ex);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public T transform(T t) {
|
|
|
|
// If a message is sent *locally* across the event bus.
|
|
|
|
// This sends message just as is
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name() {
|
|
|
|
return codecName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public byte systemCodecID() {
|
|
|
|
// Always -1
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|