#pragma once
#include "pch.h"
#include "CertificateManager.h"
namespace SSLClient {
///
/// SSL客户端类
/// 封装SSL连接、数据发送接收功能
///
class SSLClientConnection
{
public:
SSLClientConnection();
~SSLClientConnection();
// 禁止拷贝
SSLClientConnection(const SSLClientConnection&) = delete;
SSLClientConnection& operator=(const SSLClientConnection&) = delete;
///
/// 初始化SSL环境
///
/// 客户端证书PEM
/// 客户端私钥PEM
/// CA证书PEM
/// 私钥密码
/// 成功返回true
bool Initialize(const char* clientCert, const char* clientKey,
const char* caCert, const char* keyPassword);
///
/// 连接到服务器
///
/// 服务器地址
/// 服务器端口
/// 成功返回true
bool Connect(const char* address, int port);
///
/// 发送数据
///
/// 要发送的数据
/// 成功返回true
bool Send(const std::string& data);
///
/// 接收数据(非阻塞)
///
/// 接收缓冲区
/// 缓冲区大小
/// 接收到的字节数,-1表示错误,0表示没有数据
int Receive(char* buffer, int bufferSize);
///
/// 断开连接
///
void Disconnect();
///
/// 检查是否已连接
///
bool IsConnected() const { return m_isConnected; }
///
/// 获取使用的加密套件
///
const char* GetCipherSuite() const;
private:
///
/// 初始化Winsock
///
bool InitializeWinsock();
///
/// 清理资源
///
void Cleanup();
private:
SSL_CTX* m_sslContext;
SSL* m_ssl;
SOCKET m_socket;
bool m_isConnected;
CertificateManager m_certManager;
};
} // namespace SSLClient