// - More Functions for all socket interactions void WebServer::SocketClosed(QString ID){ qDebug() << "Socket Closed:" << ID; for(int i=0; i<OpenSockets.length(); i++){ if(OpenSockets[i]->ID()==ID){ delete OpenSockets.takeAt(i); break; } } QTimer::singleShot(0,this, SLOT(NewSocketConnection()) ); //check for a new connection }
bool WebServer::setupTcp(quint16 port){ TCPServer = new SslServer(this); //Setup Connections connect(TCPServer, SIGNAL(newConnection()), this, SLOT(NewSocketConnection()) ); connect(TCPServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(NewConnectError(QAbstractSocket::SocketError)) ); //Now start the server return TCPServer->listen(QHostAddress::Any, port); }
void WebServer::NewConnectError(QAbstractSocket::SocketError err){ if(csock!=0){ qWarning() << "New Connection Error["+QString::number(err)+"]:" << csock->errorString(); csock->close(); }else{ qWarning() << "New Connection Error["+QString::number(err)+"]:" << this->errorString(); } csock = 0; //remove the current socket QTimer::singleShot(0,this, SLOT(NewSocketConnection()) ); //check for a new connection }
// New Connection Signals void WebServer::NewSocketConnection(){ if(!this->hasPendingConnections()){ return; } qDebug() << "New Socket Connection"; if(csock!=0){ qDebug() << " - Placed in queue"; return;} if(idletimer->isActive()){ idletimer->stop(); } csock = this->nextPendingConnection(); connect(csock, SIGNAL(textMessageReceived(const QString&)), this, SLOT(EvaluateMessage(const QString&)) ); connect(csock, SIGNAL(binaryMessageReceived(const QByteArray&)), this, SLOT(EvaluateMessage(const QByteArray&)) ); connect(csock, SIGNAL(aboutToClose()), this, SLOT(SocketClosing()) ); connect(csock, SIGNAL(disconnected()), this, SLOT(NewSocketConnection()) ); if(csock == 0){ qWarning() << " - new connection invalid, skipping..."; QTimer::singleShot(10, this, SLOT(NewSocketConnection())); return; } qDebug() << " - Accepting connection:" << csock->origin(); idletimer->start(); //QTimer::singleShot(0,this, SLOT(EvaluateConnection())); }
//======================= // PUBLIC //======================= WebServer::WebServer() : QWebSocketServer("pc-restserver", QWebSocketServer::NonSecureMode){ csock = 0; //no current socket connected //Setup all the various settings idletimer = new QTimer(this); idletimer->setInterval(5000); //every 5 seconds idletimer->setSingleShot(true); //Any SSL changes /*QSslConfiguration ssl = this->sslConfiguration(); ssl.setProtocol(QSsl::SecureProtocols); this->setSslConfiguration(ssl);*/ //Setup Connections connect(idletimer, SIGNAL(timeout()), this, SLOT(checkIdle()) ); connect(this, SIGNAL(closed()), this, SLOT(ServerClosed()) ); connect(this, SIGNAL(serverError(QWebSocketProtocol::CloseCode)), this, SLOT(ServerError(QWebSocketProtocol::CloseCode)) ); connect(this, SIGNAL(newConnection()), this, SLOT(NewSocketConnection()) ); connect(this, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(NewConnectError(QAbstractSocket::SocketError)) ); connect(this, SIGNAL(originAuthenticationRequired(QWebSocketCorsAuthenticator*)), this, SLOT(OriginAuthRequired(QWebSocketCorsAuthenticator*)) ); connect(this, SIGNAL(peerVerifyError(const QSslError&)), this, SLOT(PeerVerifyError(const QSslError&)) ); connect(this, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(SslErrors(const QList<QSslError>&)) ); }
//=================== // PRIVATE //=================== bool WebServer::setupWebSocket(quint16 port){ WSServer = new QWebSocketServer("sysadm-server", QWebSocketServer::SecureMode, this); //SSL Configuration QSslConfiguration config = QSslConfiguration::defaultConfiguration(); QFile CF( QStringLiteral(SSLCERTFILE) ); if(CF.open(QIODevice::ReadOnly) ){ QSslCertificate CERT(&CF,QSsl::Pem); config.setLocalCertificate( CERT ); CF.close(); }else{ qWarning() << "Could not read WS certificate file:" << CF.fileName(); } QFile KF( QStringLiteral(SSLKEYFILE)); if(KF.open(QIODevice::ReadOnly) ){ QSslKey KEY(&KF, QSsl::Rsa, QSsl::Pem); config.setPrivateKey( KEY ); KF.close(); }else{ qWarning() << "Could not read WS key file:" << KF.fileName(); } config.setPeerVerifyMode(QSslSocket::VerifyNone); config.setProtocol(SSLVERSION); WSServer->setSslConfiguration(config); //Setup Connections connect(WSServer, SIGNAL(newConnection()), this, SLOT(NewSocketConnection()) ); connect(WSServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(NewConnectError(QAbstractSocket::SocketError)) ); // -- websocket specific signals connect(WSServer, SIGNAL(closed()), this, SLOT(ServerClosed()) ); connect(WSServer, SIGNAL(serverError(QWebSocketProtocol::CloseCode)), this, SLOT(ServerError(QWebSocketProtocol::CloseCode)) ); connect(WSServer, SIGNAL(originAuthenticationRequired(QWebSocketCorsAuthenticator*)), this, SLOT(OriginAuthRequired(QWebSocketCorsAuthenticator*)) ); connect(WSServer, SIGNAL(peerVerifyError(const QSslError&)), this, SLOT(PeerVerifyError(const QSslError&)) ); connect(WSServer, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(SslErrors(const QList<QSslError>&)) ); connect(WSServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(ConnectError(QAbstractSocket::SocketError)) ); //Now start the server return WSServer->listen(QHostAddress::Any, port); }
void WebServer::NewConnectError(QAbstractSocket::SocketError err){ qWarning() << "New Connection Error["+QString::number(err)+"]:" << ( (WSServer!=0) ? WSServer->errorString() : TCPServer->errorString()); QTimer::singleShot(0,this, SLOT(NewSocketConnection()) ); //check for a new connection }