#include "TServer.h"
//------------------------------------------------------------------------------------------------------------------------
CClient::CClient(CServer * parent, int handle)
{
m_parent = parent;
m_Handle = handle;
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::run()
{
// ----
if(m_Socket->setSocketDescriptor(m_Handle) == false)
{
m_parent->onError(__FUNCTION__, m_Socket->errorString());
}
else
{
connect(m_Socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readAble()));
connect(m_Socket, SIGNAL(destroyed()), this, SLOT(dead()));
}
// ----
m_parent->onConnect(m_Handle);
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::dead()
{
printf("dead \n"); // never called
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::disconnected()
{
m_parent->onDisconnect(m_Handle);
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::readAble()
{
m_parent->onRecv(m_Handle, m_Socket->readAll());
}
//------------------------------------------------------------------------------------------------------------------------
{
m_PoolThreadsCount = 4;
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::run(ushort port)
{
bool bReturn = false;
// ----
static QString functionName
= __FUNCTION__;
// ----
{
onInfo
(functionName,
QString("Server is listening on port %0").
arg(port
));
// ----
bReturn = true;
}
else
{
onInfo
(functionName,
QString("The server can not run. port %0, %1").
arg(port
).
arg(errorString
()));
}
// ----
m_ThreadPoolMgr = new QThreadPool(this);
m_ThreadPoolMgr->setMaxThreadCount(m_PoolThreadsCount);
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onConnect(int handle)
{
onInfo
(__FUNCTION__,
QString("new connection from handle %0").
arg(handle
));
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onDisconnect(int handle)
{
onInfo
(__FUNCTION__,
QString("disconnection from handle %0").
arg(handle
));
}
//------------------------------------------------------------------------------------------------------------------------
void CServer
::onRecv(int handle,
QByteArray packet
) {
onInfo
(__FUNCTION__,
QString("recv from handle %0").
arg(handle
));
}
//------------------------------------------------------------------------------------------------------------------------
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::incomingConnection(int handle)
{
CClient * newClient = new CClient(this, handle);
// ----
newClient->run();
}
//------------------------------------------------------------------------------------------------------------------------
#include "TServer.h"
//------------------------------------------------------------------------------------------------------------------------
CClient::CClient(CServer * parent, int handle)
{
m_parent = parent;
m_Handle = handle;
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::run()
{
m_Socket = new QTcpSocket();
// ----
if(m_Socket->setSocketDescriptor(m_Handle) == false)
{
m_parent->onError(__FUNCTION__, m_Socket->errorString());
}
else
{
connect(m_Socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readAble()));
connect(m_Socket, SIGNAL(destroyed()), this, SLOT(dead()));
}
// ----
m_parent->onConnect(m_Handle);
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::dead()
{
printf("dead \n"); // never called
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::disconnected()
{
m_parent->onDisconnect(m_Handle);
}
//------------------------------------------------------------------------------------------------------------------------
void CClient::readAble()
{
m_parent->onRecv(m_Handle, m_Socket->readAll());
}
//------------------------------------------------------------------------------------------------------------------------
CServer::CServer(QObject * parent) : QTcpServer(parent)
{
m_PoolThreadsCount = 4;
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::run(ushort port)
{
bool bReturn = false;
// ----
static QString functionName = __FUNCTION__;
// ----
if(listen(QHostAddress::Any, port) == true)
{
onInfo(functionName, QString("Server is listening on port %0").arg(port));
// ----
bReturn = true;
}
else
{
onInfo(functionName, QString("The server can not run. port %0, %1").arg(port).arg(errorString()));
}
// ----
m_ThreadPoolMgr = new QThreadPool(this);
m_ThreadPoolMgr->setMaxThreadCount(m_PoolThreadsCount);
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onConnect(int handle)
{
onInfo(__FUNCTION__, QString("new connection from handle %0").arg(handle));
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onDisconnect(int handle)
{
onInfo(__FUNCTION__, QString("disconnection from handle %0").arg(handle));
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onRecv(int handle, QByteArray packet)
{
onInfo(__FUNCTION__, QString("recv from handle %0").arg(handle));
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onError(QString function, QString message)
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onInfo(QString function, QString message)
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::onWrong(QString function, QString message)
{
printf("[%s][%s]\n", function.toStdString().c_str(), message.toStdString().c_str());
}
//------------------------------------------------------------------------------------------------------------------------
void CServer::incomingConnection(int handle)
{
CClient * newClient = new CClient(this, handle);
// ----
newClient->run();
}
//------------------------------------------------------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode
//------------------------------------------------------------------------------------------------------------------------
#ifndef TSERVER_H
#define TSERVER_H
//------------------------------------------------------------------------------------------------------------------------
#include <QString>
#include <QDebug>
#include <QTCPServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QThreadPool>
#include <QRunnable>
//------------------------------------------------------------------------------------------------------------------------
#include <list>
//------------------------------------------------------------------------------------------------------------------------
class CClient;
class CServer;
//------------------------------------------------------------------------------------------------------------------------
#define CLIENT_LIST std::list<CClient *>
//------------------------------------------------------------------------------------------------------------------------
{
Q_OBJECT
private:
CServer * m_parent;
int m_Handle;
public:
explicit CClient (CServer * parent, int handle);
// ----
void run ();
private slots:
void disconnected ();
void readAble ();
void dead();
};
//------------------------------------------------------------------------------------------------------------------------
{
Q_OBJECT
private:
ushort m_PoolThreadsCount;
QThreadPool * m_ThreadPoolMgr;
public:
explicit CServer
(QObject * parent
= 0);
// ----
void run (ushort port = 1234);
// ----
virtual void onConnect (int handle);
virtual void onDisconnect (int handle);
virtual void onRecv
(int handle,
QByteArray packet
);
// ----
// ----
void inline setPoolThreadsCount (ushort Count) { m_PoolThreadsCount = Count; };
ushort inline getPoolThreadsCount () { return m_PoolThreadsCount; };
protected:
void incomingConnection (int handle);
};
//------------------------------------------------------------------------------------------------------------------------
#endif // TSERVER_H
//------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------
#ifndef TSERVER_H
#define TSERVER_H
//------------------------------------------------------------------------------------------------------------------------
#include <QString>
#include <QDebug>
#include <QTCPServer>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QThreadPool>
#include <QRunnable>
//------------------------------------------------------------------------------------------------------------------------
#include <list>
//------------------------------------------------------------------------------------------------------------------------
class CClient;
class CServer;
//------------------------------------------------------------------------------------------------------------------------
#define CLIENT_LIST std::list<CClient *>
//------------------------------------------------------------------------------------------------------------------------
class CClient : public QObject
{
Q_OBJECT
private:
CServer * m_parent;
QTcpSocket * m_Socket;
int m_Handle;
public:
explicit CClient (CServer * parent, int handle);
// ----
void run ();
private slots:
void disconnected ();
void readAble ();
void dead();
};
//------------------------------------------------------------------------------------------------------------------------
class CServer : public QTcpServer
{
Q_OBJECT
private:
ushort m_PoolThreadsCount;
QThreadPool * m_ThreadPoolMgr;
public:
explicit CServer (QObject * parent = 0);
// ----
void run (ushort port = 1234);
// ----
virtual void onConnect (int handle);
virtual void onDisconnect (int handle);
virtual void onRecv (int handle, QByteArray packet);
// ----
virtual void onInfo (QString function, QString message);
virtual void onError (QString function, QString message);
virtual void onWrong (QString function, QString message);
// ----
void inline setPoolThreadsCount (ushort Count) { m_PoolThreadsCount = Count; };
ushort inline getPoolThreadsCount () { return m_PoolThreadsCount; };
protected:
void incomingConnection (int handle);
};
//------------------------------------------------------------------------------------------------------------------------
#endif // TSERVER_H
//------------------------------------------------------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode
Bookmarks