How to use async_tm_ in fbthrift?
I'm a novice. And I write codes as follow: client:
class MyCallback : public RequestCallback{
public:
explicit MyCallback(){ }
private:
void requestSent() override {
std::cout << "requestSend() has been called " << std::endl;
}
void replyReceived(ClientReceiveState&& state) override{
std::cout << "replyReceived() has been called " << std::endl;
}
void requestError(ClientReceiveState&& state) override{
std::cout << "requestError() has been called " << std::endl;
}
};
void MyEchoClient(){
EventBase evb;
auto addr = folly::SocketAddress(SERVER_IP,SERVER_PORT);
TAsyncSocket::UniquePtr sock(new TAsyncSocket(&evb,addr));
auto chan = HeaderClientChannel::newChannel(std::move(sock));
chan->setProtocolId(apache::thrift::protocol::T_COMPACT_PROTOCOL);
auto client = std::make_unique<EchoAsyncClient>(std::move(chan));
int message_len=1024;
std::string message(message_len,' ');
client->echo(std::make_unique<MyCallback>(),message);
}
int main(int argc,char** argv){
folly::init(&argc,&argv);
MyEchoClient();
return 0;
}
server
class EchoSvIfImple : public EchoSvIf{
async_tm_echo(std::unique_ptr<apache::thrift::HandlerCallback<std::unique_ptr<std::string>>> callback,std::unique_ptr<std::string> message) override {
std::cout << "Do Nothing" << std::endl;
}
};
int main(int argc,char** argv){
folly::init(&argc,&argv);
auto handler = std::make_shared<EchoSvIfImple>();
auto server = std::make_shared<ThriftServer>();
server->setInterface(handler);
server->setPort(9999);
std::cout << "Starting the server..." << std::endl;
server->serve();
return 0;
}
.thrift file
namespace cpp2 echoapp
service Echo{
string echo(1:string message)
}
but it doesn't work, and client always calls MyCallback:: requestError(ClientReceiveState&& state).I don't know how to fix it . Can anyone help me?
Please update your async_tm_echo function to the following content:
std::cout << "Do Nothing" << std::endl;
callback->result("server return");
@duynl58 Sir, it doesn't work. the client always calls MyCallback:: requestError(ClientReceiveState&& state)
Can you send me your full implementation? I make it work in my machine just by adding
callback->result("server return");
@duynl58 Sir, i have sent my code to your email [email protected]. Thank you! I use one machine as a client and another as a server. And the server's IP is 172.16. 40.91