minor code corrections (#1430)

use compact span cast. extend split_path api with only_first_part flag. use single loadlibrary and getprocaddress for IP helper
This commit is contained in:
Andrei Kurushin 2016-12-27 09:45:48 +03:00 committed by Arvid Norberg
parent 192ef4962b
commit 1ec5c69dcd
8 changed files with 63 additions and 63 deletions

View File

@ -146,7 +146,8 @@ namespace libtorrent
TORRENT_EXTRA_EXPORT void hard_link(std::string const& file
, std::string const& link, error_code& ec);
TORRENT_EXTRA_EXPORT std::string split_path(std::string const& f);
TORRENT_EXTRA_EXPORT std::string split_path(std::string const& f
, bool only_first_part = false);
TORRENT_EXTRA_EXPORT char const* next_path_element(char const* p);
TORRENT_EXTRA_EXPORT std::string extension(std::string const& f);
TORRENT_EXTRA_EXPORT std::string remove_extension(std::string const& f);

View File

@ -173,6 +173,7 @@ namespace libtorrent
if (m_lock) m_lock->lock();
m_lock = rhs.m_lock;
rhs.m_lock = nullptr;
return *this;
}
private:
Lock* m_lock;

View File

@ -385,6 +385,41 @@ namespace libtorrent
return false;
}
#ifdef TORRENT_WINDOWS
struct iphlpapi {
static constexpr char const* library_name = "iphlpapi.dll";
};
template <typename Library>
HMODULE get_library_handle()
{
static bool handle_checked = false;
static HMODULE handle = 0;
if (!handle_checked)
{
handle = LoadLibraryA(Library::library_name);
handle_checked = true;
}
return handle;
}
template <typename Library, typename Signature>
Signature get_library_procedure(LPCSTR name)
{
static Signature proc = nullptr;
static bool failed_proc = false;
if ((proc == nullptr) && !failed_proc)
{
HMODULE const handle = get_library_handle<Library>();
if (handle) proc = (Signature)GetProcAddress(handle, name);
failed_proc = (proc == nullptr);
}
return proc;
}
#endif
#if TORRENT_USE_GETIPFORWARDTABLE
address build_netmask(int bits, int family)
{
@ -590,22 +625,13 @@ namespace libtorrent
#elif TORRENT_USE_GETADAPTERSADDRESSES
#if _WIN32_WINNT >= 0x0501
// Load Iphlpapi library
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
if (iphlp)
typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG,ULONG,PVOID,PIP_ADAPTER_ADDRESSES,PULONG);
// Get GetAdaptersAddresses() pointer
auto GetAdaptersAddresses =
get_library_procedure<iphlpapi, GetAdaptersAddresses_t>("GetAdaptersAddresses");
if (GetAdaptersAddresses != nullptr)
{
// Get GetAdaptersAddresses() pointer
typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG,ULONG,PVOID,PIP_ADAPTER_ADDRESSES,PULONG);
GetAdaptersAddresses_t GetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(
iphlp, "GetAdaptersAddresses");
if (GetAdaptersAddresses == nullptr)
{
FreeLibrary(iphlp);
ec = error_code(boost::system::errc::not_supported, generic_category());
return std::vector<ip_interface>();
}
ULONG buf_size = 10000;
std::vector<char> buffer(buf_size);
PIP_ADAPTER_ADDRESSES adapter_addresses
@ -622,7 +648,6 @@ namespace libtorrent
}
if (res != NO_ERROR)
{
FreeLibrary(iphlp);
ec = error_code(WSAGetLastError(), system_category());
return std::vector<ip_interface>();
}
@ -645,8 +670,6 @@ namespace libtorrent
}
}
// Free memory
FreeLibrary(iphlp);
return ret;
}
#endif
@ -915,20 +938,11 @@ namespace libtorrent
#elif TORRENT_USE_GETIPFORWARDTABLE
/*
move this to enum_net_interfaces
// Load Iphlpapi library
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
if (!iphlp)
{
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
// Get GetAdaptersInfo() pointer
typedef DWORD (WINAPI *GetAdaptersInfo_t)(PIP_ADAPTER_INFO, PULONG);
GetAdaptersInfo_t GetAdaptersInfo = (GetAdaptersInfo_t)GetProcAddress(iphlp, "GetAdaptersInfo");
if (!GetAdaptersInfo)
GetAdaptersInfo_t GetAdaptersInfo = get_library_procedure<iphlpapi, GetAdaptersInfo_t>("GetAdaptersInfo");
if (GetAdaptersInfo == nullptr)
{
FreeLibrary(iphlp);
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
@ -937,7 +951,6 @@ namespace libtorrent
ULONG out_buf_size = 0;
if (GetAdaptersInfo(adapter_info, &out_buf_size) != ERROR_BUFFER_OVERFLOW)
{
FreeLibrary(iphlp);
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
@ -945,7 +958,6 @@ namespace libtorrent
adapter_info = (IP_ADAPTER_INFO*)malloc(out_buf_size);
if (!adapter_info)
{
FreeLibrary(iphlp);
ec = boost::asio::error::no_memory;
return std::vector<ip_route>();
}
@ -973,20 +985,12 @@ namespace libtorrent
// Free memory
free(adapter_info);
FreeLibrary(iphlp);
*/
// Load Iphlpapi library
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
if (!iphlp)
{
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
typedef DWORD (WINAPI *GetIfEntry_t)(PMIB_IFROW pIfRow);
GetIfEntry_t GetIfEntry = (GetIfEntry_t)GetProcAddress(iphlp, "GetIfEntry");
if (!GetIfEntry)
auto GetIfEntry = get_library_procedure<iphlpapi, GetIfEntry_t>("GetIfEntry");
if (GetIfEntry == nullptr)
{
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
@ -997,11 +1001,9 @@ namespace libtorrent
ADDRESS_FAMILY, PMIB_IPFORWARD_TABLE2*);
typedef void (WINAPI *FreeMibTable_t)(PVOID Memory);
GetIpForwardTable2_t GetIpForwardTable2 = (GetIpForwardTable2_t)GetProcAddress(
iphlp, "GetIpForwardTable2");
FreeMibTable_t FreeMibTable = (FreeMibTable_t)GetProcAddress(
iphlp, "FreeMibTable");
if (GetIpForwardTable2 && FreeMibTable)
auto GetIpForwardTable2 = get_library_procedure<iphlpapi, GetIpForwardTable2_t>("GetIpForwardTable2");
auto FreeMibTable = get_library_procedure<iphlpapi, FreeMibTable_t>("FreeMibTable");
if (GetIpForwardTable2 != nullptr && FreeMibTable != nullptr)
{
MIB_IPFORWARD_TABLE2* routes = nullptr;
int res = GetIpForwardTable2(AF_UNSPEC, &routes);
@ -1026,7 +1028,6 @@ namespace libtorrent
}
}
if (routes) FreeMibTable(routes);
FreeLibrary(iphlp);
return ret;
}
#endif
@ -1034,11 +1035,9 @@ namespace libtorrent
// Get GetIpForwardTable() pointer
typedef DWORD (WINAPI *GetIpForwardTable_t)(PMIB_IPFORWARDTABLE pIpForwardTable,PULONG pdwSize,BOOL bOrder);
GetIpForwardTable_t GetIpForwardTable = (GetIpForwardTable_t)GetProcAddress(
iphlp, "GetIpForwardTable");
if (!GetIpForwardTable)
auto GetIpForwardTable = get_library_procedure<iphlpapi, GetIpForwardTable_t>("GetIpForwardTable");
if (GetIpForwardTable == nullptr)
{
FreeLibrary(iphlp);
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
@ -1047,7 +1046,6 @@ namespace libtorrent
ULONG out_buf_size = 0;
if (GetIpForwardTable(routes, &out_buf_size, FALSE) != ERROR_INSUFFICIENT_BUFFER)
{
FreeLibrary(iphlp);
ec = boost::asio::error::operation_not_supported;
return std::vector<ip_route>();
}
@ -1055,7 +1053,6 @@ namespace libtorrent
routes = (MIB_IPFORWARDTABLE*)malloc(out_buf_size);
if (!routes)
{
FreeLibrary(iphlp);
ec = boost::asio::error::no_memory;
return std::vector<ip_route>();
}
@ -1082,7 +1079,6 @@ namespace libtorrent
// Free memory
free(routes);
FreeLibrary(iphlp);
#elif TORRENT_USE_NETLINK
enum { BUFSIZE = 8192 };

View File

@ -720,7 +720,7 @@ namespace libtorrent
}
}
std::string split_path(std::string const& f)
std::string split_path(std::string const& f, bool only_first_part)
{
if (f.empty()) return f;
@ -738,11 +738,13 @@ namespace libtorrent
if (p - start > 0)
{
ret.append(start, p - start);
if (only_first_part) return ret;
ret.append(1, '\0');
}
if (*p != 0) ++p;
start = p;
}
if (only_first_part) return ret;
ret.append(1, '\0');
return ret;
}

