org.jackstaff.grpc.Packet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grpc-core Show documentation
Show all versions of grpc-core Show documentation
Jackstaff Grpc Core: A java RPC framework , gRPC based and compatible with gRPC
package org.jackstaff.grpc;
import com.google.protobuf.ByteString;
import org.jackstaff.grpc.exception.SerializationException;
import org.jackstaff.grpc.internal.InternalProto;
import org.jackstaff.grpc.internal.Serializer;
import java.util.Arrays;
import java.util.function.Consumer;
/**
* the package for interact client side and server side
* @author [email protected]
*/
public class Packet implements Command {
private int command;
private T payload;
public Packet() {
}
public Packet(int command) {
this.command = command;
}
public Packet(int command, T payload) {
this.command = command;
this.payload = payload;
}
public int getCommand() {
return command;
}
public void setCommand(int command) {
this.command = command;
}
public T getPayload() {
return payload;
}
public void setPayload(T payload) {
this.payload = payload;
}
@Override
public String toString() {
return "Packet{" +
"command=" + command +
", payload=" + payload +
'}';
}
boolean isOk(){
return command == OK;
}
boolean isException(){
return command == EXCEPTION;
}
static Packet> throwable(Throwable ex){
return new Packet<>(EXCEPTION, ex);
}
static Packet> ok(Object value){
return new Packet<>(OK, value);
}
static Packet> message(Object value){
return new Packet<>(MESSAGE, value);
}
Object[] unboxing(){
return Arrays.stream((Object[])payload).map(a->a ==null || a.getClass().equals(Object.class) ? null : a).toArray();
}
Object[] unboxing(int length){
return Arrays.copyOf(unboxing(), length);
}
static Packet