blob: 6db62b3093b832b5c0b8e1ce9fdb1b29d5d179ba [file] [log] [blame]
San Mehatfa644ff2009-05-08 11:15:53 -07001#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
11SocketClient::SocketClient(int socket) {
12 mSocket = socket;
13 pthread_mutex_init(&mWriteMutex, NULL);
14}
15
16int 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
31int 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