本文整理汇总了C++中span::size方法的典型用法代码示例。如果您正苦于以下问题:C++ span::size方法的具体用法?C++ span::size怎么用?C++ span::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类span
的用法示例。
在下文中一共展示了span::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SendFrame
bool CommObject::SendFrame(span<const std::uint8_t> frame)
{
if (frame.size() > CommMaxFrameSize)
{
LOGF(LOG_LEVEL_ERROR, "Frame payload is too long. Allowed: %d, Requested: '%d'.", CommMaxFrameSize, frame.size());
return false;
}
uint8_t cmd[ComPrefferedBufferSize];
cmd[0] = TransmitterSendFrame;
memcpy(cmd + 1, frame.data(), frame.size());
uint8_t remainingBufferSize;
const bool status = (this->_low.WriteRead(CommTransmitter, //
span<const uint8_t>(cmd, 1 + frame.size()), //
span<uint8_t>(&remainingBufferSize, 1) //
) == I2CResult::OK);
if (!status)
{
LOG(LOG_LEVEL_ERROR, "[comm] Failed to send frame");
}
if (remainingBufferSize == 0xff)
{
LOG(LOG_LEVEL_ERROR, "[comm] Frame was not accepted by the transmitter.");
}
return status && remainingBufferSize != 0xff;
}
示例2: iterate_every_other_element
void iterate_every_other_element(span<int, dynamic_range> av)
{
// pick every other element
auto length = av.size() / 2;
#if _MSC_VER > 1800
auto bounds = strided_bounds<1>({ length }, { 2 });
#else
auto bounds = strided_bounds<1>(index<1>{ length }, index<1>{ 2 });
#endif
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
CHECK(strided.size() == length);
CHECK(strided.bounds().index_bounds()[0] == length);
for (auto i = 0; i < strided.size(); ++i)
{
CHECK(strided[i] == av[2 * i + 1]);
}
int idx = 0;
for (auto num : strided)
{
CHECK(num == av[2 * idx + 1]);
idx++;
}
}
示例3: on_announce_response
bool udp_tracker_connection::on_announce_response(span<char const> buf)
{
if (buf.size() < 20) return false;
buf = buf.subspan(8);
restart_read_timeout();
tracker_response resp;
resp.interval = aux::read_int32(buf);
resp.min_interval = 60;
resp.incomplete = aux::read_int32(buf);
resp.complete = aux::read_int32(buf);
int const num_peers = int(buf.size()) / 6;
if ((buf.size() % 6) != 0)
{
fail(error_code(errors::invalid_tracker_response_length));
return false;
}
boost::shared_ptr<request_callback> cb = requester();
#ifndef TORRENT_DISABLE_LOGGING
if (cb)
{
cb->debug_log("<== UDP_TRACKER_RESPONSE [ url: %s ]", tracker_req().url.c_str());
}
#endif
if (!cb)
{
close();
return true;
}
std::vector<peer_entry> peer_list;
resp.peers4.reserve(num_peers);
for (int i = 0; i < num_peers; ++i)
{
ipv4_peer_entry e;
memcpy(&e.ip[0], buf.data(), 4);
buf = buf.subspan(4);
e.port = aux::read_uint16(buf);
resp.peers4.push_back(e);
}
std::list<address> ip_list;
for (std::vector<tcp::endpoint>::const_iterator i = m_endpoints.begin()
, end(m_endpoints.end()); i != end; ++i)
{
ip_list.push_back(i->address());
}
cb->tracker_response(tracker_req(), m_target.address(), ip_list
, resp);
close();
return true;
}
示例4: buffer
// allocate an uninitialized buffer of the specified size
// and copy the initialization range into the start of the buffer
buffer(std::size_t const size, span<char const> initialize)
: buffer(size)
{
TORRENT_ASSERT(initialize.size() <= size);
if (!initialize.empty())
{
std::memcpy(m_begin, initialize.data(), (std::min)(initialize.size(), size));
}
}
示例5: on_scrape_response
bool udp_tracker_connection::on_scrape_response(span<char const> buf)
{
using namespace libtorrent::aux;
restart_read_timeout();
int const action = aux::read_int32(buf);
std::uint32_t const transaction = read_uint32(buf);
if (transaction != m_transaction_id)
{
fail(error_code(errors::invalid_tracker_transaction_id));
return false;
}
if (action == action_error)
{
fail(error_code(errors::tracker_failure), -1
, std::string(buf.data(), buf.size()).c_str());
return true;
}
if (action != action_scrape)
{
fail(error_code(errors::invalid_tracker_action));
return true;
}
if (buf.size() < 12)
{
fail(error_code(errors::invalid_tracker_response_length));
return true;
}
int const complete = aux::read_int32(buf);
int const downloaded = aux::read_int32(buf);
int const incomplete = aux::read_int32(buf);
boost::shared_ptr<request_callback> cb = requester();
if (!cb)
{
close();
return true;
}
cb->tracker_scrape_response(tracker_req()
, complete, incomplete, downloaded, -1);
close();
return true;
}
示例6: random_bytes
void random_bytes(span<char> buffer)
{
#if TORRENT_USE_CRYPTOAPI
HCRYPTPROV prov;
if (!CryptAcquireContext(&prov, NULL, NULL
, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
if (!CryptGenRandom(prov, int(buffer.size())
, reinterpret_cast<BYTE*>(buffer.data())))
{
CryptReleaseContext(prov, 0);
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
}
CryptReleaseContext(prov, 0);
#elif defined TORRENT_USE_LIBCRYPTO
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data())
, int(buffer.size()));
if (r != 1)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(int(::ERR_get_error()), system_category()));
#else
std::terminate();
#endif
}
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
#pragma clang diagnostic pop
#endif
#else
for (auto& b : buffer) b = char(random(0xff));
#endif
}
示例7: count_trailing_ones_hw
int count_trailing_ones_hw(span<std::uint32_t const> buf)
{
auto const num = int(buf.size());
std::uint32_t const* ptr = buf.data();
TORRENT_ASSERT(num >= 0);
TORRENT_ASSERT(ptr != nullptr);
for (int i = num - 1; i >= 0; i--)
{
if (ptr[i] == 0xffffffff) continue;
#if TORRENT_HAS_BUILTIN_CTZ
std::uint32_t const v = ~aux::network_to_host(ptr[i]);
return (num - i - 1) * 32 + __builtin_ctz(v);
#elif defined _MSC_VER
std::uint32_t const v = ~aux::network_to_host(ptr[i]);
DWORD pos;
_BitScanForward(&pos, v);
return (num - i - 1) * 32 + pos;
#else
TORRENT_ASSERT_FAIL();
return -1;
#endif
}
return num * 32;
}
示例8: incoming_packet
bool tracker_manager::incoming_packet(char const* hostname
, span<char const> const buf)
{
TORRENT_ASSERT(is_single_thread());
// ignore packets smaller than 8 bytes
if (buf.size() < 16) return false;
// the first word is the action, if it's not [0, 3]
// it's not a valid udp tracker response
span<const char> ptr = buf;
std::uint32_t const action = aux::read_uint32(ptr);
if (action > 3) return false;
std::uint32_t const transaction = aux::read_uint32(ptr);
auto const i = m_udp_conns.find(transaction);
if (i == m_udp_conns.end())
{
#ifndef TORRENT_DISABLE_LOGGING
// now, this may not have been meant to be a tracker response,
// but chances are pretty good, so it's probably worth logging
m_ses.session_log("incoming UDP tracker packet from %s has invalid "
"transaction ID (%x)", hostname, int(transaction));
#endif
return false;
}
std::shared_ptr<udp_tracker_connection> const p = i->second;
// on_receive() may remove the tracker connection from the list
return p->on_receive_hostname(hostname, buf);
}
示例9: DetectImageFormat
ImageFileInfo DetectImageFormat(span<uint8_t> data) {
ImageFileInfo info;
stbi__context ctx;
stbi__start_mem(&ctx, &data[0], data.size_bytes());
int comp;
if (stbi__bmp_info(&ctx, &info.width, &info.height, &comp)) {
info.hasAlpha = (comp == 4);
info.format = ImageFileFormat::BMP;
return info;
}
TjDecompressHandle handle;
if (tjDecompressHeader(handle, &data[0], data.size_bytes(), &info.width, &info.height) == 0) {
info.hasAlpha = false;
info.format = ImageFileFormat::JPEG;
return info;
}
if (DetectTga(data, info)) {
return info;
}
// Not a very good heuristic
if (data.size() == 256 * 256) {
info.width = 256;
info.height = 256;
info.hasAlpha = true;
info.format = ImageFileFormat::FNTART;
return info;
}
return info;
}
示例10: on_connect_response
bool udp_tracker_connection::on_connect_response(span<char const> buf)
{
// ignore packets smaller than 16 bytes
if (buf.size() < 16) return false;
restart_read_timeout();
// skip header
buf = buf.subspan(8);
// reset transaction
update_transaction_id();
std::uint64_t const connection_id = aux::read_int64(buf);
std::lock_guard<std::mutex> l(m_cache_mutex);
connection_cache_entry& cce = m_connection_cache[m_target.address()];
cce.connection_id = connection_id;
cce.expires = aux::time_now() + seconds(m_man.settings().get_int(settings_pack::udp_tracker_token_expiry));
if (0 == (tracker_req().kind & tracker_request::scrape_request))
send_udp_announce();
else if (0 != (tracker_req().kind & tracker_request::scrape_request))
send_udp_scrape();
return true;
}
示例11: update
void update(span<char const> data)
{
if (CryptHashData(m_hash, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false)
{
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
}
示例12: random_bytes
void random_bytes(span<char> buffer)
{
#ifdef TORRENT_BUILD_SIMULATOR
// simulator
for (auto& b : buffer) b = char(random(0xff));
#elif TORRENT_USE_CRYPTOAPI
// windows
aux::crypt_gen_random(buffer);
#elif TORRENT_USE_DEV_RANDOM
// /dev/random
static dev_random dev;
dev.read(buffer);
#elif defined TORRENT_USE_LIBCRYPTO
// openssl
int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data())
, int(buffer.size()));
if (r != 1) aux::throw_ex<system_error>(errors::no_entropy);
#else
// fallback
for (auto& b : buffer) b = char(random(0xff));
#endif
}
示例13: assert
explicit
enumerated_value_range(span<const T> v)
noexcept
: _begin(v.data())
, _end(v.data()+v.size())
{
assert(_begin <= _end);
}
示例14:
entry::entry(span<char const> v)
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) string_type(v.data(), v.size());
m_type = string_t;
}
示例15: crypt_gen_random
inline void crypt_gen_random(span<char> buffer)
{
static HCRYPTPROV provider = crypt_acquire_provider(PROV_RSA_FULL);
if (!CryptGenRandom(provider, int(buffer.size())
, reinterpret_cast<BYTE*>(buffer.data())))
{
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
}