nnxx_client.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Created by zx on 2020/6/10.
  3. //
  4. #include "nnxx_client.h"
  5. Client::Client()
  6. {
  7. m_socket=nnxx::socket{ nnxx::SP, nnxx::REQ};
  8. }
  9. Client::~Client()
  10. {
  11. std::lock_guard<std::mutex> lck (m_lock);
  12. m_socket.close();
  13. }
  14. Error_manager Client::connect(std::string connect_str)
  15. {
  16. std::lock_guard<std::mutex> lck (m_lock);
  17. m_socket.connect(connect_str);
  18. return SUCCESS;
  19. }
  20. Error_manager Client::request(std::string request_str, std::string& response,unsigned int timeout)
  21. {
  22. std::lock_guard<std::mutex> lck (m_lock);
  23. m_socket.send(request_str);
  24. nnxx::message message;
  25. try { nnxx::with_recv_timeout _ { m_socket, std::chrono::milliseconds(timeout) };
  26. message=m_socket.recv();
  27. }
  28. catch (const nnxx::timeout_error &) {
  29. // return Error_manager(NNXX_CLIENT_REQUEST_TIMEOUT,MINOR_ERROR,"nnxx client request timeout");
  30. }
  31. catch (const std::exception &) {
  32. // return Error_manager(NNXX_CLIENT_REQUEST_UNKNOW,MINOR_ERROR,"nnxx client request unknow error");
  33. }
  34. response=nnxx::to_string(message);
  35. return SUCCESS;
  36. }