View File

@ -563,7 +563,7 @@ namespace libtorrent
else
{
if (m_files.empty())
m_name = split_path(path).c_str();
m_name = split_path(path, true);
}
// this is poor-man's emplace_back()

View File

@ -834,8 +834,9 @@ namespace libtorrent
, storage_error& ec)
{
file_storage const& fs = files();
#ifndef TORRENT_DISABLE_MUTABLE_TORRENTS
#ifdef TORRENT_DISABLE_MUTABLE_TORRENTS
TORRENT_UNUSED(links);
#else
if (!links.empty())
{
TORRENT_ASSERT(int(links.size()) == fs.num_files());

View File

@ -161,7 +161,7 @@ namespace libtorrent
{
// UTP_LOGV("incoming packet size:%d\n", size);
if (p.size() < int(sizeof(utp_header))) return false;
if (p.size() < sizeof(utp_header)) return false;
utp_header const* ph = reinterpret_cast<utp_header const*>(p.data());

View File

@ -712,8 +712,7 @@ bool utp_incoming_packet(utp_socket_impl* s
, span<char const> p
, udp::endpoint const& ep, time_point receive_time)
{
return s->incoming_packet(
span<std::uint8_t const>(reinterpret_cast<std::uint8_t const*>(p.data()), int(p.size()))
return s->incoming_packet({reinterpret_cast<std::uint8_t const*>(p.data()), p.size()}
, ep, receive_time);
}