New NativeDaemonConnector protocol adds a seqnum.
Allows for one socket to be multiplexed for multiple requests.
Doesn't use command sequence numbers for broadcasts - would make no sense.
Doesn't alter current default behavior so OEM's using these classes
won't notice a difference.
bug:5864209
Change-Id: Ie3b19c4f81eea868569229a365c8cb7de249c2dd
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index 3f871ea..0361641 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -29,18 +29,25 @@
#include <sysutils/SocketListener.h>
#include <sysutils/SocketClient.h>
+#define LOG_NDEBUG 0
+
SocketListener::SocketListener(const char *socketName, bool listen) {
- mListen = listen;
- mSocketName = socketName;
- mSock = -1;
- pthread_mutex_init(&mClientsLock, NULL);
- mClients = new SocketClientCollection();
+ init(socketName, -1, listen, false);
}
SocketListener::SocketListener(int socketFd, bool listen) {
+ init(NULL, socketFd, listen, false);
+}
+
+SocketListener::SocketListener(const char *socketName, bool listen, bool useCmdNum) {
+ init(socketName, -1, listen, useCmdNum);
+}
+
+void SocketListener::init(const char *socketName, int socketFd, bool listen, bool useCmdNum) {
mListen = listen;
- mSocketName = NULL;
+ mSocketName = socketName;
mSock = socketFd;
+ mUseCmdNum = useCmdNum;
pthread_mutex_init(&mClientsLock, NULL);
mClients = new SocketClientCollection();
}
@@ -73,13 +80,14 @@
mSocketName, strerror(errno));
return -1;
}
+ SLOGV("got mSock = %d for %s", mSock, mSocketName);
}
if (mListen && listen(mSock, 4) < 0) {
SLOGE("Unable to listen on socket (%s)", strerror(errno));
return -1;
} else if (!mListen)
- mClients->push_back(new SocketClient(mSock, false));
+ mClients->push_back(new SocketClient(mSock, false, mUseCmdNum));
if (pipe(mCtrlPipe)) {
SLOGE("pipe failed (%s)", strerror(errno));
@@ -164,11 +172,11 @@
max = fd;
}
pthread_mutex_unlock(&mClientsLock);
-
+ SLOGV("mListen=%d, max=%d, mSocketName=%s", mListen, max, mSocketName);
if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {
if (errno == EINTR)
continue;
- SLOGE("select failed (%s)", strerror(errno));
+ SLOGE("select failed (%s) mListen=%d, max=%d", strerror(errno), mListen, max);
sleep(1);
continue;
} else if (!rc)
@@ -184,6 +192,7 @@
do {
alen = sizeof(addr);
c = accept(mSock, &addr, &alen);
+ SLOGV("%s got %d from accept", mSocketName, c);
} while (c < 0 && errno == EINTR);
if (c < 0) {
SLOGE("accept failed (%s)", strerror(errno));
@@ -191,7 +200,7 @@
continue;
}
pthread_mutex_lock(&mClientsLock);
- mClients->push_back(new SocketClient(c, true));
+ mClients->push_back(new SocketClient(c, true, mUseCmdNum));
pthread_mutex_unlock(&mClientsLock);
}
@@ -217,6 +226,7 @@
* connection-based, remove and destroy it */
if (!onDataAvailable(c) && mListen) {
/* Remove the client from our array */
+ SLOGV("going to zap %d for %s", c->getSocket(), mSocketName);
pthread_mutex_lock(&mClientsLock);
for (it = mClients->begin(); it != mClients->end(); ++it) {
if (*it == c) {
@@ -238,19 +248,8 @@
SocketClientCollection::iterator i;
for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(code, msg, addErrno)) {
- SLOGW("Error sending broadcast (%s)", strerror(errno));
- }
- }
- pthread_mutex_unlock(&mClientsLock);
-}
-
-void SocketListener::sendBroadcast(const char *msg) {
- pthread_mutex_lock(&mClientsLock);
- SocketClientCollection::iterator i;
-
- for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(msg)) {
+ // broadcasts are unsolicited and should not include a cmd number
+ if ((*i)->sendMsg(code, msg, addErrno, false)) {
SLOGW("Error sending broadcast (%s)", strerror(errno));
}
}