use of com.google.protobuf.MessageLite in project MSEC by Tencent.
the class ResponseEncoder method encodeProtobufBody.
protected int encodeProtobufBody(RpcResponse rpcResponse, ChannelBufferOutputStream stream) throws IOException {
if (rpcResponse.getResultObj() == null) {
return 0;
}
MessageLite message = (MessageLite) rpcResponse.getResultObj();
stream.write(message.toByteArray());
return message.getSerializedSize();
}
use of com.google.protobuf.MessageLite in project MSEC by Tencent.
the class RequestDecoder method deserializeProtobufPackage.
private RpcRequest deserializeProtobufPackage(byte[] headBytes, byte[] bodyBytes) {
Head.CRpcHead pbHead = null;
RpcRequest rpcRequest = new RpcRequest();
try {
pbHead = Head.CRpcHead.parseFrom(headBytes);
rpcRequest.setSeq(pbHead.getSequence());
} catch (InvalidProtocolBufferException e) {
log.error("Parse protobuf head failed.");
rpcRequest.setException(new IllegalArgumentException("Parse protobuf head failed."));
return rpcRequest;
}
String serviceMethodName = pbHead.getMethodName().toStringUtf8();
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
log.error("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*");
rpcRequest.setException(new IllegalArgumentException("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*"));
return rpcRequest;
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setFlowid(pbHead.getFlowId());
if (pbHead.getCaller() != null && !pbHead.getCaller().isEmpty()) {
rpcRequest.setFromModule(pbHead.getCaller().toStringUtf8());
} else {
rpcRequest.setFromModule("UnknownModule");
}
// If flowid == 0, we must generate one
if (rpcRequest.getFlowid() == 0) {
rpcRequest.setFlowid(rpcRequest.getSeq() ^ NettyCodecUtils.generateColorId(serviceMethodName));
}
AccessMonitor.add("frm.rpc request incoming: " + methodName);
ServiceFactory.ServiceMethodEntry serviceMethodEntry = ServiceFactory.getServiceMethodEntry(serviceName, methodName);
if (serviceMethodEntry == null) {
log.error("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName());
rpcRequest.setException(new IllegalArgumentException("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName()));
return rpcRequest;
}
MessageLite param = null;
try {
param = (MessageLite) serviceMethodEntry.getParamTypeParser().parseFrom(bodyBytes);
} catch (InvalidProtocolBufferException ex) {
log.error("Parse protobuf body failed.");
rpcRequest.setException(new IllegalArgumentException("RParse protobuf body failed." + ex.getMessage()));
return rpcRequest;
}
rpcRequest.setParameter(param);
log.info("RPC Request received. ServiceMethodName: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName() + "\tSeq: " + rpcRequest.getSeq() + "\tParameter: " + rpcRequest.getParameter());
return rpcRequest;
}
use of com.google.protobuf.MessageLite in project grpc-java by grpc.
the class AbstractInteropTest method checkMetrics.
private static void checkMetrics(MetricsRecord record, boolean server, Collection<? extends MessageLite> requests, Collection<? extends MessageLite> responses) {
int uncompressedRequestsSize = 0;
for (MessageLite request : requests) {
uncompressedRequestsSize += request.getSerializedSize();
}
int uncompressedResponsesSize = 0;
for (MessageLite response : responses) {
uncompressedResponsesSize += response.getSerializedSize();
}
if (server) {
assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
// It's impossible to get the expected wire sizes because it may be compressed, so we just
// check if they are recorded.
assertNotNull(record.getMetric(RpcConstants.RPC_SERVER_REQUEST_BYTES));
assertNotNull(record.getMetric(RpcConstants.RPC_SERVER_RESPONSE_BYTES));
} else {
assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
// It's impossible to get the expected wire sizes because it may be compressed, so we just
// check if they are recorded.
assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_REQUEST_BYTES));
assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_RESPONSE_BYTES));
}
}
use of com.google.protobuf.MessageLite in project tink by google.
the class Ed25519PublicKeyManagerTest method testModifiedSignature.
@Test
public void testModifiedSignature() throws Exception {
Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
KeyTemplate template = SignatureKeyTemplates.ED25519;
MessageLite key = manager.newKey(template);
Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
PublicKeySign signer = manager.getPrimitive(key);
byte[] message = Random.randBytes(20);
byte[] signature = signer.sign(message);
Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
try {
verifier.verify(signature, message);
} catch (GeneralSecurityException e) {
fail("Did not expect GeneralSecurityException: " + e);
}
// Flip bits in message.
for (int i = 0; i < message.length; i++) {
byte[] copy = Arrays.copyOf(message, message.length);
copy[i] = (byte) (copy[i] ^ 0xff);
try {
verifier.verify(signature, copy);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "Signature check failed.");
}
}
// Flip bits in signature.
// Flip the last byte.
byte[] copySig = Arrays.copyOf(signature, signature.length);
copySig[copySig.length - 1] = (byte) (copySig[copySig.length - 1] ^ 0xff);
try {
verifier.verify(copySig, message);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "Signature check failed.");
}
// Flip other bytes.
for (int i = 0; i < signature.length - 1; i++) {
byte[] copy = Arrays.copyOf(signature, signature.length);
copy[i] = (byte) (copy[i] ^ 0xff);
try {
verifier.verify(copy, message);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "Signature check failed.");
}
}
}
use of com.google.protobuf.MessageLite in project MSEC by Tencent.
the class ServiceFactory method callMethod.
public static MessageLite callMethod(String moduleName, String serviceMethodName, MessageLite request, MessageLite responseInstance, int timeoutMillis) throws Exception {
MessageLite ret = null;
NettyClient client = null;
// System.out.println("GetClient begin: " + System.currentTimeMillis());
try {
client = ClientManager.getClient(moduleName, true);
} catch (Exception e) {
log.error("get route for [" + moduleName + "] failed.", e);
return ret;
}
// System.out.println("AfterConnect: " + System.currentTimeMillis());
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
throw new Exception("Invalid serviceMethodName. Must be in format like *.*");
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setSeq(generateSequence());
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParameter(request);
RpcContext context = (RpcContext) RequestProcessor.getThreadContext("session");
if (context != null) {
rpcRequest.setFlowid(context.getRequest().getFlowid());
}
RpcResponse rpcResponse = client.sendRequestAndWaitResponse(rpcRequest, timeoutMillis);
if (rpcResponse != null) {
if (rpcResponse.getErrno() == 0) {
ret = responseInstance.getParserForType().parseFrom((byte[]) rpcResponse.getResultObj());
return ret;
} else if (rpcResponse.getError() != null) {
throw rpcResponse.getError();
} else {
throw new Exception("Rpc failed: errno = " + rpcResponse.getErrno());
}
}
return ret;
}
Aggregations