#include "ServerDemo.h"
#include <QHostAddress>
#include <QTcpSocket>
#include <QObjectList>
#include <QDebug>
#include "dataInfo.h"
#pragma execution_character_set("utf-8")
ServerDemo::ServerDemo(QObject* parent) : QObject(parent)
{
connect(&m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
my_sqlDataBase.Creat_MPUDb();
my_sqlDataBase.Create_MPUTable();
my_sqlDataBase.db_queryAllMPUItem();
if(GlobalMPUInfo.size()>0)
{
qDebug()<<"读取数据库成功";
}
my_sqlDataBase.closeMPUDb();
}
void ServerDemo::onNewConnection()
{
qDebug() << "onNewConnection";
QTcpSocket* tcp = m_server.nextPendingConnection();
connect(tcp, SIGNAL(connected()), this, SLOT(onConnected()));
connect(tcp, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
connect(tcp, SIGNAL(readyRead()), this, SLOT(onDataReady()));
connect(tcp, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
}
void ServerDemo::onConnected()
{
QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());
if( tcp != NULL )
{
qDebug() << "onConnected";
qDebug() << "Local Address:" << tcp->localAddress();
qDebug() << "Local Port:" << tcp->localPort();
}
}
void ServerDemo::onDisconnected()
{
unsigned int tcpClientSize = windowsDeviceList.size();
qDebug()<<"当前连接数:"<<tcpClientSize;
//遍历检查容器中保存的客户端通信套接字是否已经断开连接,如果是则删除
for (unsigned int i = 0; i < tcpClientSize; i++)
{
if (windowsDeviceList.at(i)->state() == QAbstractSocket::UnconnectedState) // state是套接字状态,用来标识连接状态
{
windowsDeviceList.removeAt(i);
--i; //保证删除掉一个元素后依旧能遍历全部元素
--tcpClientSize; //删除掉一个后元素量也少了一个
}
}
qDebug()<<"清理后的连接数:"<<tcpClientSize;
qDebug() << "onDisconnected";
}
void ServerDemo::onDataReady()
{
QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());
char buf[256] = {0};
int flag = 0;
if( tcp != NULL )
{
qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf)-1);
qDebug() << "Data:" << buf;
QJsonParseError json_err;
QJsonDocument parse_doucment=QJsonDocument::fromJson(buf,&json_err);
if(json_err.error==QJsonParseError::NoError)
{
if(parse_doucment.isObject())
{
QJsonObject obj=parse_doucment.object();
if(obj.contains("device"))
{
QJsonValue dev_value=obj.take("device");
if(dev_value.isString())
{
QString dev = dev_value.toString();
if(dev == "windows")
{
qDebug()<<"new windows connect";
unsigned int tcpClientSize = windowsDeviceList.size();
for(int i = 0;i<tcpClientSize;i++)
{
if(windowsDeviceList.at(i)->state()==QAbstractSocket::ConnectedState)
{
if(tcp == windowsDeviceList.at(i))
{
flag = 1;
break;
}
}
else if(windowsDeviceList.at(i)->state() == QAbstractSocket::UnconnectedState)
{
windowsDeviceList.removeAt(i);
--i; //保证删除掉一个元素后依旧能遍历全部元素
--tcpClientSize; //删除掉一个后元素量也少了一个
}
}
if(flag != 1)
{
windowsDeviceList.append(tcp);
}
}
}
}
}
}
else
{
unsigned int tcpClientSize = windowsDeviceList.size();
for(int i = 0;i<tcpClientSize;i++)
{
if(windowsDeviceList.at(i)->state()==QAbstractSocket::ConnectedState)
{
windowsDeviceList.at(i)->write(buf);
}
else if(windowsDeviceList.at(i)->state() == QAbstractSocket::UnconnectedState)
{
windowsDeviceList.removeAt(i);
--i; //保证删除掉一个元素后依旧能遍历全部元素
--tcpClientSize; //删除掉一个后元素量也少了一个
}
}
}
}
}
void ServerDemo::onBytesWritten(qint64 bytes)
{
qDebug() << "onBytesWritten:" << bytes;
}
bool ServerDemo::start(int port)
{
bool ret = true;
if( !m_server.isListening() )
{
ret = m_server.listen(QHostAddress::Any, port);
}
if(ret == false)
{
qDebug()<<"监听端口失败";
}
return ret;
}
bool ServerDemo::start(QString IP,int port)
{
bool ret = true;
if( !m_server.isListening() )
{
ret = m_server.listen(QHostAddress(IP), port);
if(ret == false)
{
qDebug()<<"监听端口失败";
}
}
return ret;
}
void ServerDemo::stop()
{
if( m_server.isListening() )
{
m_server.close();
}
}
ServerDemo::~ServerDemo()
{
const QObjectList& list = m_server.children();
for(int i=0; i<list.length(); i++)
{
QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(list[i]);
if( tcp != NULL )
{
tcp->close();
}
}
}
/*
if(m_client.state()==QAbstractSocket::ConnectedState){
return 0;
}
*/
评论0