void Client::processMessage(QString frame) { QtWebsocket::QWsSocket* socket = dynamic_cast<QtWebsocket::QWsSocket*>(sender()); if(socket && !_needDelete){ emit frameReceived(socket, frame); } }
/** * @brief SocketThread::run */ void SocketThread::run() { std::cout << tr("connect done in thread : 0x%1").arg(QString::number((intptr_t)QThread::currentThreadId(), 16)).toStdString() << std::endl; if( !(conn = mysql_init((MYSQL*)NULL))){ printf("init fail\n"); exit(1); } printf("mysql_init success.\n"); if(!mysql_real_connect(conn, server, user, password, NULL, 3306, NULL, 0)){ printf("connect error.\n"); exit(1); } printf("mysql_real_connect success.\n"); if(mysql_select_db(conn, database) != 0){ mysql_close(conn); printf("select_db fail.\n"); exit(1); } // Connecting the socket signals here to exec the slots in the new thread QObject::connect(socket, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); QObject::connect(socket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); QObject::connect(this, SIGNAL(finished()), this, SLOT(finished()), Qt::DirectConnection); // Launch the event loop to exec the slots exec(); }
void Client::addSocket(QtWebsocket::QWsSocket *socket) { sockets << socket; connect(socket, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); connect(socket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); }
void WebSocketServer::processNewConnection() { QtWebsocket::QWsSocket* clientSocket = server->nextPendingConnection(); QObject::connect(clientSocket, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); QObject::connect(clientSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); QObject::connect(clientSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); clients << clientSocket; std::cout << tr("Client connected").toStdString() << std::endl; }
void XBeeNodeImpl::handleFrame(const XBeeFrame& frame) { APIFrame apiFrame; apiFrame.type = frame.type(); apiFrame.data.assign( reinterpret_cast<const Poco::UInt8*>(frame.data()), reinterpret_cast<const Poco::UInt8*>(frame.data()) + frame.dataSize()); try { frameReceived(apiFrame); } catch (Poco::Exception& exc) { _logger.log(exc); } switch (frame.type()) { case XBeeFrame::XBEE_FRAME_TRANSMIT_STATUS: handleTransmitStatusReceived(frame); break; case XBeeFrame::XBEE_FRAME_RECEIVE_PACKET_64BIT_ADDRESS: case XBeeFrame::XBEE_FRAME_RECEIVE_PACKET_16BIT_ADDRESS: handlePacketReceived(frame); break; case XBeeFrame::XBEE_FRAME_RECEIVE_PACKET_64BIT_ADDRESS_IO: case XBeeFrame::XBEE_FRAME_RECEIVE_PACKET_16BIT_ADDRESS_IO: handleIODataReceived(frame); break; case XBeeFrame::XBEE_FRAME_ZIGBEE_TRANSMIT_STATUS: handleZigBeeTransmitStatusReceived(frame); break; case XBeeFrame::XBEE_FRAME_ZIGBEE_RECEIVE_PACKET: handleZigBeePacketReceived(frame); break; case XBeeFrame::XBEE_FRAME_ZIGBEE_EXPLICIT_RX_INDICATOR: handleExplicitAddressingZigBeePacketReceived(frame); break; case XBeeFrame::XBEE_FRAME_MODEM_STATUS: handleModemStatus(frame); break; case XBeeFrame::XBEE_FRAME_AT_COMMAND_RESPONSE: handleCommandResponse(frame); break; case XBeeFrame::XBEE_FRAME_REMOTE_AT_COMMAND_RESPONSE: handleRemoteCommandResponse(frame); break; case XBeeFrame::XBEE_FRAME_ZIGBEE_IO_DATA_SAMPLE_RX_INDICATOR: handleSampleRxIndicator(frame); break; default: break; } }
void Server::processNewConnection() { client = server->nextPendingConnection(); QObject::connect(client, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); QObject::connect(client, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); std::cout << tr("Client connected").toStdString() << std::endl; connected = true; emit state(true); }
void ServerThreaded::processNewConnection() { Log::display("Client connected"); // Get the connecting socket QWsSocket * socket = server->nextPendingConnection(); // Create a new thread and giving to him the socket SocketThread * thread = new SocketThread( socket ); // connect for message broadcast connect( socket, SIGNAL(frameReceived(QString)), this, SIGNAL(broadcastMessage(QString)) ); connect( this, SIGNAL(broadcastMessage(QString)), thread, SLOT(sendMessage(QString)) ); // connect for message display in log connect( socket, SIGNAL(frameReceived(QString)), this, SLOT(displayMessage(QString)) ); // Starting the thread thread->start(); }
void Server::processNewConnection() { QtWebsocket::QWsSocket* clientSocket = server->nextPendingConnection(); QObject::connect(clientSocket, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); QObject::connect(clientSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); QObject::connect(clientSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); clients << clientSocket; qDebug() << tr("Client connected (%1)").arg(clientSocket->isEncrypted() ? "encrypted" : "not encrypted"); }
void WSChat::initiateWebSocket() { //ip = "ws://localhost"; ip = "ws://tunnel.vince.im"; port = 8999; websocket = new QtWebsocket::QWsSocket(this, NULL, QtWebsocket::WS_V13); websocket->connectToHost(ip.toUtf8(), port); QObject::connect(websocket, SIGNAL(frameReceived(QString)), this, SLOT(checkMessage(QString))); //set the socket state of dlg and main window socketState = currentSocketState(websocket->state()); //QMessageBox::warning(this, tr(""), socketState, QMessageBox::Yes); ui->infoLabel->setText(socketState); }
void SocketThread::run() { std::cout << tr("connect done in thread : 0x%1") .arg(QString::number((unsigned int)QThread::currentThreadId(), 16)) .toStdString() << std::endl; // Connecting the socket signals here to exec the slots in the new thread QObject::connect(socket, SIGNAL(frameReceived(QString)), this, SLOT(processMessage(QString))); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); QObject::connect(socket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64))); QObject::connect(this, SIGNAL(finished()), this, SLOT(finished()), Qt::DirectConnection); // Launch the event loop to exec the slots exec(); }
Client::Client(QWidget *parent) : QWidget(parent), ui(new Ui::Client) { ui->setupUi(this); defaultPseudo = QString("user%1").arg(qrand() % 9000 + 1000); ui->pseudoLineEdit->setPlaceholderText(defaultPseudo); wsSocket = new QtWebsocket::QWsSocket(this, NULL, QtWebsocket::WS_V13); socketStateChanged(wsSocket->state()); QObject::connect(ui->sendButton, SIGNAL(pressed()), this, SLOT(sendMessage())); QObject::connect(ui->textLineEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage())); QObject::connect(ui->connectButton, SIGNAL(pressed()), this, SLOT(connectSocket())); QObject::connect(ui->disconnectButton, SIGNAL(pressed()), this, SLOT(disconnectSocket())); QObject::connect(wsSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); QObject::connect(wsSocket, SIGNAL(frameReceived(QString)), this, SLOT(displayMessage(QString))); QObject::connect(wsSocket, SIGNAL(connected()), this, SLOT(socketConnected())); QObject::connect(wsSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); QObject::connect(wsSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(displaySslErrors(const QList<QSslError>&))); }
void SharedDaemon::handleConnection() { QTcpSocket *socket = nextPendingConnection(); if ( !socket ) return; //the connecting socket should have sent password.. //the client should be sending a password.. socket->waitForReadyRead(); if (!socket->bytesAvailable()) { //std::cout << "no bytes available to read" << std::endl; socket->close(); return; } QAbstractSocket* finalSocket = NULL; ConnectionType typeOfConnection = TcpConnection; QByteArray result = socket->readAll(); QString input(result); /// initial connection must pass password, but can optionally pass /// whether the client canRender and what the threshold value should be.. std::string lpasswd = ""; bool canRender = false; /// check if this is a WebSocketConnection QString response = ""; if(input.startsWith("{") && ParseInput(input,lpasswd,canRender)) { finalSocket = socket; typeOfConnection = TcpConnection; } /// check if this is a WebSocketConnection.. else if(QWsSocket::initializeWebSocket(result,response)) { /// this is a websocket connection, respond and get frame.. socket->write(response.toAscii()); socket->flush(); QEventLoop loop; matched_input = ""; QWsSocket* wssocket = new QWsSocket(socket); connect(wssocket,SIGNAL(frameReceived(QString)), this,SLOT(getPasswordMessage(QString))); connect(wssocket,SIGNAL(frameReceived(QString)), &loop,SLOT(quit())); /// wait for password to be sent .. /// std::cout << "waiting for password from websocket" << std::endl; loop.exec(); disconnect(wssocket,SIGNAL(frameReceived(QString)), this,SLOT(getPasswordMessage(QString))); disconnect(wssocket,SIGNAL(frameReceived(QString)), &loop,SLOT(quit())); //std::cout << matched_input.toStdString() << std::endl; if( !ParseInput(matched_input,lpasswd,canRender) ) { //std::cout << "passwords do not match: " // << matched_password.toStdString() // << " " << password << std::endl; disconnect(wssocket,SIGNAL(frameReceived(QString)), this,SLOT(getPasswordMessage(QString))); wssocket->close("passwords do not match or operation timed out"); socket->waitForDisconnected(); wssocket->deleteLater(); return; } finalSocket = wssocket; typeOfConnection = WSocketConnection; } /// not sure what connection this is, reject it.. else { //send rejection notice.. std::string errorString = "Unknown connection.."; socket->write(errorString.c_str(),errorString.length()); socket->disconnectFromHost(); socket->waitForDisconnected(); return; } //passwords match enable RemoteProcess and get port remote Process is listening to. //send host,port,security_key and whatever else so that remote machine can successfully reverse connect std::string program = "remoteApp"; std::string clientName = "newclient1"; ViewerClientConnection *newClient = new ViewerClientConnection(subject->GetViewerState(), this, clientName.c_str(), true); newClient->SetExternalClient(true); newClient->SetAdvancedRendering(canRender); stringVector args; /// assign whether connection is of type WebSocket or TCPConnection /// Register Type & Register Callback RemoteProcess::SetCustomConnectionCallback(createCustomConnection,&typeOfConnection); void* data[2]; data[0] = &typeOfConnection; data[1] = (void*)finalSocket; newClient->LaunchClient(program,args,AddNewClient,data,0,0); RemoteProcess::SetCustomConnectionCallback(0,0); /// reset connection.. /// Now that client has launched RemoveCallback.. subject->AddNewViewerClientConnection(newClient); }
void SharedDaemon::handleConnection() { QTcpSocket *socket = nextPendingConnection(); if ( !socket ) return; //the connecting socket should have sent password.. //the client should be sending a password.. socket->waitForReadyRead(); if (!socket->bytesAvailable()) { //std::cout << "no bytes available to read" << std::endl; socket->close(); return; } std::cout << "user: "******" is attempting to connect" << std::endl; QAbstractSocket* finalSocket = NULL; ConnectionType typeOfConnection = TcpConnection; QByteArray result = socket->readAll(); QString input(result); /// initial connection must pass password, but can optionally pass /// whether the client canRender and what the threshold value should be.. JSONNode output; /// check if this is a WebSocketConnection QString response = ""; if(input.startsWith("{") && ParseInput(input,output)) { finalSocket = socket; typeOfConnection = TcpConnection; } /// check if this is a WebSocketConnection.. else if(QWsSocket::initializeWebSocket(result,response)) { /// this is a websocket connection, respond and get frame.. socket->write(response.toLatin1()); socket->flush(); QEventLoop loop; matched_input = ""; QWsSocket* wssocket = new QWsSocket(socket); connect(wssocket,SIGNAL(frameReceived(QString)), this,SLOT(getPasswordMessage(QString))); connect(wssocket,SIGNAL(frameReceived(QString)), &loop,SLOT(quit())); /// wait for password to be sent .. /// std::cout << "waiting for password from websocket" << std::endl; loop.exec(); disconnect(wssocket,SIGNAL(frameReceived(QString)), this,SLOT(getPasswordMessage(QString))); disconnect(wssocket,SIGNAL(frameReceived(QString)), &loop,SLOT(quit())); //std::cout << matched_input.toStdString() << std::endl; if( !ParseInput(matched_input,output) ) { //std::cout << "passwords do not match: " // << matched_password.toStdString() // << " " << password << std::endl; wssocket->close("passwords do not match or operation timed out"); if(socket->state() != QAbstractSocket::UnconnectedState) socket->waitForDisconnected(); wssocket->deleteLater(); return; } finalSocket = wssocket; typeOfConnection = WSocketConnection; } /// not sure what connection this is, reject it.. else { //send rejection notice.. std::string errorString = "Unknown connection.."; socket->write(errorString.c_str(),errorString.length()); socket->disconnectFromHost(); socket->waitForDisconnected(); return; } //passwords match enable RemoteProcess and get port remote Process is listening to. //send host,port,security_key and whatever else so that remote machine can successfully reverse connect std::string program = "remoteApp"; std::string clientName = "newclient1"; ViewerClientConnection *newClient = new ViewerClientConnection(subject->GetViewerState(), this, clientName.c_str(), true); ViewerClientAttributes& clientAtts = newClient->GetViewerClientAttributes(); JSONNode::JSONObject jo = output.GetJsonObject(); clientAtts.SetExternalClient(true); if(jo.count("name") == 0 || jo["name"].GetString().size() == 0) clientAtts.SetTitle(socket->peerAddress().toString().toStdString()); else clientAtts.SetTitle(jo["name"].GetString()); if(jo.count("windowIds") > 0 && jo["windowIds"].GetType() == JSONNode::JSONARRAY) { const JSONNode::JSONArray& array = jo["windowIds"].GetArray(); for(size_t i = 0; i < array.size(); ++i) { const JSONNode& node = array[i]; if(node.GetType() != JSONNode::JSONINTEGER) continue; std::cout << clientAtts.GetTitle() << " requesting window: " << node.GetInt() << " " << std::endl; clientAtts.GetWindowIds().push_back(node.GetInt()); } } if(jo.count("geometry") > 0) { std::string geometry = jo["geometry"].GetString(); /// split into width & height... size_t index = geometry.find("x"); if(index != std::string::npos && index != 0 && index != geometry.size()-1) { int geometryWidth = atoi(geometry.substr(0,index).c_str()); int geometryHeight = atoi(geometry.substr(index+1).c_str()); clientAtts.SetImageWidth(geometryWidth); clientAtts.SetImageHeight(geometryHeight); //std::cout << "geometry: " << clientAtts.clientWidth << " " << clientAtts.clientHeight << std::endl; } } /// advanced rendering can be true or false (image only), or string none,image,data if(jo.count("canRender") == 0) { clientAtts.SetRenderingType(ViewerClientAttributes::None); clientAtts.GetRenderingTypes().push_back(ViewerClientAttributes::None); } else { const JSONNode& node = jo["canRender"]; QString type = node.GetString().c_str(); type = type.toLower(); /// TODO: remove the boolean check and make all current clients comply.. if(node.GetType() == JSONNode::JSONBOOL) { clientAtts.SetRenderingType( node.GetBool() ? ViewerClientAttributes::Image : ViewerClientAttributes::None); clientAtts.GetRenderingTypes().push_back(node.GetBool() ? ViewerClientAttributes::Image : ViewerClientAttributes::None); } else if(node.GetType() == JSONNode::JSONSTRING) { if(type == "image") { clientAtts.SetRenderingType(ViewerClientAttributes::Image); clientAtts.GetRenderingTypes().push_back((int)ViewerClientAttributes::Image); } else if(type == "data") { clientAtts.SetRenderingType(ViewerClientAttributes::Data); clientAtts.GetRenderingTypes().push_back((int)ViewerClientAttributes::Data); } else { clientAtts.SetRenderingType(ViewerClientAttributes::None); clientAtts.GetRenderingTypes().push_back((int)ViewerClientAttributes::None); } } else { clientAtts.SetRenderingType(ViewerClientAttributes::None); clientAtts.GetRenderingTypes().push_back((int)ViewerClientAttributes::None); } } stringVector args; /// assign whether connection is of type WebSocket or TCPConnection /// Register Type & Register Callback RemoteProcess::SetCustomConnectionCallback(createCustomConnection,&typeOfConnection); TRY { void* data[3]; data[0] = &typeOfConnection; data[1] = (void*)finalSocket; data[2] = (void*)subject->GetViewerState(); newClient->LaunchClient(program,args,AddNewClient,data,0,0); /// Now that client has launched RemoveCallback.. subject->AddNewViewerClientConnection(newClient); std::cout << "user: "******" successfully connected" << std::endl; } CATCHALL { std::cout << "user: "******" failed to connected" << std::endl; delete newClient; } ENDTRY RemoteProcess::SetCustomConnectionCallback(0,0); /// reset connection.. }