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) :
|
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)
|
|
||||||
{
|
|
||||||
case WAITING_PATH:
|
|
||||||
{
|
{
|
||||||
char path[1024];
|
char path[1024];
|
||||||
mLocalSocket->readLine(path, 1023);
|
if(mLocalSocket->readLine(path, 1023) > 0)
|
||||||
reqPath = path;
|
{
|
||||||
mState = WAITING_DATA;
|
reqPath = path;
|
||||||
break;
|
char jsonData[20480];
|
||||||
}
|
if(mLocalSocket->read(jsonData, 20479) > 0)
|
||||||
case WAITING_DATA:
|
|
||||||
{
|
{
|
||||||
char jsonData[1024];
|
|
||||||
mLocalSocket->read(jsonData, 1023);
|
|
||||||
resource_api::JsonStream reqJson;
|
resource_api::JsonStream reqJson;
|
||||||
reqJson.setJsonString(std::string(jsonData));
|
reqJson.setJsonString(std::string(jsonData));
|
||||||
resource_api::Request req(reqJson);
|
resource_api::Request req(reqJson);
|
||||||
req.setPath(reqPath);
|
req.setPath(reqPath);
|
||||||
std::string resultString = mApiServer->handleRequest(req);
|
std::string resultString = mApiServer->handleRequest(req);
|
||||||
mLocalSocket->write(resultString.data(), resultString.length());
|
mLocalSocket->write(resultString.c_str(), resultString.length());
|
||||||
mState = WAITING_PATH;
|
mLocalSocket->write("\n\0");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mLocalSocket->write("\"{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for path.\\n\",\"returncode\":\"fail\"}\"\n\0");
|
||||||
|
quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mLocalSocket->write("{\"data\":null,\"debug_msg\":\"ApiLocalConnectionHandler::handleRequest() Error: timeout waiting for JSON data.\\n\",\"returncode\":\"fail\"}\"\n\0");
|
||||||
|
quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
|
ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
|
||||||
{
|
{
|
||||||
|
mLocalSocket->flush();
|
||||||
mLocalSocket->close();
|
mLocalSocket->close();
|
||||||
delete mLocalSocket;
|
mLocalSocket->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace resource_api
|
} // namespace resource_api
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user