bool Block::fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block) { const uint8_t* tempBegin = buffer; const uint8_t* tempEnd = buffer + maxSize; uint32_t type = 0; bool isOk = Tlv::readType(tempBegin, tempEnd, type); if (!isOk) return false; uint64_t length; isOk = Tlv::readVarNumber(tempBegin, tempEnd, length); if (!isOk) return false; if (length > static_cast<uint64_t>(tempEnd - tempBegin)) { return false; } BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); block = Block(sharedBuffer, type, sharedBuffer->begin(), sharedBuffer->end(), sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end()); return true; }
void TcpConnection::RealSend(const BufferPtr& buffer) { send_buf_.push_back(buffer); socket_.Send(std::bind(&TcpConnection::OnWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2), &(*buffer->begin()), buffer->size()); }
std::tuple<bool, Block> Block::fromBuffer(const uint8_t* buffer, size_t maxSize) { const uint8_t* tempBegin = buffer; const uint8_t* tempEnd = buffer + maxSize; uint32_t type = 0; bool isOk = tlv::readType(tempBegin, tempEnd, type); if (!isOk) return std::make_tuple(false, Block()); uint64_t length; isOk = tlv::readVarNumber(tempBegin, tempEnd, length); if (!isOk) return std::make_tuple(false, Block()); if (length > static_cast<uint64_t>(tempEnd - tempBegin)) return std::make_tuple(false, Block()); BufferPtr sharedBuffer = make_shared<Buffer>(buffer, tempBegin + length); return std::make_tuple(true, Block(sharedBuffer, type, sharedBuffer->begin(), sharedBuffer->end(), sharedBuffer->begin() + (tempBegin - buffer), sharedBuffer->end())); }