diff options
Diffstat (limited to 'libsysutils/src/SocketClient.cpp')
-rw-r--r-- | libsysutils/src/SocketClient.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp new file mode 100644 index 0000000..6db62b3 --- /dev/null +++ b/libsysutils/src/SocketClient.cpp @@ -0,0 +1,41 @@ +#include <alloca.h> +#include <errno.h> +#include <sys/types.h> +#include <pthread.h> + +#define LOG_TAG "SocketClient" +#include <cutils/log.h> + +#include <sysutils/SocketClient.h> + +SocketClient::SocketClient(int socket) { + mSocket = socket; + pthread_mutex_init(&mWriteMutex, NULL); +} + +int SocketClient::sendMsg(char *msg) { + LOGD("SocketClient::sendMsg(%s)", msg); + if (mSocket < 0) { + errno = EHOSTUNREACH; + return -1; + } + + pthread_mutex_lock(&mWriteMutex); + if (write(mSocket, msg, strlen(msg) +1) < 0) { + LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno)); + } + pthread_mutex_unlock(&mWriteMutex); + return 0; +} + +int SocketClient::sendMsg(char *msg, char *data) { + char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1); + if (!buffer) { + errno = -ENOMEM; + return -1; + } + strcpy(buffer, msg); + strcat(buffer, data); + return sendMsg(buffer); +} + |