TranImp.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "TranImp.h"
  2. #include "HelloServer.h"
  3. using namespace tars;
  4. TranImp::TranImp()
  5. {
  6. }
  7. ///////////////////////////////////////////////////////////////////////////////
  8. void TranImp::initialize()
  9. {
  10. Application::getCommunicator()->stringToProxy(g_TransDstServerObj, _servantPrx);
  11. ProxyProtocol proto;
  12. proto.requestFunc = ProxyProtocol::streamRequest;
  13. proto.responseFunc = ProxyProtocol::tupResponse;
  14. _servantPrx->tars_set_protocol(proto);
  15. }
  16. void TranImp::async_call(tars::CurrentPtr current, vector<char> &response)
  17. {
  18. const vector<char> &request = current->getRequestBuffer();
  19. TarsInputStream<> is;
  20. is.setBuffer(request.data(), request.size());
  21. RequestPacket req;
  22. req.readFrom(is);
  23. ResponsePacket rsp;
  24. TLOGDEBUG("async_call: begin remote call, req len:" << req.sBuffer.size() << endl);
  25. /*a-sync-call*/
  26. ServantProxyCallbackPtr cb = new ServantCallback("ServantCallback", this, current);
  27. _servantPrx->rpc_call_async(req.iRequestId, req.sFuncName, request.data(), request.size(), cb);
  28. current->setResponse(false);
  29. }
  30. std::atomic<int> wup_trans_count(0);
  31. int TranImp::doRequest(tars::CurrentPtr current, vector<char> &response) {
  32. ++wup_trans_count;
  33. async_call(current, response);
  34. return 0;
  35. }
  36. int TranImp::doResponse(ReqMessagePtr resp) {
  37. ServantCallback *cb = dynamic_cast<ServantCallback *>(resp->callback.get());
  38. if (cb) {
  39. vector<char> &buff = resp->response->sBuffer;
  40. if (!buff.empty()) {
  41. TLOGDEBUG("end remote call, req len:" << buff.size() << endl);
  42. cb->getCurrent()->sendResponse(buff.data(), buff.size());
  43. }
  44. }
  45. return 0;
  46. }
  47. void TranImp::destroy() {
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////