net.dongliu.prettypb.rpc.server.RpcServerChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prettypb-rpc Show documentation
Show all versions of prettypb-rpc Show documentation
proto rpc libs, compatible with proto-rpc-pro
package net.dongliu.prettypb.rpc.server;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import net.dongliu.prettypb.rpc.protocol.OobResponse;
import net.dongliu.prettypb.rpc.protocol.RpcError;
import net.dongliu.prettypb.rpc.protocol.RpcResponse;
import net.dongliu.prettypb.rpc.protocol.WirePayload;
import net.dongliu.prettypb.rpc.info.PeerInfo;
import net.dongliu.prettypb.runtime.ProtobufSerializer;
/**
* One rpc connection with client
*
* @author dongliu
*/
public class RpcServerChannel {
private Channel channel;
private PeerInfo serverInfo;
private PeerInfo clientInfo;
private boolean compress;
public RpcServerChannel(Channel channel, PeerInfo serverInfo, PeerInfo clientInfo,
boolean compress) {
this.channel = channel;
this.serverInfo = serverInfo;
this.clientInfo = clientInfo;
this.compress = compress;
}
/**
* channel unique name used to identify
*
* @return
*/
public String getChannelName() {
return clientInfo.getName();
}
public PeerInfo getServerInfo() {
return serverInfo;
}
public Channel getChannel() {
return channel;
}
public PeerInfo getClientInfo() {
return clientInfo;
}
public boolean isCompress() {
return compress;
}
/**
* send response
*/
public void sendRpcResponse(int correlationId, Object response, Class clazz) {
RpcResponse rpcResponse = new RpcResponse();
rpcResponse.setCorrelationId(correlationId);
rpcResponse.setResponseBytes(ProtobufSerializer.toBytes(response, clazz));
WirePayload wirePayload = new WirePayload();
wirePayload.setRpcResponse(rpcResponse);
channel.writeAndFlush(wirePayload);
}
public ChannelFuture sendOobResponse(int correlationId, byte[] bytes) {
OobResponse oobResponse = new OobResponse();
oobResponse.setCorrelationId(correlationId);
oobResponse.setMessageBytes(bytes);
WirePayload wirePayload = new WirePayload();
wirePayload.setOobResponse(oobResponse);
return channel.writeAndFlush(wirePayload);
}
/**
* send error response
*/
public void sendRpcError(int correlationId, String msg) {
RpcError rpcError = new RpcError();
rpcError.setCorrelationId(correlationId);
rpcError.setErrorMessage(msg);
WirePayload wirePayload = new WirePayload();
wirePayload.setRpcError(rpcError);
channel.writeAndFlush(wirePayload);
}
}