To be able to store data in my LMDB Service, I need to store data as base64 items. To do that, I need to transmit json data from the client to the server. It can’t be passed on the url. So I use post handling. Here is the C++ handler:
void TheServer::handle_post(http_request message) { try { g_Logger.WriteLog(_T("handle_post")); PrintRequest(message); std::wstring request = ServerHelper::FindParameter(message, _T("request")); if (request == Constants::VerbSetDataB64) { RequestVerbSetData64(message); return; } else if (request == Constants::VerbGetDataB64) { // Does not work yet RequestVerbGetData64(message); return; } } catch (...) { // an internal problem occured g_Logger.WriteLog(_T("handle_post exception...")); } message.reply(status_codes::OK); };
The C++ routine here to analyze is RequestVerbSetData64. Here is the code:
void TheServer::RequestVerbSetData64(http_request message) { USES_CONVERSION; CLMDBWrapper lmdb; g_Logger.WriteLog(Constants::VerbSetDataB64.c_str()); std::wstring dbNameW = ServerHelper::FindParameter(message, _T("name")); std::string dbName(dbNameW.begin(), dbNameW.end()); std::wstring json; web::json::value jsonV = message.extract_json().get(); Data data = Data::FromJSON(jsonV.as_object()); TCHAR sz[255]; _stprintf_s(sz, _T("Data key:%s value:..."), data.key.c_str()); g_Logger.WriteLog(sz); if (lmdb.Init((LPSTR)dbName.c_str()) == false) { g_Logger.WriteLog(_T("LMDB Init not done !")); message.reply(status_codes::OK); return; } LPSTR lpszKey = W2A(data.key.c_str()); LPSTR lpszValue = W2A(data.value.c_str()); DWORD dwLen = strlen(lpszValue); lmdb.SetData(lpszKey, lpszValue, dwLen); message.reply(status_codes::OK); lmdb.Uninit((LPSTR)dbName.c_str()); }
The source code is simple to write, simple to read. Because it is native code, it is fast and we just need to distribute the dll we use. here, it’s just the C runtime, the C++ runtime and the CPPREST dll. This is the advantage of the native stuff, you don’t need to distribute any framework that size is around 350 MB… It’s lightweight, it’s fast, it’s built on the metal.
Leave a Reply