lanpaper/Server.cpp

74 lines
2.1 KiB
C++
Raw Permalink Normal View History

#include "Server.h"
Server::Server(QObject *parent) :
QTcpServer(parent)
{
connect(this,SIGNAL(newConnection()), this,SLOT(newConnection()) );
}
void Server::newConnection()
{
this->tcpSocket=this->nextPendingConnection();
if(this->tcpSocket!=NULL)
{
this->tcpSocket=tcpSocket;
connect(this->tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead()) );
}
else
{
QMessageBox::aboutQt(0);
}
}
void Server::readyRead()
{
QByteArray data=this->tcpSocket->readAll();
if(this->receivedData.size()==0)//first section of data
{
this->timer=this->startTimer(1000*10);
}
this->receivedData.push_back(data);
// char a1=data.at(data.size()-1);
// char a2=data.at(data.size()-2);
// char a3=data.at(data.size()-3);
// char a4=data.at(data.size()-4);
// char a5=data.at(data.size()-5);
if(this->tcpSocket->atEnd())
//if(data.at(data.size()-1)=='\n'&&data.at(data.size()-2)=='\n'&&data.at(data.size()-3)=='\n'&&data.at(data.size()-4)=='\n'&&data.at(data.size()-5)=='\n')
{
//this->receivedData.remove(this->receivedData.size()-5,5);
// QByteArray f=this->receivedData;
LanPaper *mainWin=(LanPaper *)(this->parent());
if(NULL!=mainWin)
{
//tcpSocket->waitForReadyRead(5*1000);
//qDebug()<<"read buff"<<this->tcpSocket->readBufferSize()<<"\n";
//this->tcpSocket->setReadBufferSize(1024*100);
//QByteArray data=this->tcpSocket->readAll();
//qDebug()<<data.size()<<"\n";
Secret secret;
QString decodedText=secret.getDecodeString(this->receivedData);
mainWin->addChatContentToThereSide(decodedText);
this->resetConnection();
this->killTimer(this->timer);
}
}
}
void Server::timerEvent(QTimerEvent *e)
{
this->killTimer(this->timer);
this->resetConnection();
//qDebug()<<"recei failed\n";
}
void Server::resetConnection()
{
this->receivedData.clear();
this->tcpSocket->close();
this->tcpSocket->deleteLater();
}