• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BUFFER_WRITER_H
17 #define BUFFER_WRITER_H
18 
19 #include <atomic>
20 #include <memory>
21 #include "event_notifier.h"
22 #include "plugin_module_api.h"
23 #include "share_memory_allocator.h"
24 #include "writer.h"
25 
26 
27 class BufferWriter : public Writer {
28 public:
29     BufferWriter(std::string name,
30                  std::string version,
31                  uint32_t size,
32                  int smbFd,
33                  int eventFd,
34                  uint32_t pluginId);
35     ~BufferWriter();
36     long Write(const void* data, size_t size) override;
37     bool Flush() override;
38     bool Clear() override;
39 
40     bool WriteMessage(const google::protobuf::Message& pmsg, const std::string& pluginName);
41 private:
42     void DoStats(long bytes);
43     void Report() const;
44 
45 private:
46     std::string pluginName_;
47     std::string pluginVersion_;
48     std::shared_ptr<ShareMemoryBlock> shareMemoryBlock_;
49     EventNotifierPtr eventNotifier_ = nullptr;
50     std::chrono::steady_clock::time_point lastFlushTime_;
51     std::atomic<uint64_t> bytesCount_ = 0;
52     std::atomic<uint32_t> bytesPending_ = 0;
53     std::atomic<uint64_t> writeCount_ = 0;
54     std::atomic<uint32_t> flushCount_ = 0;
55     uint32_t pluginId_ = 0;
56 };
57 
58 using BufferWriterPtr = STD_PTR(shared, BufferWriter);
59 
60 #endif // BUFFER_WRITER_H
61