HP Socket 服务端面向对象例子

ergou520 2020-1-4 1398

官方的服务端例子都是4C的,我给大家发给PACK服务端的例子。能极大的方便C++写服务端 

*****************************************************

用 future 类结合HP可以实现RPC,也就是同步发数据等待返回结果,这里不写了,可以自己研究

*****************************************************

#pragma once

#define  HPSOCKET_STATIC_LIB

#include "HPSocket.h"

#include <stdio.h>

class ServerClass2 : public CTcpServerListener

{

public:

ServerClass2():m_server(this)

{

 

 

}

void Init()

{

m_server->SetPackHeaderFlag(0x169);

m_server->SetMaxPackSize(0x1FFF);

if (m_server->Start("0.0.0.0", 8888))

{

printf("Success Start Server!\n");

}

else

{

printf("Erro Start Server!\n");

getchar();

}

}

~ServerClass2()

{

}

CTcpPackServerPtr m_server;

virtual EnHandleResult OnPrepareListen(ITcpPackServer* pSender, SOCKET soListen) { printf(__FUNCDNAME__); return HR_IGNORE; }

virtual EnHandleResult OnAccept(ITcpPackServer* pSender, CONNID dwConnID, UINT_PTR soClient) { printf(__FUNCDNAME__); return HR_IGNORE; }

virtual EnHandleResult OnHandShake(ITcpPackServer* pSender, CONNID dwConnID) { printf(__FUNCDNAME__); return HR_IGNORE; }

virtual EnHandleResult OnReceive(ITcpPackServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) { printf(__FUNCDNAME__); return HR_IGNORE; }

virtual EnHandleResult OnSend(ITcpPackServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) { printf(__FUNCDNAME__); return HR_IGNORE; }

virtual EnHandleResult OnShutdown(ITcpPackServer* pSender) { return HR_IGNORE; }

virtual EnHandleResult OnClose(ITcpPackServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) { printf(__FUNCDNAME__); return HR_IGNORE; }

};




*****************************************************************************

//方式2  用4C封装  但是很麻烦 

//头文件

#pragma once

#define  HPSOCKET_STATIC_LIB

#include "HPSocket4C.h"

#include <functional>

using namespace std;

//c++17 inline防止全局变量在头文件中无法编译

inline std::function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, SOCKET soClient)> fn_OnAccept = NULL;

inline std::function<En_HP_HandleResult(HP_Server pSender, SOCKET soListen)> fn_OnPrepareListen = NULL;

inline std::function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, const BYTE * pData, int iLength)> fn_OnSend = NULL;

inline std::function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, const BYTE * pData, int iLength)> fn_OnReceive = NULL;

inline std::function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode)> fn_OnClose = NULL;

inline std::function<En_HP_HandleResult(HP_Server pSender)> fn_OnShutdown= NULL;;

class ServerClass

{

public:

ServerClass();

~ServerClass();

void Start()

{

::HP_TcpPackServer_SetMaxPackSize(m_pServer, 0x1FFF);

::HP_TcpPackServer_SetPackHeaderFlag(m_pServer, 0x169);

if (::HP_Server_Start(m_pServer, "0.0.0.0", 8888))

{

printf("Success Start Server!\n");

}

else

{

printf("Erro Start Server!\n");

getchar();

}

}

void SetCallBack_OnPrepareListen(function<En_HP_HandleResult(HP_Server pSender, SOCKET soListen)> fn)

{

fn_OnPrepareListen = fn;

}

void SetCallBack_OnAccept(function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, SOCKET soClient)>fn)

{

if (!fn)

{

printf("error fn");

}

fn_OnAccept = fn;

}

void SetCallBack_OnSend(function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, const BYTE * pData, int iLength)> fn)

{

fn_OnSend = fn;

}

void SetCallBack_OnReceive(function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, const BYTE * pData, int iLength)> fn)

{

fn_OnReceive= fn;

}

void SetCallBack_OnClose(function<En_HP_HandleResult(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode)> fn)

{

fn_OnClose = fn;

}

void SetCallBack_OnShutdown(function<En_HP_HandleResult(HP_Server pSender)> fn)

{

fn_OnShutdown = fn;

}

private:

HP_TcpPackServer m_pServer;

HP_TcpServerListener m_pListener;

static En_HP_HandleResult __stdcall OnPrepareListen(HP_Server pSender, SOCKET soListen)

{

if (fn_OnPrepareListen !=NULL)

{

return fn_OnPrepareListen( pSender,  soListen);

}

return HR_IGNORE;

}

static En_HP_HandleResult __stdcall OnAccept(HP_Server pSender, HP_CONNID dwConnID, SOCKET soClient)

{

if (fn_OnAccept)

{

return fn_OnAccept(pSender, dwConnID, soClient);

}

else

{

printf("fn_OnAccept=null\n");

}

return HR_IGNORE;

}

static En_HP_HandleResult __stdcall OnSend(HP_Server pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength)

{

if (fn_OnSend!=NULL)

{

return fn_OnSend(pSender, dwConnID, pData, iLength);

}

return HR_IGNORE;

}

static En_HP_HandleResult __stdcall OnReceive(HP_Server pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength)

{

if (fn_OnReceive!=NULL)

{

return fn_OnReceive(pSender, dwConnID, pData, iLength);

}

return HR_IGNORE;

}

static En_HP_HandleResult __stdcall OnClose(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode)

{

if (fn_OnClose!=NULL)

{

return fn_OnClose(pSender, dwConnID, enOperation, iErrorCode);

}

return HR_IGNORE;

}

static En_HP_HandleResult __stdcall OnShutdown(HP_Server pSender)

{

if (fn_OnShutdown!=NULL)

{

return fn_OnShutdown(pSender);

}

return HR_IGNORE;

}

};

**********************************************************************

//CPP

ServerClass::ServerClass()

{

// 创建监听器对象

m_pListener = ::Create_HP_TcpPackServerListener();

// 创建 Socket 对象

m_pServer = ::Create_HP_TcpPackServer(m_pListener);

// 设置 Socket 监听器回调函数

::HP_Set_FN_Server_OnPrepareListen(m_pListener, OnPrepareListen);

::HP_Set_FN_Server_OnAccept(m_pListener, OnAccept);

::HP_Set_FN_Server_OnSend(m_pListener, OnSend);

::HP_Set_FN_Server_OnReceive(m_pListener, OnReceive);

::HP_Set_FN_Server_OnClose(m_pListener, OnClose);

::HP_Set_FN_Server_OnShutdown(m_pListener, OnShutdown);

}

ServerClass::~ServerClass()

{

}


********************************************************************

最新回复 (0)
返回