#include "QtPipeThread.h" #include #include #include #include #include extern QStringList glb_msg_list; QtPipeThread::QtPipeThread(int socketDescriptor, QObject *parent, QObject *mainObject) : QThread(parent) , socketDescriptor(socketDescriptor) , pipeSocket(new QLocalSocket(this)) , break_loop(false) , first_time(true) , m_mainObject(mainObject) { } QtPipeThread::~QtPipeThread() { } void QtPipeThread::run() { bool bret = pipeSocket->setSocketDescriptor(socketDescriptor); if (bret != true) { exit(-1); } pipeSocket->setReadBufferSize(40000); connect(pipeSocket, SIGNAL(stateChanged(QLocalSocket::LocalSocketState)), this, SLOT(OnStateChanged(QLocalSocket::LocalSocketState))); connect(pipeSocket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(OnError(QLocalSocket::LocalSocketError))); connect(pipeSocket, SIGNAL(readyRead()), this, SLOT(readMessage())); connect(pipeSocket, SIGNAL(disconnected()), this, SLOT(disconnected_Message())); connect(m_mainObject, SIGNAL(close_Threads()), this, SLOT(OnCloseFromMain()), Qt::QueuedConnection); connect(this, SIGNAL(Connected(qint32, QString)), m_mainObject, SLOT(OnConnected(qint32, QString)), Qt::QueuedConnection); connect(this, SIGNAL(Disconnected(qint32)), m_mainObject, SLOT(OnDisconnected(qint32)), Qt::QueuedConnection); connect(this, SIGNAL(NewMessage()), m_mainObject, SLOT(OnNewMessage()), Qt::QueuedConnection); exec(); } void QtPipeThread::OnCloseFromMain() { break_loop = true; quit(); } void QtPipeThread::readMessage() { qDebug() << "readMessage"; disconnect(pipeSocket, SIGNAL(readyRead()), this, SLOT(readMessage())); connect(pipeSocket, SIGNAL(readyRead()), this, SLOT(readNextMessage())); int wert = 0; QByteArray inbuffer; while(pipeSocket->bytesAvailable()) { inbuffer = pipeSocket->read(4000); if (first_time == true) { // Its the connect identifier (Name and blalbla) emit Connected(socketDescriptor, QString::fromLocal8Bit(inbuffer)); first_time = false; } else { glb_msg_list.append(QString::fromLocal8Bit(inbuffer)); } inbuffer.clear(); msleep(0); } emit NewMessage(); } void QtPipeThread::readNextMessage() { // qDebug() << "readNextMessage"; int wert = 0; QByteArray inbuffer; while (pipeSocket->bytesAvailable()) { inbuffer = pipeSocket->read(4000); glb_msg_list.append(QString::fromLocal8Bit(inbuffer)); inbuffer.clear(); msleep(0); } emit NewMessage(); } void QtPipeThread::disconnected_Message() { emit Disconnected(socketDescriptor); break_loop = true; quit(); } void QtPipeThread::OnStateChanged(QLocalSocket::LocalSocketState p_state) { //QAbstractSocket::UnconnectedState 0 The socket is not connected. //QAbstractSocket::HostLookupState 1 The socket is performing a host name lookup. //QAbstractSocket::ConnectingState 2 The socket has started establishing a connection. //QAbstractSocket::ConnectedState 3 A connection is established. //QAbstractSocket::BoundState 4 The socket is bound to an address and port. //QAbstractSocket::ListeningState 5 For internal use only. //QAbstractSocket::ClosingState 6 The socket is about to close(data may still be waiting to be written). } void QtPipeThread::OnError(QLocalSocket::LocalSocketError p_e) { //QAbstractSocket::ConnectionRefusedError 0 The connection was refused by the peer(or timed out). //QAbstractSocket::RemoteHostClosedError 1 The remote host closed the connection.Note that the client socket(i.e., this socket) will be closed after the remote close notification has been sent. //QAbstractSocket::HostNotFoundError 2 The host address was not found. //QAbstractSocket::SocketAccessError 3 The socket operation failed because the application lacked the required privileges. //QAbstractSocket::SocketResourceError 4 The local system ran out of resources(e.g., too many sockets). //QAbstractSocket::SocketTimeoutError 5 The socket operation timed out. //QAbstractSocket::DatagramTooLargeError 6 The datagram was larger than the operating system's limit (which can be as low as 8192 bytes). //QAbstractSocket::NetworkError 7 An error occurred with the network(e.g., the network cable was accidentally plugged out). //QAbstractSocket::AddressInUseError 8 The address specified to QAbstractSocket::bind() is already in use and was set to be exclusive. //QAbstractSocket::SocketAddressNotAvailableError 9 The address specified to QAbstractSocket::bind() does not belong to the host. //QAbstractSocket::UnsupportedSocketOperationError 10 The requested socket operation is not supported by the local operating system(e.g., lack of IPv6 support). //QAbstractSocket::ProxyAuthenticationRequiredError 12 The socket is using a proxy, and the proxy requires authentication. //QAbstractSocket::SslHandshakeFailedError 13 The SSL / TLS handshake failed, so the connection was closed(only used in QSslSocket) //QAbstractSocket::UnfinishedSocketOperationError 11 Used by QAbstractSocketEngine only, The last operation attempted has not finished yet(still in progress in the background). //QAbstractSocket::ProxyConnectionRefusedError 14 Could not contact the proxy server because the connection to that server was denied //QAbstractSocket::ProxyConnectionClosedError 15 The connection to the proxy server was closed unexpectedly(before the connection to the final peer was established) //QAbstractSocket::ProxyConnectionTimeoutError 16 The connection to the proxy server timed out or the proxy server stopped responding in the authentication phase. //QAbstractSocket::ProxyNotFoundError 17 The proxy address set with setProxy() (or the application proxy) was not found. //QAbstractSocket::ProxyProtocolError 18 The connection negotiation with the proxy server failed, because the response from the proxy server could not be understood. //QAbstractSocket::OperationError 19 An operation was attempted while the socket was in a state that did not permit it. //QAbstractSocket::SslInternalError 20 The SSL library being used reported an internal error.This is probably the result of a bad installation or misconfiguration of the library. //QAbstractSocket::SslInvalidUserDataError 21 Invalid data(certificate, key, cypher, etc.) was provided and its use resulted in an error in the SSL library. //QAbstractSocket::TemporaryError 22 A temporary error occurred(e.g., operation would block and socket is non - blocking). //QAbstractSocket::UnknownSocketError - 1 An unidentified error occurred. }