• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/corennnnn


コミットメタ情報

リビジョンd768066ef54270a0d3ccfccd50ae8238db5a2cdd (tree)
日時2009-05-13 06:02:32
作者San Mehat <san@goog...>
コミッターSan Mehat

ログメッセージ

libsysutils: Tweak SocketListener and friends

Signed-off-by: San Mehat <san@google.com>

変更サマリ

差分

--- a/include/sysutils/SocketClient.h
+++ b/include/sysutils/SocketClient.h
@@ -15,8 +15,8 @@ public:
1515
1616 int getSocket() { return mSocket; }
1717
18+ int sendMsg(int code, char *msg, bool addErrno);
1819 int sendMsg(char *msg);
19- int sendMsg(char *msg, char *data);
2020 };
2121
2222 typedef android::List<SocketClient *> SocketClientCollection;
--- a/include/sysutils/SocketListener.h
+++ b/include/sysutils/SocketListener.h
@@ -37,8 +37,8 @@ public:
3737 int startListener();
3838 int stopListener();
3939
40+ void sendBroadcast(int code, char *msg, bool addErrno);
4041 void sendBroadcast(char *msg);
41- void sendBroadcast(char *msg, char *data);
4242
4343 protected:
4444 virtual bool onDataAvailable(SocketClient *c) = 0;
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -30,7 +30,7 @@ FrameworkListener::FrameworkListener(const char *socketName) :
3030 }
3131
3232 bool FrameworkListener::onDataAvailable(SocketClient *c) {
33- char buffer[101];
33+ char buffer[255];
3434 int len;
3535
3636 if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
@@ -41,15 +41,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) {
4141 return false;
4242 }
4343
44- int start = 0;
44+ int offset = 0;
4545 int i;
4646
47- buffer[len] = '\0';
48-
4947 for (i = 0; i < len; i++) {
50- if (buffer[i] == '\0') {
51- dispatchCommand(c, buffer + start);
52- start = i + 1;
48+ if (buffer[i] == '\n') {
49+ buffer[i] = '\0';
50+ dispatchCommand(c, buffer + offset);
51+ offset = i + 1;
5352 }
5453 }
5554 return true;
@@ -60,11 +59,11 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
6059 }
6160
6261 void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
63-
62+ LOGD("Dispatching '%s'", cmd);
6463 char *cm, *last;
6564
6665 if (!(cm = strtok_r(cmd, ":", &last))) {
67- cli->sendMsg("BAD_MSG");
66+ cli->sendMsg(500, "Malformatted message", false);
6867 return;
6968 }
7069
@@ -81,7 +80,6 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
8180 }
8281 }
8382
84- LOGE("No cmd handlers defined for '%s'", cmd);
85- cli->sendMsg("UNKNOWN_CMD");
83+ cli->sendMsg(500, "Command not recognized", false);
8684 return;
8785 }
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -2,6 +2,7 @@
22 #include <errno.h>
33 #include <sys/types.h>
44 #include <pthread.h>
5+#include <string.h>
56
67 #define LOG_TAG "SocketClient"
78 #include <cutils/log.h>
@@ -13,29 +14,55 @@ SocketClient::SocketClient(int socket) {
1314 pthread_mutex_init(&mWriteMutex, NULL);
1415 }
1516
17+int SocketClient::sendMsg(int code, char *msg, bool addErrno) {
18+ char *buf;
19+
20+ if (addErrno) {
21+ buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8);
22+ sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno));
23+ } else {
24+ buf = (char *) alloca(strlen(msg) + strlen("XXX "));
25+ sprintf(buf, "%.3d %s", code, msg);
26+ }
27+ return sendMsg(buf);
28+}
29+
1630 int SocketClient::sendMsg(char *msg) {
1731 LOGD("SocketClient::sendMsg(%s)", msg);
32+
1833 if (mSocket < 0) {
1934 errno = EHOSTUNREACH;
2035 return -1;
2136 }
2237
38+ char *bp;
39+
40+ if (msg[strlen(msg)] != '\n') {
41+ bp = (char *) alloca(strlen(msg) + 1);
42+ strcpy(bp, msg);
43+ strcat(bp, "\n");
44+ } else
45+ bp = msg;
46+
47+ int rc = 0;
48+ char *p = bp;
49+ int brtw = strlen(bp);
50+
2351 pthread_mutex_lock(&mWriteMutex);
24- if (write(mSocket, msg, strlen(msg) +1) < 0) {
25- LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
52+ while(brtw) {
53+ if ((rc = write(mSocket,p, brtw)) < 0) {
54+ LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
55+ pthread_mutex_unlock(&mWriteMutex);
56+ return -1;
57+ } else if (!rc) {
58+ LOGW("0 length write :(");
59+ errno = EIO;
60+ pthread_mutex_unlock(&mWriteMutex);
61+ return -1;
62+ }
63+ p += rc;
64+ brtw -= rc;
2665 }
2766 pthread_mutex_unlock(&mWriteMutex);
2867 return 0;
2968 }
30-
31-int SocketClient::sendMsg(char *msg, char *data) {
32- char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
33- if (!buffer) {
34- errno = -ENOMEM;
35- return -1;
36- }
37- strcpy(buffer, msg);
38- strcat(buffer, data);
39- return sendMsg(buffer);
40-}
41-
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -186,27 +186,26 @@ void SocketListener::runListener() {
186186 }
187187 }
188188
189-void SocketListener::sendBroadcast(char *msg) {
189+void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) {
190190 pthread_mutex_lock(&mClientsLock);
191191 SocketClientCollection::iterator i;
192192
193193 for (i = mClients->begin(); i != mClients->end(); ++i) {
194- if ((*i)->sendMsg(msg)) {
194+ if ((*i)->sendMsg(code, msg, addErrno)) {
195195 LOGW("Error sending broadcast (%s)", strerror(errno));
196196 }
197197 }
198198 pthread_mutex_unlock(&mClientsLock);
199199 }
200200
201-void SocketListener::sendBroadcast(char *msg, char *data) {
201+void SocketListener::sendBroadcast(char *msg) {
202202 pthread_mutex_lock(&mClientsLock);
203203 SocketClientCollection::iterator i;
204204
205205 for (i = mClients->begin(); i != mClients->end(); ++i) {
206- if ((*i)->sendMsg(msg, data)) {
206+ if ((*i)->sendMsg(msg)) {
207207 LOGW("Error sending broadcast (%s)", strerror(errno));
208208 }
209209 }
210210 pthread_mutex_unlock(&mClientsLock);
211211 }
212-