106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include <cinttypes>
1706f6ba60Sopenharmony_ci#include <unistd.h>
1806f6ba60Sopenharmony_ci#include "logging.h"
1906f6ba60Sopenharmony_ci#include "share_memory_allocator.h"
2006f6ba60Sopenharmony_ci#include "stack_writer.h"
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_ciStackWriter::StackWriter(std::string name,
2306f6ba60Sopenharmony_ci                         uint32_t size,
2406f6ba60Sopenharmony_ci                         int smbFd,
2506f6ba60Sopenharmony_ci                         int eventFd,
2606f6ba60Sopenharmony_ci                         bool blocked)
2706f6ba60Sopenharmony_ci    : pluginName_(name), blocked_(blocked)
2806f6ba60Sopenharmony_ci{
2906f6ba60Sopenharmony_ci    shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
3006f6ba60Sopenharmony_ci    if (shareMemoryBlock_ == nullptr) {
3106f6ba60Sopenharmony_ci    }
3206f6ba60Sopenharmony_ci    eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
3306f6ba60Sopenharmony_ci    lastFlushTime_ = std::chrono::steady_clock::now();
3406f6ba60Sopenharmony_ci}
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ciStackWriter::~StackWriter()
3706f6ba60Sopenharmony_ci{
3806f6ba60Sopenharmony_ci    eventNotifier_ = nullptr;
3906f6ba60Sopenharmony_ci    ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
4006f6ba60Sopenharmony_ci    shareMemoryBlock_ = nullptr;
4106f6ba60Sopenharmony_ci}
4206f6ba60Sopenharmony_ci
4306f6ba60Sopenharmony_civoid StackWriter::Report() const
4406f6ba60Sopenharmony_ci{
4506f6ba60Sopenharmony_ci}
4606f6ba60Sopenharmony_ci
4706f6ba60Sopenharmony_civoid StackWriter::DoStats(long bytes)
4806f6ba60Sopenharmony_ci{
4906f6ba60Sopenharmony_ci    ++writeCount_;
5006f6ba60Sopenharmony_ci    bytesCount_ += bytes;
5106f6ba60Sopenharmony_ci    bytesPending_ += bytes;
5206f6ba60Sopenharmony_ci}
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_cilong StackWriter::Write(const void* data, size_t size)
5506f6ba60Sopenharmony_ci{
5606f6ba60Sopenharmony_ci    if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
5706f6ba60Sopenharmony_ci        return false;
5806f6ba60Sopenharmony_ci    }
5906f6ba60Sopenharmony_ci    return shareMemoryBlock_->PutRaw(reinterpret_cast<const int8_t*>(data), size);
6006f6ba60Sopenharmony_ci}
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_cilong StackWriter::WriteTimeout(const void* data, size_t size)
6306f6ba60Sopenharmony_ci{
6406f6ba60Sopenharmony_ci    if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
6506f6ba60Sopenharmony_ci        return false;
6606f6ba60Sopenharmony_ci    }
6706f6ba60Sopenharmony_ci    return shareMemoryBlock_->PutRawTimeout(reinterpret_cast<const int8_t*>(data), size);
6806f6ba60Sopenharmony_ci}
6906f6ba60Sopenharmony_ci
7006f6ba60Sopenharmony_cilong StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize,
7106f6ba60Sopenharmony_ci                                          const std::function<bool()>& callback)
7206f6ba60Sopenharmony_ci{
7306f6ba60Sopenharmony_ci    if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
7406f6ba60Sopenharmony_ci        return false;
7506f6ba60Sopenharmony_ci    }
7606f6ba60Sopenharmony_ci    if (blocked_) {
7706f6ba60Sopenharmony_ci        return shareMemoryBlock_->PutWithPayloadSync(
7806f6ba60Sopenharmony_ci            reinterpret_cast<const int8_t*>(data),
7906f6ba60Sopenharmony_ci            size,
8006f6ba60Sopenharmony_ci            reinterpret_cast<const int8_t*>(payload),
8106f6ba60Sopenharmony_ci            payloadSize,
8206f6ba60Sopenharmony_ci            callback);
8306f6ba60Sopenharmony_ci    } else {
8406f6ba60Sopenharmony_ci        return shareMemoryBlock_->PutWithPayloadTimeout(
8506f6ba60Sopenharmony_ci            reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
8606f6ba60Sopenharmony_ci    }
8706f6ba60Sopenharmony_ci}
8806f6ba60Sopenharmony_ci
8906f6ba60Sopenharmony_cibool StackWriter::Flush()
9006f6ba60Sopenharmony_ci{
9106f6ba60Sopenharmony_ci    ++flushCount_;
9206f6ba60Sopenharmony_ci    eventNotifier_->Post(flushCount_.load());
9306f6ba60Sopenharmony_ci    lastFlushTime_ = std::chrono::steady_clock::now();
9406f6ba60Sopenharmony_ci    bytesPending_ = 0;
9506f6ba60Sopenharmony_ci    return true;
9606f6ba60Sopenharmony_ci}
97