Merge "LocIpc: enhance startListeningNoBlocking with ready callback"
This commit is contained in:
commit
e301338767
3 changed files with 21 additions and 11 deletions
|
@ -70,6 +70,7 @@ public :
|
||||||
inline const MsgTask* getMsgTask() { return mMsgTask; }
|
inline const MsgTask* getMsgTask() { return mMsgTask; }
|
||||||
void subscribe(bool yes);
|
void subscribe(bool yes);
|
||||||
|
|
||||||
|
protected:
|
||||||
void onReceive(const std::string& data) override;
|
void onReceive(const std::string& data) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright (c) 2017 The Linux Foundation. All rights reserved.
|
/* Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are
|
* modification, are permitted provided that the following conditions are
|
||||||
|
@ -76,6 +76,7 @@ bool LocIpc::startListeningNonBlocking(const std::string& name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LocIpc::startListeningBlocking(const std::string& name) {
|
bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
|
|
||||||
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LOC_LOGe("create socket error. reason:%s", strerror(errno));
|
LOC_LOGe("create socket error. reason:%s", strerror(errno));
|
||||||
|
@ -100,6 +101,9 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
|
||||||
|
|
||||||
mIpcFd = fd;
|
mIpcFd = fd;
|
||||||
|
|
||||||
|
// inform that the socket is ready to receive message
|
||||||
|
onListenerReady();
|
||||||
|
|
||||||
ssize_t nBytes = 0;
|
ssize_t nBytes = 0;
|
||||||
std::string msg = "";
|
std::string msg = "";
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright (c) 2017 The Linux Foundation. All rights reserved.
|
/* Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions are
|
* modification, are permitted provided that the following conditions are
|
||||||
|
@ -66,15 +66,6 @@ public:
|
||||||
// Stop listening to new messages.
|
// Stop listening to new messages.
|
||||||
void stopListening();
|
void stopListening();
|
||||||
|
|
||||||
// Callback function for receiving incoming messages.
|
|
||||||
// Override this function in your derived class to process incoming messages.
|
|
||||||
// For each received message, this callback function will be called once.
|
|
||||||
// This callback function will be called in the calling thread of startListeningBlocking
|
|
||||||
// or in the new LocThread created by startListeningNonBlocking.
|
|
||||||
//
|
|
||||||
// Argument data contains the received message. You need to parse it.
|
|
||||||
virtual void onReceive(const std::string& /*data*/) {}
|
|
||||||
|
|
||||||
// Send out a message.
|
// Send out a message.
|
||||||
// Call this function to send a message in argument data to socket in argument name.
|
// Call this function to send a message in argument data to socket in argument name.
|
||||||
//
|
//
|
||||||
|
@ -84,6 +75,20 @@ public:
|
||||||
static bool send(const char name[], const std::string& data);
|
static bool send(const char name[], const std::string& data);
|
||||||
static bool send(const char name[], const uint8_t data[], uint32_t length);
|
static bool send(const char name[], const uint8_t data[], uint32_t length);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Callback function for receiving incoming messages.
|
||||||
|
// Override this function in your derived class to process incoming messages.
|
||||||
|
// For each received message, this callback function will be called once.
|
||||||
|
// This callback function will be called in the calling thread of startListeningBlocking
|
||||||
|
// or in the new LocThread created by startListeningNonBlocking.
|
||||||
|
//
|
||||||
|
// Argument data contains the received message. You need to parse it.
|
||||||
|
inline virtual void onReceive(const std::string& /*data*/) {}
|
||||||
|
|
||||||
|
// LocIpc client can overwrite this function to get notification
|
||||||
|
// when the socket for LocIpc is ready to receive messages.
|
||||||
|
inline virtual void onListenerReady() {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool sendData(int fd, const sockaddr_un& addr,
|
static bool sendData(int fd, const sockaddr_un& addr,
|
||||||
const uint8_t data[], uint32_t length);
|
const uint8_t data[], uint32_t length);
|
||||||
|
|
Loading…
Reference in a new issue