proto-debuger/protoDebuger/network_controller.cpp

159 lines
4.1 KiB
C++
Raw Normal View History

2021-04-24 17:21:31 +00:00
#include "network_controller.h"
2021-05-03 08:49:33 +00:00
NetworkController::NetworkType NetworkController::Type()
{
return mType;
}
NetworkController::NetworkController(NetworkController::NetworkType type,
2021-05-03 05:50:07 +00:00
QString ip,
uint16_t port):mTcp(nullptr),
mUDP(nullptr),
mTcpServer(nullptr),
2021-05-03 15:07:13 +00:00
mCnn(nullptr),
mState(false)
2021-04-24 17:21:31 +00:00
{
2021-05-02 18:02:25 +00:00
mType = type;
if(_checkType(type) == TYPE_UNKOWN){
}
2021-05-03 08:49:33 +00:00
if(type == NetworkType::TYPE_TCP_CLIENT){
2021-04-24 17:21:31 +00:00
mTcp = new QTcpSocket();
mCnn = mTcp;
QObject::connect(mTcp, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
QObject::connect(mTcp, SIGNAL(disconnected()), this, SLOT(on_disconect()));
mTcp->connectToHost(ip,port,QIODevice::ReadWrite);
2021-05-03 15:07:13 +00:00
if(mTcp->state() == QAbstractSocket::ConnectedState)
mState = true;
2021-04-24 17:21:31 +00:00
}
2021-05-02 18:02:25 +00:00
2021-05-03 08:49:33 +00:00
if(type == NetworkType::TYPE_TCP_SERVER){
2021-05-02 18:02:25 +00:00
mTcpServer = new QTcpServer();
connect(mTcpServer,SIGNAL(newConnection()),
this,SLOT(on_server_accept()));
connect(mTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError socketError)),
2021-05-03 05:50:07 +00:00
this, SLOT( on_accept_error(QAbstractSocket::SocketError socketError)));
2021-05-02 18:02:25 +00:00
if (!mTcpServer->listen(QHostAddress::Any, port))
{
qDebug() << "m_pTcpServer->listen() error";
}
}
2021-05-03 08:49:33 +00:00
if((type == NetworkType::TYPE_UDP_SERVER) || (type == NetworkType::TYPE_TCP_CLIENT)){
2021-05-03 05:50:07 +00:00
mUDP = new QUdpSocket(this);
2021-05-03 06:07:04 +00:00
mUDP->bind(QHostAddress::Any, port);
2021-05-03 05:50:07 +00:00
mCnn = mUDP;
connect(mUDP, SIGNAL(readyRead()),
this, SLOT(on_ready_read()));
2021-05-03 06:07:04 +00:00
mUDP->open(QIODevice::ReadWrite);
2021-05-03 15:07:13 +00:00
mState = true;
2021-05-03 05:50:07 +00:00
}
2021-05-02 18:02:25 +00:00
2021-04-24 17:21:31 +00:00
}
2021-04-29 14:26:45 +00:00
int NetworkController::SendData(int8_t *data, uint32_t len)
{
2021-05-01 15:41:40 +00:00
if(nullptr == data)
return -1;
return mCnn->write((const char *)data,len);
2021-04-29 14:26:45 +00:00
}
2021-05-03 08:49:33 +00:00
NetworkController::RecvResult NetworkController::ReadData(int8_t *data)
2021-04-24 17:21:31 +00:00
{
2021-05-03 08:49:33 +00:00
RecvResult ret;
if(nullptr == data){
ret.len = -1;
return ret;
}
if((mType == TYPE_UDP_CLIENT )||(mType == TYPE_UDP_SERVER)){
int len = 0;
while(mUDP->hasPendingDatagrams()){
QNetworkDatagram datagram = mUDP->receiveDatagram();
memcpy(data + len,datagram.data().data(),datagram.data().size());
len += datagram.data().size();
ret.addr = datagram.senderAddress().toString();
ret.port = datagram.senderPort();
qDebug()<<datagram.data().length();
}
}
else{
memcpy(data,mCnn->readAll().data(),mCnn->size());
2021-05-01 15:41:40 +00:00
}
2021-05-03 08:49:33 +00:00
return ret;
2021-05-01 15:41:40 +00:00
}
2021-05-03 05:50:07 +00:00
int NetworkController::Close()
{
if(mType == TYPE_TCP_SERVER){
mTcpServer->close();
if(nullptr != mCnn)
mCnn->close();
delete mTcpServer;
delete mCnn;
}
if(mType == TYPE_TCP_CLIENT){
mCnn->close();
}
2021-05-03 15:07:13 +00:00
mState = false;
2021-05-03 05:50:07 +00:00
return 0;
}
2021-04-24 17:21:31 +00:00
2021-05-01 15:41:40 +00:00
NetworkController::~NetworkController()
{
delete mTcp;
2021-05-03 05:50:07 +00:00
delete mCnn;
delete mTcpServer;
delete mUDP;
2021-04-24 17:21:31 +00:00
}
2021-04-29 14:26:45 +00:00
void NetworkController::on_ready_read()
2021-04-24 17:21:31 +00:00
{
2021-05-03 08:49:33 +00:00
qDebug()<<"data read:";
2021-05-02 18:02:25 +00:00
emit(on_data_recv());
2021-04-24 17:21:31 +00:00
}
2021-04-29 14:26:45 +00:00
void NetworkController::on_disconect()
2021-04-24 17:21:31 +00:00
{
2021-05-01 15:41:40 +00:00
qDebug()<<"close";
2021-05-02 18:02:25 +00:00
if(nullptr != mCnn){
mCnn->close();
2021-05-03 05:50:07 +00:00
emit(this->on_conection_close());
}
if(mType == TYPE_TCP_SERVER){
mTcpServer->resumeAccepting();
2021-05-02 18:02:25 +00:00
}
2021-04-24 17:21:31 +00:00
}
2021-05-02 18:02:25 +00:00
void NetworkController::on_server_accept()
{
if(mType == TYPE_TCP_SERVER){
mTcpServer->pauseAccepting();
QTcpSocket* pClientConnection = mTcpServer->nextPendingConnection();
if(nullptr != pClientConnection){
QObject::connect(pClientConnection, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
QObject::connect(pClientConnection, SIGNAL(disconnected()), this, SLOT(on_disconect()));
}
mTcp = pClientConnection;
2021-05-03 05:50:07 +00:00
mCnn = mTcp;
2021-05-03 15:07:13 +00:00
mState = true;
2021-05-02 18:02:25 +00:00
}
}
2021-05-03 05:50:07 +00:00
void NetworkController::on_accept_error(QAbstractSocket::SocketError socketError)
{
qDebug()<<socketError;
}
2021-05-03 08:49:33 +00:00
NetworkController::NetworkType NetworkController::_checkType(NetworkType type){
2021-05-02 18:02:25 +00:00
if(type < TYPE_UNKOWN){
return type;
}else{
return TYPE_UNKOWN;
}
}