libsysutils: Tweak SocketListener and friends

Signed-off-by: San Mehat <san@google.com>
This commit is contained in:
San Mehat 2009-05-12 11:16:59 -07:00
parent c73d9e43a0
commit d768066ef5
5 changed files with 64 additions and 40 deletions

View file

@ -15,8 +15,8 @@ public:
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;

View file

@ -37,8 +37,8 @@ public:
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;

View file

@ -30,7 +30,7 @@ FrameworkListener::FrameworkListener(const char *socketName) :
}
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 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) {
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::registerCmd(FrameworkCommand *cmd) {
}
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 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
}
}
LOGE("No cmd handlers defined for '%s'", cmd);
cli->sendMsg("UNKNOWN_CMD");
cli->sendMsg(500, "Command not recognized", false);
return;
}

View file

@ -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 @@ SocketClient::SocketClient(int socket) {
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);
}

View file

@ -186,6 +186,18 @@ void SocketListener::runListener() {
}
}
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 @@ void SocketListener::sendBroadcast(char *msg) {
}
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);
}