High Performance TCP/UDP/HTTP Communication Component
High Performance Network Framework
Description
- Server Based on IOCP/EPOLL communication model, combined with technology of memory pool, private heap etc., efficient memory management is implemented to support large scale and high concurrent communication scenarios.
- Agent The Agent component is essentially a Multi-Client component that uses the same technical architecture as the Server component. An Agent component object can create and efficiently handle large-scale Socket connections at the same time.
- Client Based on Event-Select/POLL communication model, each component object creates a communication thread and manages a Socket connection. Client components are suitable for small-scale client scenarios.
Document
- HP-Socket Development Guide
- HP-Socket Class Diagram
- HP-Socket Class Diagram
- HP-Socket SSL Class Diagram
- HP-Socket HTTP Class Diagram
Workflow
- Create listener object
- Create component object (and binding with listener object)
- Start component object
- Connect to dest host (for Agent Component only)
- process network events (OnConnect/OnReceive/OnClose etc.)
- Stop component object (optional: component object will be stopped before destroy in step 7)
- Destroy component object
- Destroy listener object
Example
- C++ Example
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 spserver(&slistener); // 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 Example
C
#include <hpsocket/HPSocket4C.h>
// 5. process network events EnHandleResult _HPCALL OnConnect(HPAgent pSender, HPCONNID dwConnID); EnHandleResult _HPCALL OnReceive(HPAgent pSender, HPCONNID dwConnID, int iLength); EnHandleResult _HPCALL OnSend(HPAgent pSender, HPCONNID dwConnID, const BYTE* pData, int iLength); EnHandleResult _HPCALL OnClose(HPAgent pSender, HPCONNID dwConnID, EnHPSocketOperation enOperation, int iErrorCode); EnHandleResult _HPCALL OnShutdown(HP_Agent pSender);
int main(int argc, char* const argv[]) { HPTcpPullAgentListener slistener; HPTcpPullAgent sagent;
// 1. Create listener object slistener = ::CreateHP_TcpPullAgentListener(); // 2. Create component object (and binding with listener object) sagent = ::CreateHPTcpPullAgent(slistener); / Set listener callbacks / ::HPSetFNAgentOnConnect(s_listener, OnConnect); ::HPSetFNAgentOnSend(s_listener, OnSend); ::HPSetFNAgentOnPullReceive(s_listener, OnReceive); ::HPSetFNAgentOnClose(s_listener, OnClose); ::HPSetFNAgentOnShutdown(s_listener, OnShutdown); // 3. Start component object if(!::HPAgentStart(s_agent, "0.0.0.0", TRUE)) exit(1); // 4. Connect to dest host ::HPAgentConnect(sagent, REMOTEHOST1, REMOTEPORT_1, nullptr); ::HPAgentConnect(sagent, REMOTEHOST2, REMOTEPORT_2, nullptr); ::HPAgentConnect(sagent, REMOTEHOST3, REMOTEPORT_3, nullptr); / wait for exit / // ... ... // 6. (optional) Stop component object ::HPAgentStop(s_agent);
// 7. Destroy component object ::DestroyHPTcpPullAgent(s_agent); // 8. Destroy listener object ::DestroyHPTcpPullAgentListener(s_listener); return 0; }
Component List
- Basic Components
- SSL Components
- HTTP Components