Web: [skip ci] implement blocking tcp server
This commit is contained in:
@@ -39,11 +39,10 @@
|
||||
/*
|
||||
import socket
|
||||
import threading
|
||||
import SocketServer
|
||||
|
||||
|
||||
ip = "127.0.0.1"
|
||||
port=54880
|
||||
port = 54880
|
||||
|
||||
def client(ip, port, message):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@@ -51,15 +50,14 @@ def client(ip, port, message):
|
||||
try:
|
||||
sock.sendall(message)
|
||||
response = sock.recv(1024)
|
||||
print "Received: {}".format(response)
|
||||
print ("Received: {}".format(response))
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
|
||||
client(ip, port, "print 'Hello World 1'")
|
||||
client(ip, port, "import FreeCAD\nFreeCAD.newDocument()")
|
||||
client(ip, port, "Hello World 3\n")
|
||||
client(ip, port, b"print ('Hello World')")
|
||||
client(ip, port, b"import FreeCAD\nFreeCAD.newDocument()")
|
||||
|
||||
*/
|
||||
|
||||
@@ -72,6 +70,12 @@ public:
|
||||
add_varargs_method("startServer",&Module::startServer,
|
||||
"startServer(address=127.0.0.1,port=0) -- Start a server."
|
||||
);
|
||||
add_varargs_method("waitForConnection",&Module::waitForConnection,
|
||||
"waitForConnection(address=127.0.0.1,port=0,timeout=0)\n"
|
||||
"Start a server, wait for connection and close server.\n"
|
||||
"Its use is disadvised in a the GUI version, since it will\n"
|
||||
"stop responding until the function returns."
|
||||
);
|
||||
add_varargs_method("registerServerFirewall",&Module::registerServerFirewall,
|
||||
"registerServerFirewall(callable(string)) -- Register a firewall."
|
||||
);
|
||||
@@ -111,6 +115,45 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
Py::Object waitForConnection(const Py::Tuple& args)
|
||||
{
|
||||
const char* addr = "127.0.0.1";
|
||||
int port = 0;
|
||||
int timeout = 0;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "|sii",&addr,&port, &timeout))
|
||||
throw Py::Exception();
|
||||
if (port > USHRT_MAX) {
|
||||
throw Py::OverflowError("port number is greater than maximum");
|
||||
}
|
||||
else if (port < 0) {
|
||||
throw Py::OverflowError("port number is lower than 0");
|
||||
}
|
||||
|
||||
QTcpServer server;
|
||||
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();
|
||||
return Py::Boolean(ok);
|
||||
}
|
||||
else {
|
||||
std::stringstream out;
|
||||
out << "Server failed to listen at address " << addr << " and port " << port;
|
||||
throw Py::RuntimeError(out.str());
|
||||
}
|
||||
}
|
||||
|
||||
Py::Object registerServerFirewall(const Py::Tuple& args)
|
||||
{
|
||||
PyObject* obj;
|
||||
|
||||
@@ -151,6 +151,14 @@ void AppServer::customEvent(QEvent* e)
|
||||
QByteArray msg = ev->request();
|
||||
QTcpSocket* socket = ev->socket();
|
||||
|
||||
std::string str = runPython(msg);
|
||||
|
||||
socket->write(str.c_str());
|
||||
socket->close();
|
||||
}
|
||||
|
||||
std::string AppServer::runPython(const QByteArray& msg)
|
||||
{
|
||||
std::string str;
|
||||
|
||||
try {
|
||||
@@ -175,8 +183,7 @@ void AppServer::customEvent(QEvent* e)
|
||||
str = "Unknown exception thrown";
|
||||
}
|
||||
|
||||
socket->write(str.c_str());
|
||||
socket->close();
|
||||
return str;
|
||||
}
|
||||
|
||||
#include "moc_Server.cpp"
|
||||
|
||||
@@ -83,6 +83,7 @@ class AppServer : public QTcpServer
|
||||
|
||||
public:
|
||||
AppServer(QObject* parent = 0);
|
||||
static std::string runPython(const QByteArray&);
|
||||
|
||||
#if QT_VERSION >=0x050000
|
||||
void incomingConnection(qintptr socket);
|
||||
|
||||
Reference in New Issue
Block a user