pqtcpsocket.cpp 833 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "pqtcpsocket.h"
  2. namespace PQ {
  3. PQTcpSocket::PQTcpSocket(QObject *parent) : QTcpSocket(parent),_msg(false)
  4. {
  5. connect(&_msg,SIGNAL(revicedMSG(MSGHeader,QByteArray)),this,SIGNAL(revicedMSG(MSGHeader,QByteArray)));
  6. connect(&_msg,SIGNAL(hasError()),this,SLOT(msgError()));
  7. connect(this,SIGNAL(readyRead()),this,SLOT(readSocketData()));
  8. }
  9. PQTcpSocket::~PQTcpSocket()
  10. {}
  11. void PQTcpSocket::msgError()
  12. {
  13. this->setErrorString("message handle error!");
  14. emit error(QAbstractSocket::UnknownSocketError);
  15. this->close();
  16. }
  17. void PQTcpSocket::readSocketData()
  18. {
  19. _msg.readMsg(this->readAll());
  20. }
  21. void PQTcpSocket::writeMessage(MSGHeader header, const QByteArray & byte)
  22. {
  23. header.length = byte.length();
  24. this->write(header.buildArray());
  25. if(header.length > 0)
  26. this->write(byte);
  27. }
  28. }