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