HP-Socket高性能TCP/UDP/HTTP 通信框架

为了让使用者能方便快速地学习和使用HP-Socket,迅速掌握框架的设计思想和使用方法,特此精心制作了大量Demo示例(如:PUSH模型示例、PULL模型示例、PACK模型示例、性能测试示例以及其它编程语言示例)。HP-Socket当前支持Windows和Linux平台。

工作流程

创建监听器对象
创建组件对象(并绑定监听器)
启动组件
连接远程主机(仅用于Agent组件)
处理通信事件(OnConnect/OnReceive/OnClose ......)
停止组件(可选,第7步销毁组件对象前会先停止组件)
销毁组件对象
销毁监听器对象

示例代码

C++

#include "hpsocket/HPSocket.h"

/* Listener Class */
class CListenerImpl : public CTcpPullServerListener
{
 
public:
// 5. process network events
virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen);
virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient);
virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID);
virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, int iLength);
virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
virtual EnHandleResult OnShutdown(ITcpServer* pSender);
};
 
int main(int argc, char* const argv[])
{
// 1. Create listener object
CListenerImpl s_listener;
// 2. Create component object (and binding with listener object)
CTcpPullServerPtr s_pserver(&s_listener);
 
// 3. Start component object
if(!s_pserver->Start("0.0.0.0", 5555))
exit(1);
 
/* wait for exit */
// ... ... 
 
// 6. (optional) Stop component object
s_pserver->Stop()
 
return 0;
 
// 7. Destroy component object automatically
// 8. Destroy listener object automatically
}

C

#include "hpsocket/HPSocket4C.h"

// 5. process network events
EnHandleResult __HP_CALL OnConnect(HP_Agent pSender, HP_CONNID dwConnID);
EnHandleResult __HP_CALL OnReceive(HP_Agent pSender, HP_CONNID dwConnID, int iLength);
EnHandleResult __HP_CALL OnSend(HP_Agent pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength);
EnHandleResult __HP_CALL OnClose(HP_Agent pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode);
EnHandleResult __HP_CALL OnShutdown(HP_Agent pSender);
 
int main(int argc, char* const argv[])
{
HP_TcpPullAgentListener s_listener;
HP_TcpPullAgent s_agent;
 
// 1. Create listener object
s_listener = ::Create_HP_TcpPullAgentListener();
// 2. Create component object (and binding with listener object)
s_agent    = ::Create_HP_TcpPullAgent(s_listener);
 
/* Set listener callbacks */
::HP_Set_FN_Agent_OnConnect(s_listener, OnConnect);
::HP_Set_FN_Agent_OnSend(s_listener, OnSend);
::HP_Set_FN_Agent_OnPullReceive(s_listener, OnReceive);
::HP_Set_FN_Agent_OnClose(s_listener, OnClose);
::HP_Set_FN_Agent_OnShutdown(s_listener, OnShutdown);
 
// 3. Start component object
if(::HP_Agent_HasStarted(s_agent))
exit(1);
 
// 4. Connect to dest host
::HP_Agent_Connect(s_agent, "remote.host.1", REMOTE_PORT_1, nullptr);
::HP_Agent_Connect(s_agent, "remote.host.2", REMOTE_PORT_2, nullptr);
::HP_Agent_Connect(s_agent, "remote.host.3", REMOTE_PORT_3, nullptr);
 
/* wait for exit */
// ... ... 
 
// 6. (optional) Stop component object
::HP_Agent_Stop(s_agent)
 
// 7. Destroy component object
::Destroy_HP_TcpPullAgent(s_agent);
// 8. Destroy listener object
::Destroy_HP_TcpPullAgentListener(s_listener);
 
return 0;
}

版本及下载地址

HP-Socket v5.4.1 https://github.com/ldcsaa/HP-Socket
HP-Socket v5.3.5 https://codeload.github.com/ldcsaa/HP-Socket/zip/v3.5.3
HP-Socket v5.3.2 https://www.oschina.net/news/97435/hp-socket-5-3-2-released


发布日期:

所属分类: 易语言 标签: