1#include <alloca.h>
2#include <errno.h>
3#include <sys/types.h>
4#include <pthread.h>
5
6#define LOG_TAG "FrameworkClient"
7#include <cutils/log.h>
8
9#include <sysutils/FrameworkClient.h>
10
11FrameworkClient::FrameworkClient(int socket) {
12    mSocket = socket;
13    pthread_mutex_init(&mWriteMutex, NULL);
14}
15
16int FrameworkClient::sendMsg(const char *msg) {
17    int ret;
18    if (mSocket < 0) {
19        errno = EHOSTUNREACH;
20        return -1;
21    }
22
23    pthread_mutex_lock(&mWriteMutex);
24    ret = TEMP_FAILURE_RETRY(write(mSocket, msg, strlen(msg) +1));
25    if (ret < 0) {
26        SLOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
27    }
28    pthread_mutex_unlock(&mWriteMutex);
29    return 0;
30}
31
32int FrameworkClient::sendMsg(const char *msg, const char *data) {
33    size_t bufflen = strlen(msg) + strlen(data) + 1;
34    char *buffer = (char *) alloca(bufflen);
35    if (!buffer) {
36        errno = -ENOMEM;
37        return -1;
38    }
39    snprintf(buffer, bufflen, "%s%s", msg, data);
40    return sendMsg(buffer);
41}
42
43