Skip to content

Commit

Permalink
Web: support synchronous TCP/IP communication
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 14, 2021
1 parent 0e73140 commit 39e02ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
9 changes: 1 addition & 8 deletions src/Mod/Web/App/AppWeb.cpp
Expand Up @@ -129,19 +129,12 @@ class Module : public Py::ExtensionModule<Module>
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();
Expand Down
25 changes: 21 additions & 4 deletions src/Mod/Web/App/Server.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QCoreApplication>
#include <QTcpSocket>
#include <stdexcept>
#include <memory>

#include "Server.h"
#include <Base/Exception.h>
Expand Down Expand Up @@ -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) {
Expand All @@ -127,14 +129,21 @@ 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()
{
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
Expand All @@ -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);
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/Mod/Web/App/Server.h
Expand Up @@ -82,20 +82,23 @@ 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:
void readClient();
void discardClient();

private:
bool direct;
Py::Object module;
};

Expand Down

0 comments on commit 39e02ca

Please sign in to comment.