* B+ i" m. Z6 @$ ~, Z: g' Q! Q( W8 XQT使用HPSocket,QTHPSocketDemo( ~) W! F" \" l' w+ k7 f
3 V5 ~! T- P. c0 l, @! V( l[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;
}
. x7 z4 f$ E# F/ L u* m7 \" l
1 @: A' u0 j% n" h* E4 h( A$ y2 z
9 z) i" w8 @, M
, X2 e; }- h# }) h6 p; b: ?1 E4 ~, m+ X. U1 ]+ W2 C( a) }
! z* R, }, J0 e7 b7 Z
|