take in account that QLocalSocket is of type STREAM so it doesn't emit readyRead event for each write() on the other side

This commit is contained in:
Gio 2016-07-28 17:24:17 +02:00
parent fe7de83529
commit 7c2e2bd503
2 changed files with 26 additions and 25 deletions

View File

@ -27,48 +27,50 @@ void ApiServerLocal::handleConnection()
} }
ApiLocalConnectionHandler::ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock) : ApiLocalConnectionHandler::ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock) :
QThread(), mApiServer(apiServer), mLocalSocket(sock), mState(WAITING_PATH) QThread(), mApiServer(apiServer), mLocalSocket(sock)
{ {
sock->moveToThread(this); sock->moveToThread(this);
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
connect(this, SIGNAL(finished()), this, SLOT(deleteLater())); connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest())); connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest()));
start(); start();
} }
void ApiLocalConnectionHandler::handleRequest() void ApiLocalConnectionHandler::handleRequest()
{ {
switch(mState) char path[1024];
if(mLocalSocket->readLine(path, 1023) > 0)
{ {
case WAITING_PATH:
{
char path[1024];
mLocalSocket->readLine(path, 1023);
reqPath = path; reqPath = path;
mState = WAITING_DATA; char jsonData[20480];
break; if(mLocalSocket->read(jsonData, 20479) > 0)
{
resource_api::JsonStream reqJson;
reqJson.setJsonString(std::string(jsonData));
resource_api::Request req(reqJson);
req.setPath(reqPath);
std::string resultString = mApiServer->handleRequest(req);
mLocalSocket->write(resultString.c_str(), resultString.length());
mLocalSocket->write("\n\0");
}
else
{
mLocalSocket->write("\"{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for path.\\n\",\"returncode\":\"fail\"}\"\n\0");
quit();
}
} }
case WAITING_DATA: else
{ {
char jsonData[1024]; mLocalSocket->write("{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for JSON data.\\n\",\"returncode\":\"fail\"}\"\n\0");
mLocalSocket->read(jsonData, 1023); quit();
resource_api::JsonStream reqJson;
reqJson.setJsonString(std::string(jsonData));
resource_api::Request req(reqJson);
req.setPath(reqPath);
std::string resultString = mApiServer->handleRequest(req);
mLocalSocket->write(resultString.data(), resultString.length());
mState = WAITING_PATH;
break;
}
} }
} }
ApiLocalConnectionHandler::~ApiLocalConnectionHandler() ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
{ {
mLocalSocket->flush();
mLocalSocket->close(); mLocalSocket->close();
delete mLocalSocket; mLocalSocket->deleteLater();
} }
} // namespace resource_api } // namespace resource_api

View File

@ -43,7 +43,6 @@ class ApiLocalConnectionHandler : public QThread
public: public:
ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock); ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock);
~ApiLocalConnectionHandler(); ~ApiLocalConnectionHandler();
enum State {WAITING_PATH, WAITING_DATA};
public slots: public slots:
void handleRequest(); void handleRequest();
@ -51,8 +50,8 @@ public slots:
private: private:
ApiServer* mApiServer; ApiServer* mApiServer;
QLocalSocket* mLocalSocket; QLocalSocket* mLocalSocket;
State mState;
std::string reqPath; std::string reqPath;
void _die();
}; };
} // namespace resource_api } // namespace resource_api