You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.6 KiB
C++
150 lines
3.6 KiB
C++
#include <QFile>
|
|
#include "lpRawTcpClientSession.h"
|
|
#include "lptkRawTcpServer.h"
|
|
#pragma execution_character_set("utf-8")
|
|
lptkRawTcpServer::lptkRawTcpServer(QObject *parent /*= NULL*/)
|
|
: QTcpServer(parent)
|
|
{
|
|
qRegisterMetaType<QSharedPointer<QByteArray>>();
|
|
client_sessions_.clear();
|
|
}
|
|
|
|
lptkRawTcpServer::~lptkRawTcpServer()
|
|
{
|
|
server_end();
|
|
}
|
|
|
|
bool lptkRawTcpServer::server_begin(const QString &strIP, quint16 nPort)
|
|
{
|
|
QHostAddress listen_addr_ = QHostAddress::Any;
|
|
QString ip_addr = strIP;
|
|
quint16 listen_port_ = nPort;
|
|
ip_addr.trimmed();
|
|
if (ip_addr != "*")
|
|
{
|
|
listen_addr_.setAddress(ip_addr);
|
|
}
|
|
if (!this->listen(listen_addr_, nPort))
|
|
{
|
|
qWarning() << "Could not start server. Error: " << this->errorString()
|
|
<< __FUNCTION__;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "Server started. Listening to port " << nPort << __FUNCTION__;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void lptkRawTcpServer::server_end()
|
|
{
|
|
close();
|
|
//close all connected session
|
|
auto it = client_sessions_.begin();
|
|
while (it != client_sessions_.end())
|
|
{
|
|
TP_RAW_CLIENT_SESS cli_sess = it.value();
|
|
disconnect(cli_sess.pSess_);
|
|
cli_sess.pSess_->force_close();
|
|
|
|
cli_sess.pThrd_->quit();
|
|
cli_sess.pThrd_->wait();
|
|
if (cli_sess.pThrd_->isRunning()) {
|
|
cli_sess.pThrd_->exit(0);
|
|
}
|
|
delete cli_sess.pThrd_;// ->deleteLater();
|
|
delete cli_sess.pSess_;
|
|
client_sessions_.erase(it++);
|
|
}
|
|
|
|
client_sessions_.clear();
|
|
|
|
//stop listening
|
|
|
|
}
|
|
|
|
void lptkRawTcpServer::incomingConnection(qintptr socketDescriptor)
|
|
{
|
|
lpRawClientSession *pclientSess = nullptr;
|
|
|
|
pclientSess = new lpRawClientSession(socketDescriptor);
|
|
|
|
QThread *pThrd = new QThread();
|
|
pclientSess->moveToThread(pThrd);
|
|
|
|
TP_RAW_CLIENT_SESS cli_sess;
|
|
cli_sess.pThrd_ = pThrd;
|
|
cli_sess.pSess_ = pclientSess;
|
|
if (_func)
|
|
{
|
|
pclientSess->setRecvCallBackFunc(_func);
|
|
}
|
|
|
|
//connect(pThrd, &QThread::finished, pclientSess, &QObject::deleteLater);
|
|
connect(pclientSess, &lpRawClientSession::data_received, this, &lptkRawTcpServer::on_data_received);
|
|
connect(pclientSess, &lpRawClientSession::sess_disconnected, this, &lptkRawTcpServer::on_sess_disconnected);
|
|
|
|
connect(this, &lptkRawTcpServer::data_to_send, pclientSess, &lpRawClientSession::on_data_to_send);
|
|
client_sessions_[socketDescriptor] = cli_sess;
|
|
|
|
pThrd->start();
|
|
emit client_status_changed(socketDescriptor, TCPSERVER_ON_CLIENT_CONNECTED);
|
|
}
|
|
|
|
void lptkRawTcpServer::on_data_received(int sess_id, QSharedPointer<QByteArray> data_ptr)
|
|
{
|
|
auto it = client_sessions_.find(sess_id);
|
|
if (it == client_sessions_.end())
|
|
{
|
|
qDebug() << "Invalid session ID : " << sess_id << __FUNCTION__;
|
|
return;
|
|
}
|
|
|
|
|
|
emit data_received(sess_id, data_ptr);
|
|
|
|
}
|
|
|
|
void lptkRawTcpServer::on_sess_disconnected(int sess_id)
|
|
{
|
|
auto it = client_sessions_.find(sess_id);
|
|
if (it != client_sessions_.end())
|
|
{
|
|
TP_RAW_CLIENT_SESS cli_sess = it.value();
|
|
cli_sess.pSess_->force_close();
|
|
cli_sess.pThrd_->quit();
|
|
cli_sess.pThrd_->wait();
|
|
if (cli_sess.pThrd_->isRunning()) {
|
|
cli_sess.pThrd_->exit(0);
|
|
}
|
|
delete cli_sess.pThrd_;// ->deleteLater();
|
|
delete cli_sess.pSess_;
|
|
//cli_sess.pThrd_->deleteLater();
|
|
client_sessions_.erase(it);
|
|
}
|
|
|
|
emit client_status_changed(sess_id, TCPSERVER_ON_CLIENT_DISCONNECTED);
|
|
}
|
|
|
|
void lptkRawTcpServer::send_data(int sess_id, QSharedPointer<QByteArray> data_ptr)
|
|
{
|
|
auto it = client_sessions_.find(sess_id);
|
|
if (it != client_sessions_.end())
|
|
{
|
|
TP_RAW_CLIENT_SESS cli_sess = it.value();
|
|
emit data_to_send(it.key(), data_ptr);
|
|
}
|
|
}
|
|
|
|
void lptkRawTcpServer::broadcast_send_data(QSharedPointer<QByteArray> data_ptr)
|
|
{
|
|
auto it = client_sessions_.begin();
|
|
while (it != client_sessions_.end())
|
|
{
|
|
TP_RAW_CLIENT_SESS cli_sess = it.value();
|
|
emit data_to_send(it.key(), data_ptr);
|
|
it++;
|
|
}
|
|
} |