Merge "fix: LocIpc client app unable to exit" into location.lnx.3.9
This commit is contained in:
commit
a35498006d
2 changed files with 25 additions and 15 deletions
|
@ -28,19 +28,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/un.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <error.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <array>
|
|
||||||
#include <loc_pla.h>
|
|
||||||
#include <log_util.h>
|
#include <log_util.h>
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include "gps_extended_c.h"
|
|
||||||
#include "LocIpc.h"
|
#include "LocIpc.h"
|
||||||
|
|
||||||
namespace loc_util {
|
namespace loc_util {
|
||||||
|
@ -52,8 +42,10 @@ namespace loc_util {
|
||||||
|
|
||||||
#define LOC_MSG_BUF_LEN 8192
|
#define LOC_MSG_BUF_LEN 8192
|
||||||
#define LOC_MSG_HEAD "$MSGLEN$"
|
#define LOC_MSG_HEAD "$MSGLEN$"
|
||||||
|
#define LOC_MSG_ABORT "LocIpcMsg::ABORT"
|
||||||
|
|
||||||
class LocIpcRunnable : public LocRunnable {
|
class LocIpcRunnable : public LocRunnable {
|
||||||
|
friend LocIpc;
|
||||||
public:
|
public:
|
||||||
LocIpcRunnable(LocIpc& locIpc, const std::string& ipcName)
|
LocIpcRunnable(LocIpc& locIpc, const std::string& ipcName)
|
||||||
: mLocIpc(locIpc), mIpcName(ipcName) {}
|
: mLocIpc(locIpc), mIpcName(ipcName) {}
|
||||||
|
@ -70,10 +62,10 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
bool LocIpc::startListeningNonBlocking(const std::string& name) {
|
bool LocIpc::startListeningNonBlocking(const std::string& name) {
|
||||||
mRunnable.reset(new LocIpcRunnable(*this, name));
|
mRunnable = new LocIpcRunnable(*this, name);
|
||||||
std::string threadName("LocIpc-");
|
std::string threadName("LocIpc-");
|
||||||
threadName.append(name);
|
threadName.append(name);
|
||||||
return mThread.start(threadName.c_str(), mRunnable.get());
|
return mThread.start(threadName.c_str(), mRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LocIpc::startListeningBlocking(const std::string& name) {
|
bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
|
@ -107,6 +99,7 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
|
|
||||||
ssize_t nBytes = 0;
|
ssize_t nBytes = 0;
|
||||||
std::string msg = "";
|
std::string msg = "";
|
||||||
|
std::string abort = LOC_MSG_ABORT;
|
||||||
while (1) {
|
while (1) {
|
||||||
msg.resize(LOC_MSG_BUF_LEN);
|
msg.resize(LOC_MSG_BUF_LEN);
|
||||||
nBytes = ::recvfrom(mIpcFd, (void*)(msg.data()), msg.size(), 0, NULL, NULL);
|
nBytes = ::recvfrom(mIpcFd, (void*)(msg.data()), msg.size(), 0, NULL, NULL);
|
||||||
|
@ -116,6 +109,11 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strncmp(msg.data(), abort.c_str(), abort.length()) == 0) {
|
||||||
|
LOC_LOGi("recvd abort msg.data %s", msg.data());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (strncmp(msg.data(), LOC_MSG_HEAD, sizeof(LOC_MSG_HEAD) - 1)) {
|
if (strncmp(msg.data(), LOC_MSG_HEAD, sizeof(LOC_MSG_HEAD) - 1)) {
|
||||||
// short message
|
// short message
|
||||||
msg.resize(nBytes);
|
msg.resize(nBytes);
|
||||||
|
@ -142,7 +140,6 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
if (mStopRequested) {
|
if (mStopRequested) {
|
||||||
mStopRequested = false;
|
mStopRequested = false;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
LOC_LOGe("cannot read socket. reason:%s", strerror(errno));
|
LOC_LOGe("cannot read socket. reason:%s", strerror(errno));
|
||||||
(void)::close(mIpcFd);
|
(void)::close(mIpcFd);
|
||||||
|
@ -152,14 +149,28 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocIpc::stopListening() {
|
void LocIpc::stopListening() {
|
||||||
|
|
||||||
|
const char *socketName = nullptr;
|
||||||
mStopRequested = true;
|
mStopRequested = true;
|
||||||
|
|
||||||
|
if (mRunnable) {
|
||||||
|
std::string abort = LOC_MSG_ABORT;
|
||||||
|
socketName = (reinterpret_cast<LocIpcRunnable *>(mRunnable))->mIpcName.c_str();
|
||||||
|
send(socketName, abort);
|
||||||
|
mRunnable = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
if (mIpcFd >= 0) {
|
if (mIpcFd >= 0) {
|
||||||
if (::close(mIpcFd)) {
|
if (::close(mIpcFd)) {
|
||||||
LOC_LOGe("cannot close socket:%s", strerror(errno));
|
LOC_LOGe("cannot close socket:%s", strerror(errno));
|
||||||
}
|
}
|
||||||
mIpcFd = -1;
|
mIpcFd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//delete from the file system at the end
|
||||||
|
if (socketName) {
|
||||||
|
unlink(socketName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LocIpc::send(const char name[], const std::string& data) {
|
bool LocIpc::send(const char name[], const std::string& data) {
|
||||||
|
|
|
@ -32,7 +32,6 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
@ -96,7 +95,7 @@ private:
|
||||||
int mIpcFd;
|
int mIpcFd;
|
||||||
bool mStopRequested;
|
bool mStopRequested;
|
||||||
LocThread mThread;
|
LocThread mThread;
|
||||||
std::unique_ptr<LocRunnable> mRunnable;
|
LocRunnable *mRunnable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LocIpcSender {
|
class LocIpcSender {
|
||||||
|
|
Loading…
Reference in a new issue