Web: support synchronous TCP/IP communication
This commit is contained in:
@@ -129,19 +129,12 @@ private:
|
||||
throw Py::OverflowError("port number is lower than 0");
|
||||
}
|
||||
|
||||
QTcpServer server;
|
||||
AppServer server(true);
|
||||
if (server.listen(QHostAddress(QString::fromLatin1(addr)), port)) {
|
||||
bool ok = server.waitForNewConnection(timeout);
|
||||
QTcpSocket* socket = server.nextPendingConnection();
|
||||
if (socket) {
|
||||
socket->waitForReadyRead();
|
||||
if (socket->bytesAvailable()) {
|
||||
QByteArray request = socket->readAll();
|
||||
std::string str = AppServer::runPython(request);
|
||||
socket->write(str.c_str());
|
||||
socket->waitForBytesWritten();
|
||||
socket->close();
|
||||
}
|
||||
}
|
||||
|
||||
server.close();
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QTcpSocket>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
#include "Server.h"
|
||||
#include <Base/Exception.h>
|
||||
@@ -109,8 +110,9 @@ const QByteArray& ServerEvent::request() const
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
AppServer::AppServer(QObject* parent)
|
||||
AppServer::AppServer( bool direct, QObject* parent)
|
||||
: QTcpServer(parent)
|
||||
, direct(direct)
|
||||
{
|
||||
PyObject* mod = PyImport_ImportModule("__main__");
|
||||
if (mod) {
|
||||
@@ -127,6 +129,7 @@ void AppServer::incomingConnection(qintptr socket)
|
||||
connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
|
||||
connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));
|
||||
s->setSocketDescriptor(socket);
|
||||
addPendingConnection(s);
|
||||
}
|
||||
|
||||
void AppServer::readClient()
|
||||
@@ -134,7 +137,13 @@ void AppServer::readClient()
|
||||
QTcpSocket* socket = (QTcpSocket*)sender();
|
||||
if (socket->bytesAvailable() > 0) {
|
||||
QByteArray request = socket->readAll();
|
||||
QCoreApplication::postEvent(this, new ServerEvent(socket, request));
|
||||
std::unique_ptr<ServerEvent> event(std::make_unique<ServerEvent>(socket, request));
|
||||
if (direct) {
|
||||
customEvent(event.get());
|
||||
}
|
||||
else {
|
||||
QCoreApplication::postEvent(this, event.release());
|
||||
}
|
||||
}
|
||||
// if (socket->state() == QTcpSocket::UnconnectedState) {
|
||||
// //mark the socket for deletion but do not destroy immediately
|
||||
@@ -154,6 +163,15 @@ void AppServer::customEvent(QEvent* e)
|
||||
QByteArray msg = ev->request();
|
||||
QTcpSocket* socket = ev->socket();
|
||||
|
||||
std::string str = handleRequest(msg);
|
||||
socket->write(str.c_str());
|
||||
if (direct)
|
||||
socket->waitForBytesWritten();
|
||||
socket->close();
|
||||
}
|
||||
|
||||
std::string AppServer::handleRequest(QByteArray msg)
|
||||
{
|
||||
std::string str;
|
||||
if (msg.startsWith("GET ")) {
|
||||
msg = QByteArray("GET = ") + msg.mid(4);
|
||||
@@ -166,8 +184,7 @@ void AppServer::customEvent(QEvent* e)
|
||||
str = runPython(msg);
|
||||
}
|
||||
|
||||
socket->write(str.c_str());
|
||||
socket->close();
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string AppServer::getRequest(const std::string& str) const
|
||||
|
||||
@@ -82,13 +82,15 @@ class AppServer : public QTcpServer
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AppServer(QObject* parent = 0);
|
||||
static std::string runPython(const QByteArray&);
|
||||
|
||||
void incomingConnection(qintptr socket);
|
||||
AppServer(bool direct = false, QObject* parent = nullptr);
|
||||
|
||||
protected:
|
||||
void incomingConnection(qintptr socket);
|
||||
void customEvent(QEvent* e);
|
||||
|
||||
private:
|
||||
std::string handleRequest(QByteArray);
|
||||
static std::string runPython(const QByteArray&);
|
||||
std::string getRequest(const std::string&) const;
|
||||
|
||||
private Q_SLOTS:
|
||||
@@ -96,6 +98,7 @@ private Q_SLOTS:
|
||||
void discardClient();
|
||||
|
||||
private:
|
||||
bool direct;
|
||||
Py::Object module;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user