#ifndef CMYSOCKET_H#define CMYSOCKET_H#include <QThread>#include <QTcpSocket>#include <QTimer>class CMySocket : public QThread{ Q_OBJECT QTcpSocket * fsocket; QTimer * frequestTimer; QString fhost; quint16 fport; size_t freceived; size_t fguardSize; QByteArray fcurrentData;protected: void run();public: explicit CMySocket(QObject *parent = 0); ~CMySocket(); inline void setHost(const QString & value) { fhost = value; } inline void setPort(quint16 value) { fport = value; } inline const QString & host() const { return fhost; } inline quint16 port() const { return fport; }signals: void dataReceived(QString jsonString); void guardSizeExceed(); public slots:private slots: void socket_connect(); void socket_disconnect(); void socket_error(QAbstractSocket::SocketError); void socket_read(); void socket_cleanup(); void socket_send_request();};#endif // CMYSOCKET_H
#include "cmysocket.h"CMySocket::CMySocket(QObject *parent) : QThread(parent){ fsocket = NULL; frequestTimer = NULL; fhost = "127.0.0.1"; fport = 50001; fguardSize = 10485760L; freceived = 0L; connect(this,SIGNAL(finished()),this,SLOT(socket_cleanup()));}CMySocket::~CMySocket(){ socket_cleanup();}void CMySocket::socket_connect(){ qDebug("connected %s:%d",qPrintable(fhost),fport); //frequestTimer->start(500);}void CMySocket::socket_disconnect(){ //frequestTimer->stop(); qDebug("disconnected %s:%d",qPrintable(fhost),fport);}void CMySocket::socket_error(QAbstractSocket::SocketError){ qDebug("error %s:%d %s",qPrintable(fhost),fport,qPrintable(fsocket->errorString()));}void CMySocket::socket_read(){ int bytesAvail = fsocket->bytesAvailable(); if(bytesAvail<=0) return; QByteArray packetData = fsocket->readAll(); //считываем текущую часть принятых данных qDebug("read %d bytes",packetData.size()); /* if(!packetData.isEmpty()) //если что то есть(а полюбому что то должно быть) { int terminatorIndex; //индекс терминатора (нулевого символа) int prevTerminatorIndex = 0; //предыдущий терминатор int dataSize = packetData.size(); //размер полученных данных terminatorIndex = packetData.indexOf('\0',prevTerminatorIndex); //ищем первый терминатор в пакете if(terminatorIndex<0) //если нет терминатора, копируем целиком { fcurrentData.append(packetData); //добавляем полученные данные во временную строку freceived+=dataSize; //инкрементируем счетчик полученных данных if(freceived>=fguardSize) //если полученных данных больше чем охранный размер { freceived = 0L; //обнуляем счетчик принятых данных qDebug("freceived %lu",freceived); emit guardSizeExceed(); //сигналим об ошибке } return; } int last; while(terminatorIndex>=0 && prevTerminatorIndex<dataSize) { last = terminatorIndex-prevTerminatorIndex; if(last>0) { fcurrentData.append(packetData.mid(prevTerminatorIndex,last)); //копируем часть до терминатора emit dataReceived(QString(fcurrentData)); //сигналим об очередной строке с json } freceived = 0L; qDebug("freceived %lu",freceived); fcurrentData.clear(); //стираем временный буфер prevTerminatorIndex = terminatorIndex+1; //сдвигаем указатель, с которого будем искать следующий терминатор terminatorIndex = packetData.indexOf('\0',prevTerminatorIndex); } if(prevTerminatorIndex<dataSize) //если осталась часть принятых данных со следующей json строки { int lastPart = dataSize-prevTerminatorIndex; fcurrentData.append(packetData.mid(prevTerminatorIndex,(lastPart)));//копируем остаток freceived+=lastPart; qDebug("freceived %lu",freceived); } } */}void CMySocket::socket_cleanup(){ if(frequestTimer) { frequestTimer->stop(); frequestTimer->deleteLater(); frequestTimer = NULL; } if(fsocket) { fsocket->disconnectFromHost(); fsocket->deleteLater(); fsocket = NULL; }}void CMySocket::socket_send_request(){}void CMySocket::run(){ if(fhost.isEmpty() || !fport) exit(-1); //frequestTimer = new QTimer; //connect(frequestTimer,SIGNAL(timeout()),this,SLOT(socket_send_request())); fsocket = new QTcpSocket; connect(fsocket,SIGNAL(connected()),this,SLOT(socket_connect())); connect(fsocket,SIGNAL(disconnected()),this,SLOT(socket_disconnect())); connect(fsocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_error(QAbstractSocket::SocketError))); connect(fsocket,SIGNAL(readyRead()),this,SLOT(socket_read())); fsocket->connectToHost(fhost,fport); exec();}
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); QMenu * menu = new QMenu(trUtf8("Связь")); QAction * action = menu->addAction(trUtf8("Подключиться")); connect(action,SIGNAL(triggered()),this,SLOT(connect_sockets())); action = menu->addAction(trUtf8("Отключиться")); connect(action,SIGNAL(triggered()),this,SLOT(disconnect_sockets())); menuBar()->addMenu(menu); fsocket = new CMySocket(this);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::connect_sockets(){ fsocket->setHost("192.168.1.101"); fsocket->setPort(50002); connect(fsocket,SIGNAL(dataReceived(QString)),this,SLOT(process_data(QString))); if(!fsocket->isRunning()) { fsocket->start(); }}void MainWindow::disconnect_sockets(){ if(fsocket->isRunning()) { fsocket->quit(); fsocket->wait(); }}void MainWindow::process_data(const QString &value){ //qDebug("data %s",qPrintable(value));}
CMySocket::CMySocket(QObject *parent) : QThread(parent){ fsocket = NULL; frequestTimer = NULL; fhost = "127.0.0.1"; fport = 50001; fguardSize = 10485760L; freceived = 0L; connect(this,SIGNAL(finished()),this,SLOT(socket_cleanup())); qDebug("CMySocket lives in %p, currentThread %p",this->thread(),currentThread());}CMySocket::~CMySocket(){ socket_cleanup();}void CMySocket::socket_connect(){ qDebug("connected %s:%d",qPrintable(fhost),fport); frequestTimer->start(500);}void CMySocket::socket_disconnect(){ frequestTimer->stop(); qDebug("disconnected %s:%d",qPrintable(fhost),fport);}void CMySocket::socket_error(QAbstractSocket::SocketError){ qDebug("error %s:%d %s",qPrintable(fhost),fport,qPrintable(fsocket->errorString()));}void CMySocket::socket_read(){ qDebug("CMySocket::socket_read() %p",currentThread()); int bytesAvail = fsocket->bytesAvailable(); if(bytesAvail<=0) return; QByteArray packetData = fsocket->readAll(); //считываем текущую часть принятых данных qDebug("read %d bytes",packetData.size());}void CMySocket::socket_cleanup(){ if(frequestTimer) { frequestTimer->stop(); frequestTimer->deleteLater(); frequestTimer = NULL; } if(fsocket) { fsocket->disconnectFromHost(); fsocket->deleteLater(); fsocket = NULL; }}void CMySocket::socket_send_request(){}void CMySocket::run(){ if(fhost.isEmpty() || !fport) exit(-1); frequestTimer = new QTimer; connect(frequestTimer,SIGNAL(timeout()),this,SLOT(socket_send_request())); fsocket = new QTcpSocket; connect(fsocket,SIGNAL(connected()),this,SLOT(socket_connect())); connect(fsocket,SIGNAL(disconnected()),this,SLOT(socket_disconnect())); connect(fsocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socket_error(QAbstractSocket::SocketError))); connect(fsocket,SIGNAL(readyRead()),this,SLOT(socket_read())); fsocket->connectToHost(fhost,fport); qDebug("CMySocket::run() current thread %p, socket in %p",this->currentThread(),fsocket->thread()); exec();}
moveToThread(this);
#include <QThread>#include "qdspclient.h"class QDspClientThread : public QThread{ Q_OBJECTpublic: explicit QDspClientThread(QObject *parent = 0); ~QDspClientThread(); QDspClient * dspClient;};
QDspClientThread::QDspClientThread(QObject *parent) : QThread(parent){ dspClient = new QDspClient; connect(this,SIGNAL(started()),dspClient,SLOT(connectToServer())); connect(this,SIGNAL(finished()),dspClient,SLOT(disconnectFromServer()));}QDspClientThread::~QDspClientThread(){ dspClient->deleteLater();}
void CDspHandler::startClient(){ foreach(QDspClientThread * clientThread, fdspConnections) { if(!clientThread->isRunning()) { if(clientThread->dspClient->connectionConfig().interfaceIndex()>=0) { clientThread->start(); clientThread->dspClient->moveToThread(clientThread->currentThread()); } } } emit sourceChanged(NULL);}void CDspHandler::stopClient(){ foreach (QDspClientThread * clientThread, fdspConnections) { if(clientThread->isRunning()) { qDebug("quiting thread for %s:%d",qPrintable(clientThread->dspClient->connectionConfig().host()),clientThread->dspClient->connectionConfig().port()); clientThread->quit(); bool result = clientThread->wait(5000); qDebug(result?"Ok":"Failed"); clientThread->dspClient->moveToThread(thread()); } }}
void QRawClient::connectToServer(){ fmustReconnect = true; if(fsocket.state() != QAbstractSocket::UnconnectedState) return; int fd = fsocket.socketDescriptor(); if(fd<0) { fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); } if(!fbindAddress.isNull()) //если биндинг задан, создаем сокет посредством Qt {// qDebug("Try bind %i:%s to %s",connectionConfig().interfaceIndex(),qPrintable(connectionConfig().host()),qPrintable(fbindAddress.toString())); struct sockaddr_in bindAddr; memset(&bindAddr,0,sizeof(bindAddr)); bindAddr.sin_family = PF_INET; bindAddr.sin_addr.s_addr = htonl(fbindAddress.toIPv4Address()); if(bind(fd,(const struct sockaddr *)&bindAddr,sizeof(bindAddr))!=0) { qDebug("Socket bind to %s failed with reason %s",qPrintable(fbindAddress.toString()),strerror(errno)); shutdown(fd,2);#ifdef Q_WS_WIN32 closesocket(fd);#else close(fd);#endif onDisconnected(); return; } } struct sockaddr_in connect_addr; memset(&connect_addr,0,sizeof(connect_addr)); connect_addr.sin_family = PF_INET; connect_addr.sin_port = htons(connectionConfig().port()); QHostAddress address(connectionConfig().host()); connect_addr.sin_addr.s_addr = htonl(address.toIPv4Address()); if(::connect(fd,(const struct sockaddr *)&connect_addr,sizeof(connect_addr))==-1) //<<<<<ЗДЕСЬ ЗАВИСАЕТ { qDebug("connect error! %s",strerror(errno)); close(fd); onDisconnected(); return; } else { if(!fsocket.setSocketDescriptor(fd,QAbstractSocket::ConnectedState)) qDebug("binding to native socket failed"); onConnected(); }}