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(!socket_->Send(&dataLen, sizeof(dataLen)), HCCL_ERROR("[SocketAgent::%s] Send data len failed", __func__),
25 : SocketException, "Unable to send data");
26 :
27 : // 发送数据
28 2 : CHK_PRT_THROW(!socket_->Send(data, dataLen), HCCL_ERROR("[SocketAgent::%s] Send data failed", __func__),
29 : SocketException, "Unable to send data");
30 :
31 2 : HCCL_INFO("[SocketAgent::%s] end.", __func__);
32 2 : }
33 :
34 0 : bool SocketAgent::RecvMsg(void *msg, u64 &revMsgLen)
35 : {
36 0 : HCCL_INFO("[SocketAgent::%s] start.", __func__);
37 :
38 : // 检查 msg 是否为 nullptr
39 0 : CHK_PRT_THROW(msg == nullptr,
40 : HCCL_ERROR("[RankInfoDetectService::%s] msg is nullptr", __func__),
41 : NullPtrException, "RecvMsg fail");
42 :
43 : // 先接收长度
44 0 : EXCEPTION_CATCH(socket_->Recv(&revMsgLen, sizeof(revMsgLen)), return false);
45 0 : CHK_PRT_RET(revMsgLen == 0 || revMsgLen > MAX_BUFFER_LEN,
46 : HCCL_ERROR("[SocketAgent::%s] Invalid length[%llu]", __func__, revMsgLen), false);
47 :
48 : // 再接收内容
49 0 : EXCEPTION_CATCH(socket_->Recv(msg, revMsgLen), return false);
50 :
51 0 : HCCL_INFO("[SocketAgent::%s] end, revMsgLen[%llu].", __func__, revMsgLen);
52 0 : return true;
53 : }
54 :
55 : } // namespace Hccl
|