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