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 : #ifndef HCCLV2_SOCKET_BASE_H
11 : #define HCCLV2_SOCKET_BASE_H
12 :
13 : #include <chrono>
14 :
15 : #include "ip_address.h"
16 : #include "socket_config.h"
17 : #include "orion_adapter_hccp.h"
18 :
19 : namespace Hccl {
20 :
21 837877 : MAKE_ENUM(SocketStatus, INIT, LISTEN_STARTING, LISTENING, CONNECT_STARTING, CONNECTING, SENDING, RECVING, OK, TIMEOUT)
22 :
23 1018 : MAKE_ENUM(NicType, DEVICE_NIC_TYPE, HOST_NIC_TYPE, DEVICE_VNIC_TYPE)
24 :
25 : using FdHandle = void *;
26 : class Socket {
27 : public:
28 608 : Socket(SocketHandle socketHandle, IpAddress localIp, u32 listenPort, IpAddress remoteIp, const std::string &tag,
29 : SocketRole role, NicType nicType)
30 608 : : socketHandle(socketHandle), localIp(localIp), listenPort(listenPort), remoteIp(remoteIp), tag(tag),
31 608 : role(role), nicType(nicType)
32 : {
33 608 : }
34 :
35 : virtual ~Socket();
36 :
37 : virtual void Listen();
38 : virtual void Connect();
39 : SocketStatus GetStatus(u32 timeout = 0);
40 :
41 9 : virtual SocketRole GetRole() const
42 : {
43 9 : return role;
44 : }
45 0 : virtual IpAddress GetRemoteIp()
46 : {
47 0 : return remoteIp;
48 : }
49 : void Destroy();
50 : void Close();
51 : void StopListen();
52 :
53 : bool Send(const void *sendBuf, u32 size) const;
54 : bool Recv(void *recvBuf, u32 size) const;
55 :
56 : bool Listen(u32 &port);
57 : bool ISend(void *data, u64 size, u64& compSize) const;
58 :
59 : HcclResult ISendWithHeart(void *data, u64 size, u64& compSize) const;
60 : HcclResult IRecvWithHeart(void *data, u64 size, u64& compSize) const;
61 :
62 : bool IsListen() const
63 : {
64 : return isListening;
65 : }
66 :
67 : SocketStatus GetAsyncStatus();
68 :
69 : void ListenAsync();
70 : void ConnectAsync();
71 : void SendAsync(const void *sendBuf, u32 size);
72 : void RecvAsync(u8 *recvBuf, u32 size);
73 :
74 16 : FdHandle GetFdHandle() const // will be used in hccp QP connecting
75 : {
76 16 : return fdHandle;
77 : }
78 :
79 35 : NicType GetNicType() const
80 : {
81 35 : return nicType;
82 : }
83 :
84 90 : IpAddress GetLocalIp()
85 : {
86 90 : return localIp;
87 : }
88 :
89 46 : u32 GetListenPort() const
90 : {
91 46 : return listenPort;
92 : }
93 :
94 97 : string Describe()
95 : {
96 : return StringFormat("Socket[role=%s, localIp=%s, listenPort=%u, remoteIp=%s, tag=%s, nicType=%s]",
97 194 : role.Describe().c_str(), localIp.Describe().c_str(), listenPort,
98 291 : remoteIp.Describe().c_str(), tag.c_str(), nicType.Describe().c_str());
99 : }
100 :
101 : private:
102 : SocketHandle socketHandle{nullptr}; // vnic/nic创建的handle,HCCP初始化返回的handle_
103 : IpAddress localIp;
104 : u32 listenPort{0};
105 : IpAddress remoteIp;
106 : const std::string tag;
107 : SocketRole role{SocketRole::CLIENT};
108 : FdHandle fdHandle{nullptr};
109 : SocketStatus socketStatus{SocketStatus::INIT};
110 : NicType nicType{NicType::INVALID};
111 : bool isConnected{false};
112 : bool isListening{false};
113 : bool isDestroyed{false};
114 : std::unique_ptr<SocketListenInfoT> listenInfo_{nullptr};
115 :
116 : std::chrono::steady_clock::time_point lastLogTime{}; // 抑制日志刷屏时间戳,刷新时可置空
117 :
118 : RequestHandle reqHandle{0};
119 :
120 : const void *sendDataBuff{nullptr}; // 发送缓冲区的起始地址,需要调用方保证内存生命周期
121 : unsigned long long sendSize{0}; // 调用Send接口入参,返回接口调用后实际发送的数据量
122 : u32 sendLeftSize{0}; // 发送缓冲区剩余待发送数据量
123 : u32 totalSendSize{0}; // 发送缓冲区已发送总数据量
124 :
125 : void *recvDataBuff{nullptr}; // 接受缓冲区的起始地址,需要调用方保证内存生命周期
126 : unsigned long long recvSize{0}; // 调用Recv接口入参,返回接口调用后实际接受的数据量
127 : u32 recvLeftSize{0}; // 接受缓冲区剩余待接受数据量
128 : u32 totalRecvSize{0}; // 接受缓冲区已接受总数据量
129 :
130 : void GetOneSocket();
131 :
132 : bool CheckStartRequestResult();
133 : bool CheckSendRequestResult();
134 : bool CheckRecvRequestResult();
135 : void PrintErrorSocketInfo();
136 : };
137 :
138 : } // namespace Hccl
139 :
140 : #endif // HCCLV2_SOCKET_BASE_H
|