Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "sock_comm_opt.h"
11 : #include "sock_api.h"
12 : #include "log/adx_log.h"
13 : #include "adx_sock_device.h"
14 : namespace Adx {
15 : /**
16 : * @brief get comm opt name
17 : *
18 : * @return
19 : * "LOCAL_SOCK"
20 : */
21 0 : std::string SockCommOpt::CommOptName() { return "LOCAL_SOCK"; }
22 :
23 : /**
24 : * @brief get opt type
25 : *
26 : * @return
27 : * COMM_LOCAL
28 : */
29 5 : OptType SockCommOpt::GetOptType() { return OptType::COMM_LOCAL; }
30 :
31 : /**
32 : * @brief open server
33 : * @param [in] info: map of server info
34 : *
35 : * @return
36 : * handle: opt handle
37 : * ADX_OPT_INVALID_HANDLE
38 : */
39 6 : OptHandle SockCommOpt::OpenServer(const std::map<std::string, std::string>& info)
40 : {
41 6 : OptHandle handle = ADX_OPT_INVALID_HANDLE;
42 6 : if (info.empty()) {
43 1 : return handle;
44 : }
45 :
46 5 : auto it = info.find(OPT_SERVICE_KEY);
47 5 : if (it == info.end()) {
48 1 : return ADX_OPT_INVALID_HANDLE;
49 : }
50 4 : int32_t fd = SockServerCreate(it->second);
51 4 : if (fd < 0) {
52 3 : return handle;
53 : }
54 :
55 1 : handle = static_cast<OptHandle>(fd);
56 1 : return handle;
57 : }
58 :
59 : /**
60 : * @brief close server
61 : * @param [in] handle: opt handle
62 : *
63 : * @return
64 : * IDE_DAEMON_OK: close and destroy server success
65 : * IDE_DAEMON_ERROR: close and destroy server failed
66 : */
67 3 : int32_t SockCommOpt::CloseServer(const OptHandle& handle) const
68 : {
69 3 : if (handle == ADX_OPT_INVALID_HANDLE) {
70 1 : return IDE_DAEMON_ERROR;
71 : }
72 :
73 2 : auto fd = static_cast<int32_t>(handle);
74 2 : return SockServerDestroy(fd);
75 : }
76 :
77 : /**
78 : * @brief open client
79 : * @param [in] info: map of server info
80 : *
81 : * @return
82 : * fd: opt handle
83 : */
84 2 : OptHandle SockCommOpt::OpenClient(const std::map<std::string, std::string>& info)
85 : {
86 : UNUSED(info);
87 2 : int32_t fd = SockClientCreate();
88 2 : return static_cast<OptHandle>(fd);
89 : }
90 :
91 : /**
92 : * @brief close client
93 : * @param [in] handle: opt handle
94 : *
95 : * @return
96 : * IDE_DAEMON_OK: close and destroy client success
97 : * IDE_DAEMON_ERROR: close and destroy client failed
98 : */
99 2 : int32_t SockCommOpt::CloseClient(OptHandle& handle) const
100 : {
101 2 : if (handle == ADX_OPT_INVALID_HANDLE) {
102 1 : return IDE_DAEMON_ERROR;
103 : }
104 :
105 1 : auto fd = static_cast<int32_t>(handle);
106 1 : int32_t ret = SockClientDestory(fd);
107 1 : handle = ADX_OPT_INVALID_HANDLE;
108 1 : return ret;
109 : }
110 :
111 : /**
112 : * @brief call sock accept
113 : * @param [in] handle: opt handle
114 : *
115 : * @return
116 : * clientFd
117 : */
118 3 : OptHandle SockCommOpt::Accept(const OptHandle handle) const
119 : {
120 3 : if (handle == ADX_OPT_INVALID_HANDLE) {
121 1 : return ADX_OPT_INVALID_HANDLE;
122 : }
123 :
124 2 : IDE_LOGI("server %d in accept", static_cast<int32_t>(handle));
125 2 : int32_t fd = SockAccept(handle);
126 2 : if (fd < 0) {
127 1 : return ADX_OPT_INVALID_HANDLE;
128 : }
129 1 : IDE_LOGI("client %d", fd);
130 1 : return static_cast<OptHandle>(fd);
131 : }
132 :
133 : /**
134 : * @brief sock connect
135 : * @param [in] handle: opt handle
136 : * @param [in] info: map of server info
137 : *
138 : * @return
139 : * fd: opt handle
140 : * ADX_OPT_INVALID_HANDLE
141 : */
142 6 : OptHandle SockCommOpt::Connect(const OptHandle handle, const std::map<std::string, std::string>& info)
143 : {
144 6 : if (handle == ADX_OPT_INVALID_HANDLE) {
145 1 : return ADX_OPT_INVALID_HANDLE;
146 : }
147 :
148 5 : auto it = info.find(OPT_SERVICE_KEY);
149 5 : if (it == info.end()) {
150 1 : return ADX_OPT_INVALID_HANDLE;
151 : }
152 4 : int32_t fd = SockConnect(handle, it->second);
153 4 : if (fd < 0) {
154 2 : return ADX_OPT_INVALID_HANDLE;
155 : }
156 :
157 2 : return static_cast<OptHandle>(fd);
158 : }
159 :
160 : /**
161 : * @brief sock close
162 : * @param [in] handle: opt handle
163 : *
164 : * @return
165 : * IDE_DAEMON_OK: close success
166 : * IDE_DAEMON_ERROR: close failed
167 : */
168 3 : int32_t SockCommOpt::Close(OptHandle& handle) const
169 : {
170 3 : if (handle == ADX_OPT_INVALID_HANDLE) {
171 1 : return IDE_DAEMON_ERROR;
172 : }
173 :
174 2 : auto fd = static_cast<int32_t>(handle);
175 2 : handle = ADX_OPT_INVALID_HANDLE;
176 2 : return SockClose(fd);
177 : }
178 :
179 : /**
180 : * @brief sock write
181 : * @param [in] handle: opt handle
182 : * @param [in] buffer: write buffer
183 : * @param [in] length: write length
184 : * @param [in] flag: write flag
185 : *
186 : * @return
187 : * IDE_DAEMON_OK: sock write success
188 : * IDE_DAEMON_ERROR: sock write failed
189 : */
190 10 : int32_t SockCommOpt::Write(const OptHandle handle, IdeSendBuffT buffer, int32_t length, int32_t flag)
191 : {
192 10 : IDE_CTRL_VALUE_FAILED(handle != ADX_OPT_INVALID_HANDLE, return IDE_DAEMON_ERROR, "sock write input invalid");
193 6 : IDE_CTRL_VALUE_FAILED(buffer != nullptr, return IDE_DAEMON_ERROR, "sock write input invalid");
194 5 : IDE_CTRL_VALUE_FAILED(length > 0, return IDE_DAEMON_ERROR, "sock write input invalid");
195 :
196 4 : return SockWrite(handle, buffer, length, flag);
197 : }
198 :
199 : /**
200 : * @brief sock read
201 : * @param [in] handle: opt handle
202 : * @param [in] buffer: read buffer
203 : * @param [in] length: receive length
204 : * @param [in] flag: read flag
205 : *
206 : * @return
207 : * IDE_DAEMON_OK: sock read success
208 : * IDE_DAEMON_ERROR: sock read failed
209 : */
210 6 : int32_t SockCommOpt::Read(const OptHandle handle, IdeRecvBuffT buffer, int32_t& length, int32_t /* flag */)
211 : {
212 6 : IDE_CTRL_VALUE_FAILED(handle != ADX_OPT_INVALID_HANDLE, return IDE_DAEMON_ERROR, "sock write input invalid");
213 4 : IDE_CTRL_VALUE_FAILED(buffer != nullptr, return IDE_DAEMON_ERROR, "sock write input invalid");
214 :
215 3 : int32_t nonBlockFlag = 0;
216 3 : return SockRead(handle, buffer, &length, nonBlockFlag);
217 : }
218 :
219 1 : SharedPtr<AdxDevice> SockCommOpt::GetDevice()
220 : {
221 1 : if (device_ == nullptr) {
222 : try {
223 1 : device_ = std::make_shared<AdxSockDevice>();
224 0 : } catch (...) {
225 0 : return nullptr;
226 0 : }
227 : }
228 :
229 1 : return device_;
230 : }
231 :
232 0 : void SockCommOpt::Timer(void) const {}
233 : } // namespace Adx
|