HP Socket Pack笔记

非4C 静态库使用Pack服务端
使用 HPSocket 静态库时,需要在工程属性中定义预处理宏 -> HPSOCKET_STATIC_LIB

stdafx.h
#include "../../HPLib/Common/GeneralHelper.h"

dlg.h

#include "afxwin.h"
#include "../HPLib/Global/helper.h"
#include "../HPLib/HPSocket/HPSocket.h"

//继承的虚函数
private:
virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen);
virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, SOCKET soClient);
virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
virtual EnHandleResult OnReceive(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);

dlg.cpp注意设置

m_Server->SetMaxPackSize(0xFFF);
m_Server->SetPackHeaderFlag(0x169);

EnHandleResult CServerDlg::OnPrepareListen(ITcpServer* pSender, SOCKET soListen)
{
    TCHAR szAddress[50];
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;

    pSender->GetListenAddress(szAddress, iAddressLen, usPort);
    ::PostOnPrepareListen(szAddress, usPort);
    return HR_OK;
}

EnHandleResult CServerDlg::OnAccept(ITcpServer* pSender, CONNID dwConnID, SOCKET soClient)
{
    BOOL bPass = TRUE;
    TCHAR szAddress[50];
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;

    pSender->GetRemoteAddress(dwConnID, szAddress, iAddressLen, usPort);

    if(!m_strRejectAddress.IsEmpty())
    {
        if(m_strRejectAddress.CompareNoCase(szAddress) == 0)
            bPass = FALSE;
    }

    ::PostOnAccept(dwConnID, szAddress, usPort, bPass);

    return bPass ? HR_OK : HR_ERROR;
}

EnHandleResult CServerDlg::OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
    //static int t = 0;
    //if(++t % 3 == 0) return HR_ERROR;

    ::PostOnSend(dwConnID, pData, iLength);
    return HR_OK;
}

EnHandleResult CServerDlg::OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
    //static int t = 0;
    //if(++t % 3 == 0) return HR_ERROR;

    ::PostOnReceive(dwConnID, pData, iLength);

    if(pSender->Send(dwConnID, pData, iLength))
        return HR_OK;
    else
        return HR_ERROR;
}

EnHandleResult CServerDlg::OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
    iErrorCode == SE_OK ? ::PostOnClose(dwConnID)   :
    ::PostOnError(dwConnID, enOperation, iErrorCode);

    return HR_OK;
}

EnHandleResult CServerDlg::OnShutdown(ITcpServer* pSender)
{
    ::PostOnShutdown();
    return HR_OK;
}

你可能感兴趣的:(MFC)