diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2011-03-30 08:51:28 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2011-03-30 08:51:28 +0300 |
commit | 8174eb73282e177112615c2dd332b69401959764 (patch) | |
tree | 7388d613aacc928cfb006341ed7b66bc27ce09ab /messages.cpp | |
parent | c5e0140f26c8f95a1f83f1e2c86e1eb11820e901 (diff) |
one more threading variant (without mutexes)
Diffstat (limited to 'messages.cpp')
-rw-r--r-- | messages.cpp | 63 |
1 files changed, 22 insertions, 41 deletions
diff --git a/messages.cpp b/messages.cpp index ef6cf97..3426b5e 100644 --- a/messages.cpp +++ b/messages.cpp @@ -19,7 +19,7 @@ wstring new_key; HANDLE new_key_hcnt = NULL; -boost::mutex new_key_hcnt_mutex, gpg_file_mutex; +boost::mutex new_key_hcnt_mutex; bool _terminate = false; int returnNoError(HANDLE hContact); @@ -78,10 +78,11 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) char *tmp = mir_t2a(str.substr(s1,s2-s1).c_str()); TCHAR *tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); wstring path = tmp2; - path.append(_T("\\encrypted_data.asc")); + wstring encfile = toUTF16(get_random(10)); + wstring decfile = toUTF16(get_random(10)); + path.append(_T("\\tmp\\")); + path.append(encfile); DeleteFile(path.c_str()); - while(!gpg_file_mutex.timed_lock(boost::posix_time::seconds(15))) - gpg_file_mutex.unlock(); fstream f(path.c_str(), std::ios::out); while(!f.is_open()) f.open(path.c_str(), std::ios::out); @@ -132,12 +133,15 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) } { wstring path = tmp2; - path += _T("\\decrypted_data"); + path += _T("\\tmp\\"); + path += decfile; DeleteFile(path.c_str()); } cmd += _T("--output \""); cmd += tmp2; - cmd += _T("\\decrypted_data\""); + cmd += _T("\\tmp\\"); + cmd += decfile; + cmd += _T("\""); cmd += _T(" -d -a \""); cmd += path; cmd += _T("\""); @@ -155,14 +159,12 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); - gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) { DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); - gpg_file_mutex.unlock(); return 0; } _terminate = false; @@ -206,14 +208,12 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); - gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) { DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); - gpg_file_mutex.unlock(); return 0; } } @@ -225,7 +225,6 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); - gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) @@ -235,12 +234,14 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) } { wstring tmp = tmp2; - tmp += _T("\\encrypted_data.asc"); + tmp += _T("\\tmp\\"); + tmp += encfile; DeleteFile(tmp.c_str()); } { wstring tmp = tmp2; - tmp += _T("\\decrypted_data"); + tmp += _T("\\tmp\\"); + tmp += decfile; if(_waccess(tmp.c_str(), 0) == -1) { if(errno == ENOENT) @@ -252,19 +253,16 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) strcpy(tmp, str.c_str()); HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); mir_free(tmp); - gpg_file_mutex.unlock(); return 0; } } } - gpg_file_mutex.unlock(); str.clear(); { wstring path = tmp2; mir_free(tmp2); - path += _T("\\decrypted_data"); - while(!gpg_file_mutex.timed_lock(boost::posix_time::seconds(15))) - gpg_file_mutex.unlock(); + path += _T("\\tmp\\"); + path += decfile; fstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); if(f.is_open()) { @@ -278,10 +276,7 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) delete [] tmp; f.close(); DeleteFile(path.c_str()); - gpg_file_mutex.unlock(); } - else - gpg_file_mutex.unlock(); if(str.empty()) { string str = msg; @@ -309,7 +304,6 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) char *tmp = mir_strdup(toUTF8(str).c_str()); HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); mir_free(tmp); - gpg_file_mutex.unlock(); return 0; } } @@ -367,8 +361,6 @@ int RecvMsgSvc(WPARAM w, LPARAM l) _tcscat(tmp2, _T("\\")); _tcscat(tmp2, _T("temporary_exported.asc")); DeleteFile(tmp2); - while(!gpg_file_mutex.timed_lock(boost::posix_time::seconds(15))) - gpg_file_mutex.unlock(); wfstream f(tmp2, std::ios::out); while(!f.is_open()) f.open(tmp2, std::ios::out); @@ -394,15 +386,10 @@ int RecvMsgSvc(WPARAM w, LPARAM l) { gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - gpg_file_mutex.unlock(); return 1; } if(result == pxNotFound) - { - gpg_file_mutex.unlock(); return 1; - } - gpg_file_mutex.unlock(); { char *tmp = NULL; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); @@ -599,6 +586,7 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) string out; DWORD code; wstring cmd; + wstring file = toUTF16(get_random(10)); wstring path; extern bool bJabberAPI, bIsMiranda09; char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); @@ -624,14 +612,14 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) path.append(tmp2); cmd += tmp2; mir_free(tmp2); - cmd += _T("\\exported_data"); - path.append(_T("\\exported_data")); + cmd += _T("\\tmp\\"); + cmd += file; + path.append(_T("\\tmp\\")); + path += file; cmd += _T("\""); { char *tmp; tmp = mir_strdup(toUTF8(str).c_str()); - while(!gpg_file_mutex.timed_lock(boost::posix_time::seconds(15))) - gpg_file_mutex.unlock(); fstream f(path.c_str(), std::ios::out); while(!f.is_open()) f.open(path.c_str(), std::ios::out); @@ -652,13 +640,11 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; mir_free(msg); - gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } if(result == pxNotFound) { mir_free(msg); - gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } if(out.find("There is no assurance this key belongs to the named user") != string::npos) @@ -681,20 +667,17 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; mir_free(msg); - gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } if(result == pxNotFound) { mir_free(msg); - gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } } else { mir_free(msg); - gpg_file_mutex.unlock(); return 0; } } @@ -703,7 +686,6 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) MessageBox(0, _T("Something wrong, gpg does not understand us, aborting encryption."), _T("Warning"), MB_OK); DeleteFile(path.c_str()); mir_free(msg); - gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } DeleteFile(path.c_str()); @@ -724,12 +706,11 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) f.close(); DeleteFile(path.c_str()); } - gpg_file_mutex.unlock(); if(str.empty()) { HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT); hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG"); - debuglog<<time_str()<<": info: Failed to encrypt message with GPG"; + debuglog<<time_str()<<": info: Failed to encrypt message with GPG\n"; mir_free(msg); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } |