libsysutils: Tweak SocketListener and friends
Signed-off-by: San Mehat <san@google.com>
diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h
index 39ec7ae..cde64a5 100644
--- a/include/sysutils/SocketClient.h
+++ b/include/sysutils/SocketClient.h
@@ -15,8 +15,8 @@
int getSocket() { return mSocket; }
+ int sendMsg(int code, char *msg, bool addErrno);
int sendMsg(char *msg);
- int sendMsg(char *msg, char *data);
};
typedef android::List<SocketClient *> SocketClientCollection;
diff --git a/include/sysutils/SocketListener.h b/include/sysutils/SocketListener.h
index be97421..30d050d 100644
--- a/include/sysutils/SocketListener.h
+++ b/include/sysutils/SocketListener.h
@@ -37,8 +37,8 @@
int startListener();
int stopListener();
+ void sendBroadcast(int code, char *msg, bool addErrno);
void sendBroadcast(char *msg);
- void sendBroadcast(char *msg, char *data);
protected:
virtual bool onDataAvailable(SocketClient *c) = 0;
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index 9210ca5..2a96e9a 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -30,7 +30,7 @@
}
bool FrameworkListener::onDataAvailable(SocketClient *c) {
- char buffer[101];
+ char buffer[255];
int len;
if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
@@ -41,15 +41,14 @@
return false;
}
- int start = 0;
+ int offset = 0;
int i;
- buffer[len] = '\0';
-
for (i = 0; i < len; i++) {
- if (buffer[i] == '\0') {
- dispatchCommand(c, buffer + start);
- start = i + 1;
+ if (buffer[i] == '\n') {
+ buffer[i] = '\0';
+ dispatchCommand(c, buffer + offset);
+ offset = i + 1;
}
}
return true;
@@ -60,11 +59,11 @@
}
void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
-
+ LOGD("Dispatching '%s'", cmd);
char *cm, *last;
if (!(cm = strtok_r(cmd, ":", &last))) {
- cli->sendMsg("BAD_MSG");
+ cli->sendMsg(500, "Malformatted message", false);
return;
}
@@ -81,7 +80,6 @@
}
}
- LOGE("No cmd handlers defined for '%s'", cmd);
- cli->sendMsg("UNKNOWN_CMD");
+ cli->sendMsg(500, "Command not recognized", false);
return;
}
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 6db62b3..930fa4c 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -2,6 +2,7 @@
#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
+#include <string.h>
#define LOG_TAG "SocketClient"
#include <cutils/log.h>
@@ -13,29 +14,55 @@
pthread_mutex_init(&mWriteMutex, NULL);
}
+int SocketClient::sendMsg(int code, char *msg, bool addErrno) {
+ char *buf;
+
+ if (addErrno) {
+ buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8);
+ sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno));
+ } else {
+ buf = (char *) alloca(strlen(msg) + strlen("XXX "));
+ sprintf(buf, "%.3d %s", code, msg);
+ }
+ return sendMsg(buf);
+}
+
int SocketClient::sendMsg(char *msg) {
LOGD("SocketClient::sendMsg(%s)", msg);
+
if (mSocket < 0) {
errno = EHOSTUNREACH;
return -1;
}
+ char *bp;
+
+ if (msg[strlen(msg)] != '\n') {
+ bp = (char *) alloca(strlen(msg) + 1);
+ strcpy(bp, msg);
+ strcat(bp, "\n");
+ } else
+ bp = msg;
+
+ int rc = 0;
+ char *p = bp;
+ int brtw = strlen(bp);
+
pthread_mutex_lock(&mWriteMutex);
- if (write(mSocket, msg, strlen(msg) +1) < 0) {
- LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
+ while(brtw) {
+ if ((rc = write(mSocket,p, brtw)) < 0) {
+ LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
+ pthread_mutex_unlock(&mWriteMutex);
+ return -1;
+ } else if (!rc) {
+ LOGW("0 length write :(");
+ errno = EIO;
+ pthread_mutex_unlock(&mWriteMutex);
+ return -1;
+ }
+ p += rc;
+ brtw -= rc;
}
pthread_mutex_unlock(&mWriteMutex);
return 0;
}
-
-int SocketClient::sendMsg(char *msg, char *data) {
- char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
- if (!buffer) {
- errno = -ENOMEM;
- return -1;
- }
- strcpy(buffer, msg);
- strcat(buffer, data);
- return sendMsg(buffer);
-}
-
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index cb69bfd..57f31f1 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -186,6 +186,18 @@
}
}
+void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) {
+ pthread_mutex_lock(&mClientsLock);
+ SocketClientCollection::iterator i;
+
+ for (i = mClients->begin(); i != mClients->end(); ++i) {
+ if ((*i)->sendMsg(code, msg, addErrno)) {
+ LOGW("Error sending broadcast (%s)", strerror(errno));
+ }
+ }
+ pthread_mutex_unlock(&mClientsLock);
+}
+
void SocketListener::sendBroadcast(char *msg) {
pthread_mutex_lock(&mClientsLock);
SocketClientCollection::iterator i;
@@ -197,16 +209,3 @@
}
pthread_mutex_unlock(&mClientsLock);
}
-
-void SocketListener::sendBroadcast(char *msg, char *data) {
- pthread_mutex_lock(&mClientsLock);
- SocketClientCollection::iterator i;
-
- for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(msg, data)) {
- LOGW("Error sending broadcast (%s)", strerror(errno));
- }
- }
- pthread_mutex_unlock(&mClientsLock);
-}
-