#ifndef HTTPSRVRETRECIEVE_H_XZL_201808171114 #define HTTPSRVRETRECIEVE_H_XZL_201808171114 #include "client_http.hpp" #include "server_http.hpp" // Added for the json-example #define BOOST_SPIRIT_THREADSAFE #include #include // Added for the default_resource example #include #include #include #include #include #ifdef HAVE_OPENSSL #include "crypto.hpp" #endif using HttpServer = SimpleWeb::Server; using HttpClient = SimpleWeb::Client; class HttpSrvRetRecieve { using TASK_FUNCTION = std::function&)>; using OTHER_FUNCTION = std::function; public: HttpSrvRetRecieve(const std::string& ip,const unsigned short port,const std::size_t& threadPoolSize) :m_TASK_FUNCTION(nullptr) { m_server.config.address = ip; m_server.config.port = port; m_server.config.thread_pool_size = threadPoolSize; } int setInfo(const std::string& strInfo,const std::string& strType,TASK_FUNCTION f) { m_map_TASKFUNC[strInfo.substr(1,(strInfo.size()-2))] = f; m_server.resource[strInfo][strType] = [this](std::shared_ptr response, std::shared_ptr request) { std::string strInfo = request->path; auto func = m_map_TASKFUNC[strInfo]; if(func != nullptr){ std::string strRet = func(request->remote_endpoint_address(),request->remote_endpoint_port(),request->content.string(),response); if(strRet.size() > 0){ DBG("strRet.size() > 0"); response->write(strRet); } else{ ERR("strRet.size() <= 0"); } }else{ std::cout << " func == nullptr " << std::endl; } }; m_server.on_error = [](std::shared_ptr /*request*/, const SimpleWeb::error_code & /*ec*/) {}; } int start() { m_server_thread = std::make_shared([&]() { // Start server m_server.start(); }); return 0; } int waitForShutDown() { m_server_thread->join(); } private: HttpServer m_server; TASK_FUNCTION m_TASK_FUNCTION; std::map m_map_TASKFUNC; std::shared_ptr m_server_thread; }; #endif