San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 1 | #include <alloca.h> |
| 2 | #include <errno.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <pthread.h> |
| 5 | |
| 6 | #define LOG_TAG "SocketClient" |
| 7 | #include <cutils/log.h> |
| 8 | |
| 9 | #include <sysutils/SocketClient.h> |
| 10 | |
| 11 | SocketClient::SocketClient(int socket) { |
| 12 | mSocket = socket; |
| 13 | pthread_mutex_init(&mWriteMutex, NULL); |
| 14 | } |
| 15 | |
| 16 | int SocketClient::sendMsg(char *msg) { |
| 17 | LOGD("SocketClient::sendMsg(%s)", msg); |
| 18 | if (mSocket < 0) { |
| 19 | errno = EHOSTUNREACH; |
| 20 | return -1; |
| 21 | } |
| 22 | |
| 23 | pthread_mutex_lock(&mWriteMutex); |
| 24 | if (write(mSocket, msg, strlen(msg) +1) < 0) { |
| 25 | LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno)); |
| 26 | } |
| 27 | pthread_mutex_unlock(&mWriteMutex); |
| 28 | return 0; |
| 29 | } |
| 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 | |