C++中簡單使用HP-Socket
簡介
HP-Socket 是一套通用的高性能 TCP/UDP /HTTP 通訊 框架 ,包含服務端組件、客戶端組件和 Agent 組件,廣泛適用於各種不同應用場景的 TCP/UDP /HTTP 通訊系統,提供 C/C++ 、 C# 、 Delphi 、 E (易語言)、 Java 、 Python 等程式語言介面。
HP-Socket是一套國產的開源通訊庫,使用C++語言實現,提供多種程式語言的介面,支援 Windows 和 Linux 平台:
HP-Socket包含30多個組件 ,可根據通訊角色Client/Server)、通訊協議TCP/UDP/HTTP)和接收模型PUSH/PULL/PACK)進行歸類,這裡只簡單介紹一下:
- Server組件:基於IOCP/EPOLL通訊模型 ,並結合快取池 、私有堆等技術實現高效記憶體管理,支援超大規模、高並發通訊場景。
- Agent組件:實質上是Multi-Client組件,與Server組件採用相同的技術架構,可同時建立和高效處理大規模Socket連接 。
- Client組件:基於Event Select/POLL通訊模型,每個組件對象創建一個通訊執行緒並管理一個Socket連接, 適用於小規模客戶端場景。
- Thread Pool組件:HP-Socket實現的高效易用的執行緒池組件,當成普通的第三方執行緒池庫使用即可。
HP-Socket的TCP組件支援PUSH、PULL和PACK三種接收模型:
- PUSH模型:組件接收到數據時會觸發監聽器對象的OnReceive(pSender,dwConnID,pData,iLength)事件,把數據「推」給應用程式,這種模型使用起來是最自由的。
- PULL模型:組件接收到數據時會觸發監聽器對象的OnReceive(pSender,dwConnID,iTotalLength)事件 ,告訴應用程式當前已經接收到多少數據,應用程式檢查數據的長度,如果滿足需要則調用組件的**Fetch(dwConnID,pData,iDataLength)方法把需
要的數據「拉」出來。 - PACK模型:PACK模型系列組件是PUSH和PULL模型的結合體,應用程式不必處理分包與數據抓取,組件保證每個OnReceive事件都嚮應用程式提供一個完整數據包。
註:PACK模型組件會對應用程式發送的每個數據包自動加上 4 位元組(32位的包頭),前10位為用於數據包校驗的包頭標識位,後22位為記錄包體長度的長度位。
使用方式
HP-Socket支援MBCS和Unicode字符集,支援32位和64位應用程式。可以通過源程式碼、 DLL或LIB方式使用HP-Socket。 HP-Socket發行包中已經提供了HPSocket DLL和HPSocket4C DLL。
HP-Socket提供了各種情況下的dll文件,不需要我們重新編譯,dll文件按編程介面分為兩大類:
- HPSocket DLL:導出C++編程介面 ,C++程式的首選方式,使用時需要把SocketInterface.h(及其依賴文件HPTypeDef.h) 、HPSocket.h以及 DLL 對應的 *.lib 文件加入到工程項目,用到SSL組件還需要HPSocket-SSL.h文件。
- HPSocket4C DLL:導出C編程介面,提供給C語言或其它程式語言使用,使用時需要把HPSocket4C.h以及 DLL 對應的 *.lib 文件加入到工程項目,用到SSL組件還需要HPSocket4C-SSL.h文件。
實現簡單執行緒池
使用HP-Socket的執行緒池組件可以在程式中實現一個簡單的、公用的執行緒池,TCP通訊的斷線重連、發送心跳都會用到執行緒池。執行緒池組件的主要函數如下:
- Start:啟動執行緒池,具體的使用可以參考源程式碼的注釋。
- Submit:提交任務,主要使用BOOL Submit(fnTaskProc,pvArg,dwMaxWait=INFINITE),另一個函數重載是使用一個特殊的數據類型(把Socket任務參數和任務函數封裝成一個數據結構)作為參數。
- Stop:關閉執行緒池,參數dwMaxWait代表最大等待時間(毫秒,默認: INFINITE ,一直等待)。
先實現執行緒池的CHPThreadPoolListener介面,然後構造IHPThreadPool智慧指針,後面執行緒池的操作都通過智慧指針操作,程式碼如下:
class CHPThreadPoolListenerImpl : public CHPThreadPoolListener
{
private:
void LogInfo(string logStr)
{
cout <<"ThreadPool " <<logStr << endl;
}
public:
virtual void OnStartup(IHPThreadPool* pThreadPool)
{
LogInfo("執行緒池啟動");
}
virtual void OnShutdown(IHPThreadPool* pThreadPool)
{
LogInfo("執行緒池啟動關閉");
}
virtual void OnWorkerThreadStart(IHPThreadPool* pThreadPool, THR_ID dwThreadID)
{
LogInfo("[" + to_string(dwThreadID) + "] " + "工作執行緒啟動");
}
virtual void OnWorkerThreadEnd(IHPThreadPool* pThreadPool, THR_ID dwThreadID)
{
LogInfo("[" + to_string(dwThreadID) + "] " + "工作執行緒退出");
}
};
CHPThreadPoolListenerImpl ThreadPoolListener;
//全局共享變數使用extern關鍵字修飾
extern CHPThreadPoolPtr ThreadPool(&ThreadPoolListener);
實現TCP客戶端
先實現一個列印函數,顯示客戶端相關的資訊,程式碼如下:
void PrintInfo(ITcpClient* pSender, CONNID dwConnID)
{
char buffer[20];
TCHAR* ipAddr = buffer;
int ipLen;
USHORT port;
pSender->GetLocalAddress(ipAddr, ipLen, port);
cout << string(ipAddr,0,ipLen) << ":" << port << " " << " [" << dwConnID << "] -> ";
pSender->GetRemoteHost(ipAddr, ipLen, port);
cout << string(ipAddr, 0, ipLen) << ":" << port << " ";
}
實現CTcpClientListener監聽介面,客戶端斷線後自動重連,以換行符分割接收到的字元串,程式碼如下:
bool SysExit = false;
void ReConnect(ITcpClient* pSender)
{
while (pSender->GetState() != SS_STOPPED)
{
Sleep(10);
}
pSender->Start("127.0.0.1", 60000);
}
class CClientListenerImpl : public CTcpClientListener
{
public:
virtual EnHandleResult OnConnect(ITcpClient* pSender, CONNID dwConnID)
{
PrintInfo(pSender, dwConnID);
cout << "連接成功" << endl;
return HR_OK;
}
string resStr = "";
string commStr="";
virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
string str((char*)pData,0, iLength);
resStr.append(str);
int index;
while (true)
{
index = resStr.find("\r\n");
if (index == -1)break;
commStr = resStr.substr(0, index);
resStr = resStr.substr(index +2, resStr.length() - (index +2));
if (commStr!="")
{
PrintInfo(pSender, dwConnID);
cout << "收到分割字元串 " << commStr << endl;
}
}
PrintInfo(pSender, dwConnID);
cout << "數據接受 " << str << endl;
return HR_OK;
}
virtual EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
resStr = "";
PrintInfo(pSender, dwConnID);
cout << "連接斷開,"<< enOperation <<"操作導致錯誤,錯誤碼 " << iErrorCode<< endl;
if (!SysExit)
{
ThreadPool->Submit((Fn_TaskProc)(&ReConnect), (PVOID)pSender);
}
return HR_OK;
}
};
循環輸入字元串發送服務端,程式碼如下:
int main()
{
//啟動執行緒池
ThreadPool->Start();
CClientListenerImpl listener;
CTcpClientPtr client(&listener);
if (!client->Start("127.0.0.1", 60000))
{
cout << "連接錯誤:" << client->GetLastError() << "-" << client->GetLastErrorDesc();
}
string sendMsg;
while (!SysExit)
{
cin >> sendMsg;
if (sendMsg == "esc")
{
SysExit = true;
break;
}
if (client->GetState() == SS_STARTED)
{
const BYTE* data = (BYTE*)(sendMsg.c_str());
if (client->Send(data, sizeof(data)))
{
PrintInfo(client, client->GetConnectionID());
cout << "發送成功 "<<sendMsg<<endl;
}
else
{
PrintInfo(client, client->GetConnectionID());
cout << "發送失敗,錯誤描述 " << client->GetLastError() << "-" << client->GetLastErrorDesc() << endl;
}
}
else
{
PrintInfo(client, client->GetConnectionID());
cout << "無法發送,當前狀態 " <<client->GetState()<< endl;
}
}
client->Stop();
//關閉執行緒池
ThreadPool->Stop();
return 0;
}
實現TCP服務端
先實現一個列印函數,基本上和客戶端的相同,只有獲取本地IP的地方不同,程式碼如下:
void PrintInfo(ITcpServer* pSender, CONNID dwConnID)
{
char buffer[20];
TCHAR* ipAddr = buffer;
int ipLen;
USHORT port;
pSender->GetListenAddress(ipAddr, ipLen, port);
cout << string(ipAddr, 0, ipLen) << ":" << port << " " << "<- [" << dwConnID << "] ";
pSender->GetRemoteAddress(dwConnID, ipAddr, ipLen, port);
cout << string(ipAddr, 0, ipLen) << ":" << port << " ";
}
為了演示客戶端和應用數據的綁定,定義一個用戶數據類型並創建一個隊列,程式碼如下:
class UserData
{
public:
UserData(string name="")
{
Name = name;
}
string Name;
};
queue<UserData*> qName; //創建隊列對象
實現CTcpServerListener監聽介面,收到字元串後加上用戶名再發送回去,程式碼如下:
class CTcpServerListenerImpl : public CTcpServerListener
{
public:
virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient)
{
pSender->SetConnectionExtra(dwConnID,qName.front());
qName.pop();
PrintInfo(pSender, dwConnID);
cout << "連接成功" << endl;
return HR_OK;
}
virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
string str((char*)pData, 0, iLength);
PrintInfo(pSender, dwConnID);
cout << "數據接受 " << str<<endl;
PVOID pInfo = nullptr;
pSender->GetConnectionExtra(dwConnID, &pInfo);
str = "reply-" + ((UserData*)pInfo)->Name + str;
const BYTE* data = (BYTE*)(str.c_str());
pSender->Send(dwConnID, data,str.size());
return HR_OK;
}
virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
PVOID pInfo = nullptr;
pSender->GetConnectionExtra(dwConnID, &pInfo);
qName.push((UserData*)pInfo);
PrintInfo(pSender, dwConnID);
cout << "斷開連接"<< endl;
pSender->SetConnectionExtra(dwConnID, NULL);
return HR_OK;
}
};
循環輸入字元串發送到客戶端,自動回復客戶端發送的消息,程式碼如下:
bool SysExit = false;
int main()
{
UserData user1("NO1-User");
UserData user2("NO2-User");
UserData user3("NO3-User");
UserData user4("NO4-User");
qName.push(&user1);
qName.push(&user2);
qName.push(&user3);
qName.push(&user4);
CTcpServerListenerImpl listener;
CTcpServerPtr server(&listener);
if (!server->Start("127.0.0.1", 60000))
{
cout << "啟動錯誤:" << server->GetLastError() << "-" << server->GetLastErrorDesc();
}
string sendMsg;
while (!SysExit)
{
cin >> sendMsg;
if (sendMsg == "esc")
{
SysExit = true;
break;
}
//如果數組長度小於當前連接數量,則獲取失敗
DWORD count= 1000;
CONNID pIDs[1000];
ZeroMemory(pIDs, 1000);;
if (server->GetAllConnectionIDs(pIDs, count)&& count >0)
{
for (size_t i = 0; i < count; i++)
{
const BYTE* data = (BYTE*)(sendMsg.c_str());
if (server->Send(*(pIDs+i),data, sendMsg.size()))
{
PrintInfo(server, pIDs[i]);
cout << "發送成功 " << sendMsg << endl;
}
else
{
PrintInfo(server, pIDs[i]);
cout << "發送失敗,錯誤描述 " << server->GetLastError() << "-" << server->GetLastErrorDesc() << endl;
}
}
}
else
{
cout << "無法發送,當前連接數 " << count << endl;
}
}
server->Stop();
}
註:獲取連接時指針數組的長度一定要大於當前連接數量,否則會失敗。
實現Http客戶端
HP-Socket的Http客戶端有同步、非同步兩種,同步客戶端不需要綁定監聽器,這裡使用同步客戶端演示。
Sync Client:同步HTTP客戶端組件(CHttpSyncClient和CHttpsSyncClient)內部會處理所有事件,因此,它們不需要綁定監聽器(構造方法的監聽器參數傳入null); 如果綁定了監聽器則可以跟蹤組件的通訊過程。
測試客戶端可以使用實時天氣介面上面的測試示例,當前的測試示例為:
//api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
直接開始測試,程式碼如下:
int main()
{
CHttpSyncClientPtr SyncClient;
THeader type;
type.name = "Content-Type";
type.value = "text/html;charset=UTF-8";
if (SyncClient->OpenUrl("GET", "//api.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json",&type))
{
LPCBYTE pData = nullptr;
int iLength = 0;
SyncClient->GetResponseBody(&pData, &iLength);
string body((char*)pData, iLength);
//返回的有中文,需要轉化編碼格式
cout << body << endl;
cout << endl;
cout << StringToUtf(body) << endl;
cout << endl;
cout << UtfToString(StringToUtf(body)) << endl;
}
else
{
cout << "打開失敗:"<<SyncClient->GetLastError()<<"-"<< SyncClient->GetLastErrorDesc()<<endl;
}
}
上面的StringToUtf和UtfToString函數是轉載至C++ 中文亂碼的問題,該函數實現UTF-8和ANSI編碼格式的轉化,程式碼如下:
string UtfToString(string strValue)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, strValue.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1];//加上末尾'\0'
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, strValue.c_str(), strValue.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete[]pwBuf;
delete[]pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
string StringToUtf(string strValue)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1];//加上末尾'\0'
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, strValue.c_str(), strValue.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete[]pBuf;
delete[]pwBuf;
return retStr;
}
註:函數實現需放在main函數之前。