// TestEcho-SSL-Console Server
// 基于HP-Socket的SSL服务器控制台程序

#include "pch.h"

// SSL证书配置（来自helper.cpp）

// 服务端证书
static const char* g_s_lpszPemCert = R"(-----BEGIN CERTIFICATE-----
MIIEJjCCAw6gAwIBAgIBAzANBgkqhkiG9w0BAQsFADB7MQswCQYDVQQGEwJDTjEL
MAkGA1UECAwCR0QxCzAJBgNVBAcMAkdaMQwwCgYDVQQKDANTU1QxDzANBgNVBAsM
Bkplc3NtYTETMBEGA1UEAwwKamVzc21hLm9yZzEeMBwGCSqGSIb3DQEJARYPbGRj
c2FhQDIxY24uY29tMCAXDTI0MDYyNjA1MTY1NFoYDzIyNDMwNzA5MDUxNjU0WjBu
MQswCQYDVQQGEwJDTjELMAkGA1UECAwCR0QxDDAKBgNVBAoMA1NTVDEPMA0GA1UE
CwwGSmVzc21hMRMwEQYDVQQDDApqZXNzbWEub3JnMR4wHAYJKoZIhvcNAQkBFg9s
ZGNzYWFAMjFjbi5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC7
x3ilLjZtH2ZKuofj4FpVl/IF2yDI5503YQbwllxp7kNEaqTyjJUgmLlZBbwHQzTD
xfPk/nZ/m3xUHsVjwXMZqNNufgtSLaBoK4CvBAOTkELphIOZdJYPpuaU66W0phjG
VM2R4EFm/rTXddZ7N6sq3fYEL0bxqUJ6fW/+0QhdNSwfdevdAHgOmGkrTj5rILJ8
A7FwbkcuV5vBWZ+9ZhNG4csqAjH5LLLCn5hJdhE9WqUp+slfIuXE5vZGDpCQrcc5
I1qWt8VNfdwzaBDL/hXl7pAiVpZRvQxyJgbUMLr2QrYFwrPkgpncU7R7AyT/C0tO
vgPVZb+IGqbf+NrbHEk3AgMBAAGjgb8wgbwwHwYDVR0jBBgwFoAU3j3PjwPLCagP
hw1NCALqefZAmhQwCQYDVR0TBAIwADALBgNVHQ8EBAMCBPAwYgYDVR0RBFswWYIJ
bG9jYWxob3N0ggpqZXNzbWEub3JnggwqLmplc3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8kf08jtbnFvnm4ibUkQL5h+OJoIEQa9AVZOSoFG2/g5Zcn8X8C
AwEAAaNTMFEwHQYDVR0OBBYEFN49z48DywmoD4cNTQgC6nn2QJoUMB8GA1UdIwQY
MBaAFN49z48DywmoD4cNTQgC6nn2QJoUMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZI
hvcNAQELBQADggEBALJnYrYBSZLyYX14FQ04zxG3AX0CtQzNOOa7LDrr+H8Ly+nK
qS87gg2njMVZH1zM2demtMwydR/F2Ui8ggaduMvc9h5YgQKEwYl8KarJEY03oZoe
zbQGBxCXpDOtMs1vujzcl/iZbSzwEDF3g4la5U8q4MlmfGFKz9CJbvoxecqYA206
nNbW2XZsW/xMiQv6iAw5iP/LOR9HAyxcvXIsL790nfcgnTYLmyP254Dj4outc6R+
PA+f/c1FvkbUBTR5vJt2tsvHcNU218rY2hyOIhDmZeUWprqBO19sUk3scLbVPr3+
WEWEl2XaCekKuPtAnMgVQuFsocXGyiuIhkOe5Z4=
-----END CERTIFICATE-----
)";

static const int g_s_iVerifyMode = SSL_VM_PEER | SSL_VM_FAIL_IF_NO_PEER_CERT;
static const char* g_s_lpszKeyPassword = "123456";
static const LPCTSTR ADDRESS = _T("0.0.0.0");
static const USHORT PORT = 5555;

