system/corennnnn
リビジョン | d768066ef54270a0d3ccfccd50ae8238db5a2cdd (tree) |
---|---|
日時 | 2009-05-13 06:02:32 |
作者 | San Mehat <san@goog...> |
コミッター | San Mehat |
libsysutils: Tweak SocketListener and friends
@@ -15,8 +15,8 @@ public: | ||
15 | 15 | |
16 | 16 | int getSocket() { return mSocket; } |
17 | 17 | |
18 | + int sendMsg(int code, char *msg, bool addErrno); | |
18 | 19 | int sendMsg(char *msg); |
19 | - int sendMsg(char *msg, char *data); | |
20 | 20 | }; |
21 | 21 | |
22 | 22 | typedef android::List<SocketClient *> SocketClientCollection; |
@@ -37,8 +37,8 @@ public: | ||
37 | 37 | int startListener(); |
38 | 38 | int stopListener(); |
39 | 39 | |
40 | + void sendBroadcast(int code, char *msg, bool addErrno); | |
40 | 41 | void sendBroadcast(char *msg); |
41 | - void sendBroadcast(char *msg, char *data); | |
42 | 42 | |
43 | 43 | protected: |
44 | 44 | virtual bool onDataAvailable(SocketClient *c) = 0; |
@@ -30,7 +30,7 @@ FrameworkListener::FrameworkListener(const char *socketName) : | ||
30 | 30 | } |
31 | 31 | |
32 | 32 | bool FrameworkListener::onDataAvailable(SocketClient *c) { |
33 | - char buffer[101]; | |
33 | + char buffer[255]; | |
34 | 34 | int len; |
35 | 35 | |
36 | 36 | if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) { |
@@ -41,15 +41,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) { | ||
41 | 41 | return false; |
42 | 42 | } |
43 | 43 | |
44 | - int start = 0; | |
44 | + int offset = 0; | |
45 | 45 | int i; |
46 | 46 | |
47 | - buffer[len] = '\0'; | |
48 | - | |
49 | 47 | 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; | |
53 | 52 | } |
54 | 53 | } |
55 | 54 | return true; |
@@ -60,11 +59,11 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) { | ||
60 | 59 | } |
61 | 60 | |
62 | 61 | void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { |
63 | - | |
62 | + LOGD("Dispatching '%s'", cmd); | |
64 | 63 | char *cm, *last; |
65 | 64 | |
66 | 65 | if (!(cm = strtok_r(cmd, ":", &last))) { |
67 | - cli->sendMsg("BAD_MSG"); | |
66 | + cli->sendMsg(500, "Malformatted message", false); | |
68 | 67 | return; |
69 | 68 | } |
70 | 69 |
@@ -81,7 +80,6 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { | ||
81 | 80 | } |
82 | 81 | } |
83 | 82 | |
84 | - LOGE("No cmd handlers defined for '%s'", cmd); | |
85 | - cli->sendMsg("UNKNOWN_CMD"); | |
83 | + cli->sendMsg(500, "Command not recognized", false); | |
86 | 84 | return; |
87 | 85 | } |
@@ -2,6 +2,7 @@ | ||
2 | 2 | #include <errno.h> |
3 | 3 | #include <sys/types.h> |
4 | 4 | #include <pthread.h> |
5 | +#include <string.h> | |
5 | 6 | |
6 | 7 | #define LOG_TAG "SocketClient" |
7 | 8 | #include <cutils/log.h> |
@@ -13,29 +14,55 @@ SocketClient::SocketClient(int socket) { | ||
13 | 14 | pthread_mutex_init(&mWriteMutex, NULL); |
14 | 15 | } |
15 | 16 | |
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 | + | |
16 | 30 | int SocketClient::sendMsg(char *msg) { |
17 | 31 | LOGD("SocketClient::sendMsg(%s)", msg); |
32 | + | |
18 | 33 | if (mSocket < 0) { |
19 | 34 | errno = EHOSTUNREACH; |
20 | 35 | return -1; |
21 | 36 | } |
22 | 37 | |
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 | + | |
23 | 51 | 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; | |
26 | 65 | } |
27 | 66 | pthread_mutex_unlock(&mWriteMutex); |
28 | 67 | return 0; |
29 | 68 | } |
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 | - |
@@ -186,27 +186,26 @@ void SocketListener::runListener() { | ||
186 | 186 | } |
187 | 187 | } |
188 | 188 | |
189 | -void SocketListener::sendBroadcast(char *msg) { | |
189 | +void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) { | |
190 | 190 | pthread_mutex_lock(&mClientsLock); |
191 | 191 | SocketClientCollection::iterator i; |
192 | 192 | |
193 | 193 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
194 | - if ((*i)->sendMsg(msg)) { | |
194 | + if ((*i)->sendMsg(code, msg, addErrno)) { | |
195 | 195 | LOGW("Error sending broadcast (%s)", strerror(errno)); |
196 | 196 | } |
197 | 197 | } |
198 | 198 | pthread_mutex_unlock(&mClientsLock); |
199 | 199 | } |
200 | 200 | |
201 | -void SocketListener::sendBroadcast(char *msg, char *data) { | |
201 | +void SocketListener::sendBroadcast(char *msg) { | |
202 | 202 | pthread_mutex_lock(&mClientsLock); |
203 | 203 | SocketClientCollection::iterator i; |
204 | 204 | |
205 | 205 | for (i = mClients->begin(); i != mClients->end(); ++i) { |
206 | - if ((*i)->sendMsg(msg, data)) { | |
206 | + if ((*i)->sendMsg(msg)) { | |
207 | 207 | LOGW("Error sending broadcast (%s)", strerror(errno)); |
208 | 208 | } |
209 | 209 | } |
210 | 210 | pthread_mutex_unlock(&mClientsLock); |
211 | 211 | } |
212 | - |