summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorqctecmdr <qctecmdr@localhost>2019-04-22 10:24:24 -0700
committerGerrit - the friendly Code Review server <code-review@localhost>2019-04-22 10:24:24 -0700
commit3d5fbdc4027820b65896abed9ea48bc7eafc18e1 (patch)
tree668699322a094066242bef8d33aeef2f153fdd95 /utils
parent1fb5c0caf5c8902f636d502dbb7d437fbc702e85 (diff)
parent07db01d9af3ac18a2be12bbf58904a6d6fb749f6 (diff)
downloadgps-3d5fbdc4027820b65896abed9ea48bc7eafc18e1.tar.gz
Merge "LocIpc derefs nullptr if stopping blocking listener"
Diffstat (limited to 'utils')
-rw-r--r--utils/LocIpc.cpp38
-rw-r--r--utils/LocIpc.h6
2 files changed, 24 insertions, 20 deletions
diff --git a/utils/LocIpc.cpp b/utils/LocIpc.cpp
index 7347b78..4178d52 100644
--- a/utils/LocIpc.cpp
+++ b/utils/LocIpc.cpp
@@ -33,6 +33,8 @@
#include <log_util.h>
#include "LocIpc.h"
+using std::string;
+
namespace loc_util {
#ifdef LOG_TAG
@@ -47,7 +49,7 @@ namespace loc_util {
class LocIpcRunnable : public LocRunnable {
friend LocIpc;
public:
- LocIpcRunnable(LocIpc& locIpc, const std::string& ipcName)
+ LocIpcRunnable(LocIpc& locIpc, const string& ipcName)
: mLocIpc(locIpc), mIpcName(ipcName) {}
bool run() override {
if (!mLocIpc.startListeningBlocking(mIpcName)) {
@@ -58,17 +60,17 @@ public:
}
private:
LocIpc& mLocIpc;
- const std::string mIpcName;
+ const string mIpcName;
};
-bool LocIpc::startListeningNonBlocking(const std::string& name) {
- mRunnable = new LocIpcRunnable(*this, name);
- std::string threadName("LocIpc-");
+bool LocIpc::startListeningNonBlocking(const string& name) {
+ auto runnable = new LocIpcRunnable(*this, name);
+ string threadName("LocIpc-");
threadName.append(name);
- return mThread.start(threadName.c_str(), mRunnable);
+ return mThread.start(threadName.c_str(), runnable);
}
-bool LocIpc::startListeningBlocking(const std::string& name) {
+bool LocIpc::startListeningBlocking(const string& name) {
bool stopRequested = false;
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
@@ -90,13 +92,14 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
LOC_LOGe("bind socket error. reason:%s", strerror(errno));
} else {
mIpcFd = fd;
+ mIpcName = name;
// inform that the socket is ready to receive message
onListenerReady();
ssize_t nBytes = 0;
- std::string msg = "";
- std::string abort = LOC_MSG_ABORT;
+ string msg = "";
+ string abort = LOC_MSG_ABORT;
while (1) {
msg.resize(LOC_MSG_BUF_LEN);
nBytes = ::recvfrom(fd, (void*)(msg.data()), msg.size(), 0, NULL, NULL);
@@ -147,20 +150,19 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
}
void LocIpc::stopListening() {
- const char *socketName = nullptr;
-
if (mIpcFd >= 0) {
- std::string abort = LOC_MSG_ABORT;
- socketName = (reinterpret_cast<LocIpcRunnable *>(mRunnable))->mIpcName.c_str();
- send(socketName, abort);
+ string abort = LOC_MSG_ABORT;
+ if (!mIpcName.empty()) {
+ send(mIpcName.c_str(), abort);
+ }
mIpcFd = -1;
}
- if (mRunnable) {
- mRunnable = nullptr;
+ if (!mIpcName.empty()) {
+ mIpcName.clear();
}
}
-bool LocIpc::send(const char name[], const std::string& data) {
+bool LocIpc::send(const char name[], const string& data) {
return send(name, (const uint8_t*)data.c_str(), data.length());
}
@@ -195,7 +197,7 @@ bool LocIpc::sendData(int fd, const sockaddr_un &addr, const uint8_t data[], uin
result = false;
}
} else {
- std::string head = LOC_MSG_HEAD;
+ string head = LOC_MSG_HEAD;
head.append(std::to_string(length));
if (::sendto(fd, head.c_str(), head.length(), 0,
(struct sockaddr*)&addr, sizeof(addr)) < 0) {
diff --git a/utils/LocIpc.h b/utils/LocIpc.h
index 90f9e1b..707df21 100644
--- a/utils/LocIpc.h
+++ b/utils/LocIpc.h
@@ -37,6 +37,8 @@
#include <sys/un.h>
#include <LocThread.h>
+using std::string;
+
namespace loc_util {
class LocIpcSender;
@@ -44,7 +46,7 @@ class LocIpcSender;
class LocIpc {
friend LocIpcSender;
public:
- inline LocIpc() : mIpcFd(-1), mRunnable(nullptr) {}
+ inline LocIpc() : mIpcFd(-1) {}
inline virtual ~LocIpc() { stopListening(); }
// Listen for new messages in current thread. Calling this funciton will
@@ -94,7 +96,7 @@ private:
int mIpcFd;
LocThread mThread;
- LocRunnable *mRunnable;
+ string mIpcName;
};
class LocIpcSender {