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.
85 lines
1.8 KiB
C++
85 lines
1.8 KiB
C++
#include "lpRawTcpClientSession.h"
|
|
#include <QHostAddress>
|
|
|
|
#include <QDebug>
|
|
#pragma execution_character_set("utf-8")
|
|
lpRawClientSession::lpRawClientSession(int ID, QObject *parent)
|
|
: QObject(parent),
|
|
socket_descriptor_(ID)
|
|
{
|
|
psocket_ = new QTcpSocket(this);
|
|
psocket_->setSocketDescriptor(socket_descriptor_);
|
|
mPeerName = psocket_->peerName();
|
|
mAddress = psocket_->peerAddress().toString();
|
|
mPort = psocket_->peerPort();
|
|
mSocketName = QString("%1-%2").arg(mAddress).arg(mPort);
|
|
|
|
connect(psocket_, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
|
|
connect(psocket_, SIGNAL(disconnected()), this, SLOT(on_disconnect()));
|
|
|
|
}
|
|
|
|
lpRawClientSession::~lpRawClientSession()
|
|
{
|
|
if (psocket_) {
|
|
//psocket_->abort();
|
|
psocket_->close();
|
|
delete psocket_;
|
|
psocket_ = nullptr;
|
|
}
|
|
}
|
|
|
|
void lpRawClientSession::on_ready_read()
|
|
{
|
|
QByteArray data_recv;
|
|
data_recv = psocket_->readAll();
|
|
QSharedPointer<QByteArray> data_ptr = QSharedPointer<QByteArray>
|
|
(new QByteArray(data_recv.constData(), data_recv.size()));
|
|
|
|
if (_func)
|
|
{
|
|
_func(data_recv);
|
|
}
|
|
emit data_received(socket_descriptor_, data_ptr);
|
|
}
|
|
|
|
void lpRawClientSession::force_close()
|
|
{
|
|
psocket_->disconnect();
|
|
//psocket_->disconnectFromHost();
|
|
}
|
|
|
|
void lpRawClientSession::on_disconnect()
|
|
{
|
|
emit sess_disconnected(socket_descriptor_);
|
|
|
|
socket_close();
|
|
}
|
|
|
|
void lpRawClientSession::on_data_to_send(int sess_id, QSharedPointer<QByteArray> data)
|
|
{
|
|
if (sess_id != socket_descriptor_) return;
|
|
psocket_->write(data->constData(), data->size());
|
|
}
|
|
|
|
QString lpRawClientSession::getClientIP()
|
|
{
|
|
return psocket_->peerAddress().toString();
|
|
}
|
|
|
|
int lpRawClientSession::getClientPort()
|
|
{
|
|
return psocket_->peerPort();
|
|
}
|
|
|
|
void lpRawClientSession::socket_close()
|
|
{
|
|
|
|
if (psocket_->isOpen())
|
|
{
|
|
psocket_->abort();
|
|
psocket_->close();
|
|
}
|
|
}
|
|
|