mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
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:
parent
fe7de83529
commit
7c2e2bd503
@ -27,48 +27,50 @@ void ApiServerLocal::handleConnection()
|
||||
}
|
||||
|
||||
ApiLocalConnectionHandler::ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock) :
|
||||
QThread(), mApiServer(apiServer), mLocalSocket(sock), mState(WAITING_PATH)
|
||||
QThread(), mApiServer(apiServer), mLocalSocket(sock)
|
||||
{
|
||||
sock->moveToThread(this);
|
||||
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
|
||||
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
|
||||
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
|
||||
connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest()));
|
||||
start();
|
||||
}
|
||||
|
||||
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;
|
||||
mState = WAITING_DATA;
|
||||
break;
|
||||
char jsonData[20480];
|
||||
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->read(jsonData, 1023);
|
||||
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;
|
||||
}
|
||||
mLocalSocket->write("{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for JSON data.\\n\",\"returncode\":\"fail\"}\"\n\0");
|
||||
quit();
|
||||
}
|
||||
}
|
||||
|
||||
ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
|
||||
{
|
||||
mLocalSocket->flush();
|
||||
mLocalSocket->close();
|
||||
delete mLocalSocket;
|
||||
mLocalSocket->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
} // namespace resource_api
|
||||
|
@ -43,7 +43,6 @@ class ApiLocalConnectionHandler : public QThread
|
||||
public:
|
||||
ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock);
|
||||
~ApiLocalConnectionHandler();
|
||||
enum State {WAITING_PATH, WAITING_DATA};
|
||||
|
||||
public slots:
|
||||
void handleRequest();
|
||||
@ -51,8 +50,8 @@ public slots:
|
||||
private:
|
||||
ApiServer* mApiServer;
|
||||
QLocalSocket* mLocalSocket;
|
||||
State mState;
|
||||
std::string reqPath;
|
||||
void _die();
|
||||
};
|
||||
|
||||
} // namespace resource_api
|
||||
|
Loading…
Reference in New Issue
Block a user