Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 :
11 : #include "socket_agent.h"
12 : #include <climits>
13 : #include "socket_exception.h"
14 : #include "root_handle_v2.h"
15 : #include "null_ptr_exception.h"
16 :
17 : namespace Hccl {
18 :
19 2 : void SocketAgent::SendMsg(const void* data, u64 dataLen)
20 : {
21 2 : HCCL_INFO("[SocketAgent::%s] start, dataLen[%llu].", __func__, dataLen);
22 :
23 : // 发送数据长度
24 2 : CHK_PRT_THROW(
25 : !socket_->Send(&dataLen, sizeof(dataLen)), HCCL_ERROR("[SocketAgent::%s] Send data len failed", __func__),
26 : SocketException, "Unable to send data");
27 :
28 : // 发送数据
29 2 : CHK_PRT_THROW(
30 : !socket_->Send(data, dataLen), HCCL_ERROR("[SocketAgent::%s] Send data failed", __func__), SocketException,
31 : "Unable to send data");
32 :
33 2 : HCCL_INFO("[SocketAgent::%s] end.", __func__);
34 2 : }
35 :
36 0 : bool SocketAgent::RecvMsg(void* msg, u64& revMsgLen)
37 : {
38 0 : HCCL_INFO("[SocketAgent::%s] start.", __func__);
39 :
40 : // 检查 msg 是否为 nullptr
41 0 : CHK_PRT_THROW(
42 : msg == nullptr, HCCL_ERROR("[RankInfoDetectService::%s] msg is nullptr", __func__), NullPtrException,
43 : "RecvMsg fail");
44 :
45 : // 先接收长度
46 0 : EXCEPTION_CATCH(socket_->Recv(&revMsgLen, sizeof(revMsgLen)), return false);
47 0 : CHK_PRT_RET(
48 : revMsgLen == 0 || revMsgLen > MAX_BUFFER_LEN,
49 : HCCL_ERROR("[SocketAgent::%s] Invalid length[%llu]", __func__, revMsgLen), false);
50 :
51 : // 再接收内容
52 0 : EXCEPTION_CATCH(socket_->Recv(msg, revMsgLen), return false);
53 :
54 0 : HCCL_INFO("[SocketAgent::%s] end, revMsgLen[%llu].", __func__, revMsgLen);
55 0 : return true;
56 : }
57 :
58 : } // namespace Hccl
|