// 读取文件内容到字节向量
inline std::vector<unsigned char> readFile(const std::string& path) {
	std::ifstream f(path, std::ios::binary);
	return std::vector<unsigned char>(std::istreambuf_iterator<char>(f), {});
}
// 服务器监听器类
class CServerListener : public CTcpServerListener
{
public:
	virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen) override
	{
		TCHAR szAddress[100];
		int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
		USHORT usPort;
		pSender->GetListenAddress(szAddress, iAddressLen, usPort);
		
		std::wcout << L"[服务器] 准备监听: " << szAddress << L":" << usPort << std::endl;
		return HR_OK;
	}

	virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient) override
	{
		TCHAR szAddress[100];
		int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
		USHORT usPort;
		pSender->GetRemoteAddress(dwConnID, szAddress, iAddressLen, usPort);
		
		std::wcout << L"[服务器] 客户端连接: ID=" << dwConnID 
			<< L", 地址=" << szAddress << L":" << usPort << std::endl;
		
		return HR_OK;
	}

	virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID) override
	{
		std::wcout << L"[服务器] SSL握手完成: ID=" << dwConnID << std::endl;
		return HR_OK;
	}

	virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override
	{
		std::string strData((char*)pData, iLength);
		std::cout << "[服务器] 接收到消息: ID=" << dwConnID << ", 内容=\"" << strData << " from client\"" << std::endl;

		// 回显数据给客户端
		std::string response = "hello!";
		std::vector<unsigned char> fileData = readFile("Client.exe");
		//if (pSender->Send(dwConnID, fileData.data(), (int)fileData.size()))
		if (pSender->Send(dwConnID, (const BYTE*)response.c_str(), (int)response.size()))
		{
			std::cout << "[服务器] 发送数据，长度：" << response.size() << std::endl;
		}
		else
		{
			std::cout << "[服务器] 发送响应失败" << std::endl;
		}

		return HR_OK;
	}

	virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override
	{
		return HR_OK;
	}

	virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override
	{
		if (iErrorCode == SE_OK)
		{
			std::wcout << L"[服务器] 客户端断开: ID=" << dwConnID << std::endl;
		}
		else
		{
			std::wcout << L"[服务器] 客户端连接错误: ID=" << dwConnID 
				<< L", 错误码=" << iErrorCode << std::endl;
		}
		
		return HR_OK;
	}

	virtual EnHandleResult OnShutdown(ITcpServer* pSender) override
	{
		std::wcout << L"[服务器] 服务器关闭" << std::endl;
		return HR_OK;
	}
};

int main()
{
	// 设置控制台UTF-8编码和locale
	SetConsoleOutputCP(CP_UTF8);
	std::locale::global(std::locale(""));
	std::wcout.imbue(std::locale(""));
	
	std::wcout << L"========================================" << std::endl;
	std::wcout << L"  HP-Socket SSL Server 控制台演示程序" << std::endl;
	std::wcout << L"========================================" << std::endl;
	std::wcout << std::endl;

	// 创建监听器和服务器
	CServerListener listener;
	CSSLServerPtr server(&listener);

	// 初始化SSL上下文
	std::wcout << L"[服务器] 正在初始化SSL环境..." << std::endl;
	if (!server->SetupSSLContextByMemory(g_s_iVerifyMode, g_s_lpszPemCert, g_s_lpszPemKey, 
		g_s_lpszKeyPassword, g_s_lpszCAPemCert))
	{
		std::wcout << L"[服务器] SSL初始化失败！错误码: " << server->GetLastError() << std::endl;
		std::wcout << L"按任意键退出..." << std::endl;
		_getch();
		return 1;
	}
	std::wcout << L"[服务器] SSL环境初始化成功" << std::endl;

	// 启动服务器
	std::wcout << L"[服务器] 正在启动服务器..." << std::endl;
	if (!server->Start(ADDRESS, PORT))
	{
		std::wcout << L"[服务器] 启动失败！错误码: " << server->GetLastError() << std::endl;
		std::wcout << L"按任意键退出..." << std::endl;
		_getch();
		return 1;
	}
	
	std::wcout << L"[服务器] 服务器启动成功，监听端口: " << PORT << std::endl;
	std::wcout << L"[服务器] 按 'Q' 键停止服务器..." << std::endl;
	std::wcout << std::endl;

	// 等待用户输入
	while (true)
	{
		if (_kbhit())
		{
			char ch = _getch();
			if (ch == 'q' || ch == 'Q')
			{
				break;
			}
		}
		Sleep(100);
	}

	// 停止服务器
	std::wcout << L"[服务器] 正在停止服务器..." << std::endl;
	if (server->Stop())
	{
		std::wcout << L"[服务器] 服务器已停止" << std::endl;
	}

	// 清理SSL上下文
	server->CleanupSSLContext();

	std::wcout << L"按任意键退出..." << std::endl;
	_getch();
	
	return 0;
}
