HPsocket

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 48|回复: 0

[C++] QT使用HPSocket,QTHPSocketDemo

[复制链接]
发表于 2022-10-8 19:15:14 | 显示全部楼层 |阅读模式

2 }/ Y9 z3 y; cQT使用HPSocket,QTHPSocketDemo
  e# V. F2 w3 i2 ]9 V- c
$ q; o7 @; h) a. n8 g
[C#] 纯文本查看 复制代码
#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[50];
    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[50];
    int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
    USHORT usPort;

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

    QString localAddr=QString::fromWCharArray(szAddress);

    TCHAR remoteAddress[50];
    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[50];
    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;
}

, s: _) b# d/ H
6 p; o, u3 T+ g3 v  x$ _( E# u, g' V% p! `: W) g

% o2 W( Q5 x% ?. k
游客,如果您要查看本帖隐藏内容请回复

" M# _! ]  K) r+ ^7 z2 [9 F* K
% S" P/ s) z0 o8 b
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|HPsocket-国产高性能通讯框架 ( 豫ICP备19038015号-1 )|网站地图 *//

GMT+8, 2022-12-1 01:09 , Processed in 0.169350 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表