1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 #include <mutex>
17 #include <sys/types.h>
18
19 #include "network_plugin.h"
20
21 namespace {
22 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
23 std::unique_ptr<NetworkPlugin> g_plugin = nullptr;
24 std::mutex g_taskMutex;
25 } // namespace
26
NetworkPluginSessionStart(const uint8_t * configData,uint32_t configSize)27 static int NetworkPluginSessionStart(const uint8_t* configData, uint32_t configSize)
28 {
29 std::lock_guard<std::mutex> guard(g_taskMutex);
30 g_plugin = std::make_unique<NetworkPlugin>();
31 return g_plugin->Start(configData, configSize);
32 }
33
NetworkPluginReportResult(uint8_t * bufferData,uint32_t bufferSize)34 static int NetworkPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
35 {
36 std::lock_guard<std::mutex> guard(g_taskMutex);
37 if (g_plugin == nullptr) {
38 return -1;
39 }
40 return g_plugin->Report(bufferData, bufferSize);
41 }
42
NetworkPluginReportResultOptimize(RandomWriteCtx * randomWrite)43 static int NetworkPluginReportResultOptimize(RandomWriteCtx* randomWrite)
44 {
45 std::lock_guard<std::mutex> guard(g_taskMutex);
46 if (g_plugin == nullptr) {
47 return -1;
48 }
49 return g_plugin->ReportOptimize(randomWrite);
50 }
51
NetworkPluginSessionStop()52 static int NetworkPluginSessionStop()
53 {
54 std::lock_guard<std::mutex> guard(g_taskMutex);
55 if (g_plugin != nullptr) {
56 g_plugin->Stop();
57 g_plugin = nullptr;
58 }
59 return 0;
60 }
61
NetworkRegisterWriterStruct(const WriterStruct * writer)62 static int NetworkRegisterWriterStruct(const WriterStruct* writer)
63 {
64 return 0;
65 }
66
67 static PluginModuleCallbacks g_callbacks = {
68 .onPluginSessionStart = NetworkPluginSessionStart,
69 .onPluginReportResult = NetworkPluginReportResult,
70 .onPluginSessionStop = NetworkPluginSessionStop,
71 .onRegisterWriterStruct = NetworkRegisterWriterStruct,
72 .onPluginReportResultOptimize = NetworkPluginReportResultOptimize,
73 };
74
75 EXPORT_API PluginModuleStruct g_pluginModule = {
76 .callbacks = &g_callbacks,
77 .name = "network-plugin",
78 .version = "1.02",
79 .resultBufferSizeHint = MAX_BUFFER_SIZE,
80 };