aboutsummaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
-rw-r--r--contrib/epee/include/file_io_utils.h25
-rw-r--r--contrib/epee/include/net/abstract_tcp_server2.inl27
-rw-r--r--contrib/epee/include/string_tools.h35
3 files changed, 66 insertions, 21 deletions
diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h
index 4434f7383..25f8c648b 100644
--- a/contrib/epee/include/file_io_utils.h
+++ b/contrib/epee/include/file_io_utils.h
@@ -33,6 +33,7 @@
#include <boost/filesystem/operations.hpp>
#ifdef WIN32
#include <windows.h>
+#include "string_tools.h"
#endif
// On Windows there is a problem with non-ASCII characters in path and file names
@@ -72,11 +73,9 @@ namespace file_io_utils
bool save_string_to_file(const std::string& path_to_file, const std::string& str)
{
#ifdef WIN32
- WCHAR wide_path[1000];
- int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000);
- if (chars == 0)
- return false;
- HANDLE file_handle = CreateFileW(wide_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ std::wstring wide_path;
+ try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
+ HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE)
return false;
DWORD bytes_written;
@@ -131,11 +130,9 @@ namespace file_io_utils
bool load_file_to_string(const std::string& path_to_file, std::string& target_str, size_t max_size = 1000000000)
{
#ifdef WIN32
- WCHAR wide_path[1000];
- int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000);
- if (chars == 0)
- return false;
- HANDLE file_handle = CreateFileW(wide_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ std::wstring wide_path;
+ try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
+ HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE)
return false;
DWORD file_size = GetFileSize(file_handle, NULL);
@@ -202,11 +199,9 @@ namespace file_io_utils
bool get_file_size(const std::string& path_to_file, uint64_t &size)
{
#ifdef WIN32
- WCHAR wide_path[1000];
- int chars = MultiByteToWideChar(CP_UTF8, 0, path_to_file.c_str(), path_to_file.size() + 1, wide_path, 1000);
- if (chars == 0)
- return false;
- HANDLE file_handle = CreateFileW(wide_path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ std::wstring wide_path;
+ try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; }
+ HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle == INVALID_HANDLE_VALUE)
return false;
LARGE_INTEGER file_size;
diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl
index 134bb4199..5b3550005 100644
--- a/contrib/epee/include/net/abstract_tcp_server2.inl
+++ b/contrib/epee/include/net/abstract_tcp_server2.inl
@@ -653,13 +653,13 @@ PRAGMA_WARNING_DISABLE_VS(4355)
m_timer.cancel();
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
- m_was_shutdown = true;
- m_protocol_handler.release_protocol();
if (!m_host.empty())
{
try { host_count(m_host, -1); } catch (...) { /* ignore */ }
m_host = "";
}
+ m_was_shutdown = true;
+ m_protocol_handler.release_protocol();
return true;
}
//---------------------------------------------------------------------------------
@@ -1030,7 +1030,8 @@ POP_WARNINGS
void boosted_tcp_server<t_protocol_handler>::handle_accept(const boost::system::error_code& e)
{
MDEBUG("handle_accept");
- TRY_ENTRY();
+ try
+ {
if (!e)
{
if (m_connection_type == e_connection_type_RPC) {
@@ -1048,11 +1049,25 @@ POP_WARNINGS
conn->start(true, 1 < m_threads_count);
conn->save_dbg_log();
- }else
+ return;
+ }
+ else
+ {
+ MERROR("Error in boosted_tcp_server<t_protocol_handler>::handle_accept: " << e);
+ }
+ }
+ catch (const std::exception &e)
{
- _erro("Some problems at accept: " << e.message() << ", connections_count = " << m_sock_count);
+ MERROR("Exception in boosted_tcp_server<t_protocol_handler>::handle_accept: " << e.what());
}
- CATCH_ENTRY_L0("boosted_tcp_server<t_protocol_handler>::handle_accept", void());
+
+ // error path, if e or exception
+ _erro("Some problems at accept: " << e.message() << ", connections_count = " << m_sock_count);
+ misc_utils::sleep_no_w(100);
+ new_connection_.reset(new connection<t_protocol_handler>(io_service_, m_config, m_sock_count, m_sock_number, m_pfilter, m_connection_type));
+ acceptor_.async_accept(new_connection_->socket(),
+ boost::bind(&boosted_tcp_server<t_protocol_handler>::handle_accept, this,
+ boost::asio::placeholders::error));
}
//---------------------------------------------------------------------------------
template<class t_protocol_handler>
diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h
index 63705e401..8d8603076 100644
--- a/contrib/epee/include/string_tools.h
+++ b/contrib/epee/include/string_tools.h
@@ -381,6 +381,41 @@ POP_WARNINGS
res = str.substr(0, pos);
return res;
}
+ //----------------------------------------------------------------------------
+#ifdef _WIN32
+ inline std::wstring utf8_to_utf16(const std::string& str)
+ {
+ if (str.empty())
+ return {};
+ int wstr_size = MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), NULL, 0);
+ if (wstr_size == 0)
+ {
+ throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
+ }
+ std::wstring wstr(wstr_size, wchar_t{});
+ if (!MultiByteToWideChar(CP_UTF8, 0, &str[0], str.size(), &wstr[0], wstr_size))
+ {
+ throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
+ }
+ return wstr;
+ }
+ inline std::string utf16_to_utf8(const std::wstring& wstr)
+ {
+ if (wstr.empty())
+ return {};
+ int str_size = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), NULL, 0, NULL, NULL);
+ if (str_size == 0)
+ {
+ throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
+ }
+ std::string str(str_size, char{});
+ if (!WideCharToMultiByte(CP_UTF8, 0, &wstr[0], wstr.size(), &str[0], str_size, NULL, NULL))
+ {
+ throw std::runtime_error(std::error_code(GetLastError(), std::system_category()).message());
+ }
+ return str;
+ }
+#endif
}
}
#endif //_STRING_TOOLS_H_