void TvEventHandler::readClient() { QTcpSocket* socket = (QTcpSocket*)sender(); // reject everything, except the tv if(socket->peerAddress() != m_host){ socket->close(); socket->deleteLater(); qCWarning(dcLgSmartTv) << "Event handler -> rejecting connection from " << socket->peerAddress().toString(); return; } // the tv sends first the header (POST /udap/api/.... HTTP/1.1) // in the scond package the tv sends the information (xml format) while(!socket->atEnd()){ QByteArray data = socket->readAll(); // check if we got information if(data.startsWith("<?xml") && m_expectingData){ m_expectingData = false; // Answere with OK QTextStream textStream(socket); textStream.setAutoDetectUnicode(true); textStream << "HTTP/1.0 200 OK\r\n" "Content-Type: text/html; charset=\"utf-8\"\r\n" "User-Agent: UDAP/2.0 guh\r\n" << "Date: " << QDateTime::currentDateTime().toString() << "\n"; emit eventOccured(data); } // check if we got header if (data.startsWith("POST") && !m_expectingData) { m_expectingData = true; QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*")); qCDebug(dcLgSmartTv) << "event handler -> event occured" << "http://" << m_host.toString() << ":" << m_port << tokens[1]; } } }
void ServerNetworkComm::newData() { // which client has sent data ? QTcpSocket * socket = qobject_cast<QTcpSocket *>(sender()); std::string target; std::string receiver; std::string clientId; std::string frameId; QString errorMsg; try { char * frame; QDataStream in(socket); in.setVersion(QDataStream::Qt_4_6); // set stream version // if the client sends two messages very close in time, it is possible that // the server never gets the second one. // So, it is useful to explicitly read the socket until the end is reached. while(!socket->atEnd()) { in.readBytes(frame, m_blockSize); m_bytesRecieved += m_blockSize + (int)sizeof(quint32); XmlDoc::Ptr xmldoc = XML::parse_cstring( frame, m_blockSize - 1 ); // free the buffer delete[] frame; frame = nullptr; XmlNode nodedoc = Protocol::goto_doc_node(*xmldoc.get()); SignalFrame * sig_frame = new SignalFrame( nodedoc.content->first_node() ); sig_frame->xml_doc = xmldoc; target = this->getAttr(sig_frame->node, "target", errorMsg); receiver = this->getAttr(sig_frame->node, "receiver", errorMsg); clientId = this->getAttr(sig_frame->node, "clientid", errorMsg); frameId = this->getAttr(sig_frame->node, "frameid", errorMsg); if(errorMsg.isEmpty()) { if(target == "client_registration") { if(!m_clients[socket].empty()) errorMsg = "This client has already been registered."; else { m_clients[socket] = clientId; // Build the reply SignalFrame reply = sig_frame->create_reply(); SignalOptions roptions( reply ); roptions.add_option< OptionT<bool> >("accepted", true); roptions.flush(); this->send(socket, *xmldoc.get()); emit newClientConnected(clientId); } } else { if(m_clients[socket].empty()) errorMsg = "The signal came from an unregistered client."; else if(m_clients[socket] != clientId) errorMsg = QString("The client id '%1' (used for registration) " "and '%2' (used for identification) do not " "match.").arg(m_clients[socket].c_str()).arg(clientId.c_str()); else ServerRoot::processSignal(target, receiver, clientId, frameId, *sig_frame); } } m_blockSize = 0; } } catch(Exception & e) { this->sendMessage(socket, e.what(), LogMessage::EXCEPTION); } catch(std::exception & stde) { this->sendMessage(socket, stde.what(), LogMessage::EXCEPTION); } catch(...) { errorMsg = QString("An unknown exception has been caught."); } if(!errorMsg.isEmpty()) this->sendFrameRejected(socket, frameId, SERVER_CORE_PATH, errorMsg); }