猫猫 发表于 2022-10-8 19:15:14

QT使用HPSocket,QTHPSocketDemo


QT使用HPSocket,QTHPSocketDemo

#include "ServerListener.h"

#include <QDebug>
#include <QString>
#include <QByteArray>
ServerListener* ServerListener::mServerListener=nullptr;
std::mutex ServerListener::mMutex;
ServerListener::ServerListener()
    :mServerView(nullptr),
   mServer(this)
{

}






ServerListener *ServerListener::getInstance()
{
    ServerListener* tmp = mServerListener;
    if (tmp == nullptr)
    {
      std::lock_guard<std::mutex> lock(mMutex);
      tmp = mServerListener;
      if (tmp == nullptr)
      {
            tmp = new ServerListener();
            mServerListener = tmp;
      }
    }
    return mServerListener;
}



void ServerListener::registerServerView(IServerView *serverView)
{
    mServerView=serverView;
}

void ServerListener::unRegisterServerView(IServerView *serverView)
{
    if(mServerView==serverView)
      mServerView=nullptr;
}

EnHandleResult ServerListener::OnPrepareListen(ITcpServer *pSender, SOCKET soListen)
{

    TCHAR szAddress;
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;
    pSender->GetListenAddress(szAddress,iAddressLen,usPort);

    QString serverAddr=QString::fromWCharArray(szAddress);

    if(mServerView!=nullptr)
    {
      mServerView->onPrepareListen(serverAddr,usPort);
    }

    return EnHandleResult::HR_OK;
}

EnHandleResult ServerListener::OnAccept(ITcpServer *pSender, CONNID dwConnID, SOCKET soClient)
{
    TCHAR szAddress;
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;

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

    QString localAddr=QString::fromWCharArray(szAddress);

    TCHAR remoteAddress;
    int remoteAddressLen =sizeof(remoteAddress) / sizeof(TCHAR);
    USHORT remotePort;


    pSender->GetRemoteAddress(dwConnID,remoteAddress,remoteAddressLen,remotePort);

    QString remoteAddr=QString::fromWCharArray(remoteAddress);


    if(mServerView!=nullptr)
    {
      mServerView->onAccept(localAddr,usPort,remoteAddr,remotePort,dwConnID);
    }



    return EnHandleResult::HR_OK;
}

EnHandleResult ServerListener::OnSend(ITcpServer *pSender, CONNID dwConnID, const BYTE *pData, int iLength)
{

    return EnHandleResult::HR_OK;
}

EnHandleResult ServerListener::OnReceive(ITcpServer *pSender, CONNID dwConnID, const BYTE *pData, int iLength)
{
    TCHAR szAddress;
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;
    pSender->GetLocalAddress(dwConnID,szAddress,iAddressLen,usPort);
    QString localAddr=QString::fromWCharArray(szAddress);
    QByteArray data =QByteArray((const char*)pData,iLength);


    if(mServerView!=nullptr)
    {
      mServerView->onReceive(localAddr,usPort,data,iLength,dwConnID);
    }


    return EnHandleResult::HR_OK;
}

EnHandleResult ServerListener::OnClose(ITcpServer *pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
    if(mServerView!=nullptr)
    {
      mServerView->onClose();
    }

    return EnHandleResult::HR_OK;
}

EnHandleResult ServerListener::OnShutdown(ITcpServer *pSender)
{
    if(mServerView!=nullptr)
    {
      mServerView->onShutdown();
    }

    return EnHandleResult::HR_OK;
}




**** Hidden Message *****

woshicp 发表于 2022-12-26 17:35:47

看看隐藏

chiyuba 发表于 2023-1-29 09:20:37

看看隐藏内容

netweather 发表于 2023-3-18 09:13:21


看看隐藏内容

小飞侠 发表于 2023-11-20 17:31:01

看一下内容

小飞侠 发表于 2023-11-20 18:11:07

必须给点个赞:loveliness:

yeshi_long 发表于 2023-11-22 14:29:47

支持vs编译器?不支持mingw的?

yt2024 发表于 2024-9-9 15:19:39

学习一下!
页: [1]
查看完整版本: QT使用HPSocket,QTHPSocketDemo