Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc 空参数 的支持 #120

Closed
ruoleng opened this issue Nov 10, 2021 · 11 comments
Closed

rpc 空参数 的支持 #120

ruoleng opened this issue Nov 10, 2021 · 11 comments

Comments

@ruoleng
Copy link

ruoleng commented Nov 10, 2021

ProtocolBuf 自带了EmptyParam 的支持,但是在proto文件中使用导入,SRPC生成的时候报找到不到 google/protobuf/empty.proto。
所以,如何让SRPC支持空参数?
自己修改生成的RPC函数应该可以实现,但是很多地方都要修改。
对了,生成的是C++语言的。

@holmes1412
Copy link
Contributor

holmes1412 commented Nov 10, 2021

你好~目前srpc生成工具:srpc_generator是不支持查找protobuf默认安装路径,所以我没办法找到google/protobuf/empty.proto。

但有个很简单的方法,不需要自己修改生成RPC函数,把文件按照对应的路径拷贝到咱们要生成的位置即可,我试过可行~以tutrorial中为例:

我们的目标proto文件在 tutorial/echo_pb.proto
则把 google/protobuf/empty.proto 拷到 tutorial/google/protobuf/empty.proto
因为在echo_pb.proto中我们会这样引用:import "google/protobuf/empty.proto";

由于执行的路径是tutorial/下,srpc_generator会根据import自动找到我们拷贝的文件,就可以生成对应的Message了。也可以手动生成,命令如下:

cd tutorial
../_bin/srpc_generator  protobuf  echo_pb.proto  ./

@ruoleng
Copy link
Author

ruoleng commented Nov 10, 2021

我试试看,主要是空参数 不想函数有任何参数。

@holmes1412
Copy link
Contributor

我看了下,这个文件里注释有说明,用了empty的这个Message,是让我们可以这样定义rpc:

service Foo {
    rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
}

这个函数也是有参数的吧,只不过是空的而已?

@ruoleng
Copy link
Author

ruoleng commented Nov 10, 2021

是的,可以这样定义。但是SRPC生成出来的C++代码是:

class Service : public srpc::RPCService
{
public:
	// please implement these methods in server.cc

	virtual void Bar(google::protobuf.Empty *request, google::protobuf.Empty *response, srpc::RPCContext *ctx) = 0;

public:
	Service();
};

想要的效果是:

class Service : public srpc::RPCService
{
public:
	// please implement these methods in server.cc

	virtual void Bar(srpc::RPCContext *ctx) = 0;

public:
	Service();
};

@holmes1412
Copy link
Contributor

可是我用protoc生成出来的service接口,也是带着参数的呀,如下:

class Foo_Stub : public Foo { 
 public:                                              
  // implements Foo ------------------------------------------                     
                                                                                   
  void Bar(::PROTOBUF_NAMESPACE_ID::RpcController* controller,                     
                       const PROTOBUF_NAMESPACE_ID::Empty* request,                
                       PROTOBUF_NAMESPACE_ID::Empty* response,                     
                       ::google::protobuf::Closure* done);                             
};                                                                                 

这个没有参数的rpc函数的需求如果protobuf支持,我可以后续找时间支持一下。要不然srpc还是按照官方建议的来哈~

@ruoleng
Copy link
Author

ruoleng commented Nov 10, 2021

Protobuf应该是支持。可以看一下。

@holmes1412
Copy link
Contributor

嗯,我用protoc试了下是如上的结果。其中proto文件里加上了这句:
option cc_generic_services = true;
你可以确认一下~

@ruoleng
Copy link
Author

ruoleng commented Nov 10, 2021

嗯,加了后生成跟你的一致的。
谢谢了~
另外,请教一下,如果client调用的时候,传nullptr 进行调用是否会有问题?
比如

client.Bar(nullptr,[](google::protobuf.Empty *response, srpc::RPCContext *ctx)
{
     // Do something 
});

@ruoleng ruoleng closed this as completed Nov 12, 2021
@Barenboim
Copy link
Contributor

嗯,加了后生成跟你的一致的。 谢谢了~ 另外,请教一下,如果client调用的时候,传nullptr 进行调用是否会有问题? 比如

client.Bar(nullptr,[](google::protobuf.Empty *response, srpc::RPCContext *ctx)
{
     // Do something 
});

应该会挂,因为很难定义这种情况下程序的行为。

@ruoleng
Copy link
Author

ruoleng commented Nov 12, 2021

嗯,加了后生成跟你的一致的。 谢谢了~ 另外,请教一下,如果client调用的时候,传nullptr 进行调用是否会有问题? 比如

client.Bar(nullptr,[](google::protobuf.Empty *response, srpc::RPCContext *ctx)
{
     // Do something 
});

应该会挂,因为很难定义这种情况下程序的行为。

我测试了,跟进了代码。在Windows下,nullptr 传进去调用没问题,最终表现正常。
稍微看了下调用,没有很深入,好像是在Client序列化的时候能成功,也不会崩溃,构造成功后就调用了。
不知道是不是MSVC下没问题。

@Barenboim
Copy link
Contributor

嗯,加了后生成跟你的一致的。 谢谢了~ 另外,请教一下,如果client调用的时候,传nullptr 进行调用是否会有问题? 比如

client.Bar(nullptr,[](google::protobuf.Empty *response, srpc::RPCContext *ctx)
{
     // Do something 
});

应该会挂,因为很难定义这种情况下程序的行为。

我测试了,跟进了代码。在Windows下,nullptr 传进去调用没问题,最终表现正常。 稍微看了下调用,没有很深入,好像是在Client序列化的时候能成功,也不会崩溃,构造成功后就调用了。 不知道是不是MSVC下没问题。

那应该是pb把nullptr序列化成全都是默认值request发送出去。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants