baidu / sofa-pbrpc

A light-weight RPC implement of google protobuf RPC framework.
Other
2.13k stars 655 forks source link

如何在服务器端实现接收消息和响应消息的分离? #123

Closed junyiliu closed 7 years ago

junyiliu commented 7 years ago

wiki中给出可以在其他线程中调用done->run()来通知响应消息。但是response->set_message();仍然是在服务函数中定义的,如何实现这个响应回复的内容赋值在别的函数中完成呢?因为服务器端可能收到请求后会调用第三方异步API转发请求,转发的请求通过回调返回,这种场景下应该在第三方API的回调中发送到客户端的响应,应该怎么实现呢?

bluebore commented 7 years ago

response->set_message()是可以在别的函数中完成的,只要你让服务函数把response的指针传给其他函数,在调用done->run()之前填充好response就行。

LazyPlanet commented 7 years ago

这种条件下,我觉得可以修改底层库了。

qinzuoyan commented 7 years ago

response是一个Message,你可以认为是一个普通的非线程安全的结构体,那么在任何线程中给其设置值都可以,只要你自己处理好线程安全问题。 done是一个Closure*,是一个自析构的指针,在调用run()的时候会自己delete自己。所以要求必须调用一次(否则会内存泄露),且只能调用一次(否则会重复delete),这个要求用户自己来保证。既然如此,done->run()就可以在任何线程中被调用,也不需要考虑线程安全问题,因为不可能被并发调用。

On Mon, Oct 10, 2016 at 5:38 PM, LazyPlanet notifications@github.com wrote:

这种条件下,我觉得可以修改底层库了。

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/baidu/sofa-pbrpc/issues/123#issuecomment-252573067, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgQPQ66pJOtrH1q5jpZHWOp_0tKS5p2ks5qygeSgaJpZM4KL67Y .

cyshi commented 7 years ago

后续有问题欢迎讨论 gitter