Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7bf4c45f authored by Selim Gurun's avatar Selim Gurun
Browse files

Add a utility function to send a code + binary msg

Add a utility function to SocketClient to send a c-string code
prepended to a binary message. This is necessary to be able to
send a binary message while keeping compatible with underlying
text-based protocol.

Change-Id: Ifc6562003a687577d7deb50260533a5147ae4f97
parent 78f6bcf8
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -45,6 +45,17 @@ public:
    int sendMsg(int code, const char *msg, bool addErrno);
    int sendMsg(int code, const char *msg, bool addErrno, bool useCmdNum);

    // Provides a mechanism to send a response code to the client. The message uses
    // the same format as in sendMsg method above.
    // Sends the code, a space, and a null character.
    int sendCode(int code);

    // Provides a mechanism to send binary data to client. The message uses the
    // same format as in sendMsg method above.
    // Sends the code, a space, and a null character, followed by 4 bytes of
    // big-endian length, and the data.
    int sendBinaryMsg(int code, const void *data, int len);

    // Sending binary data:
    int sendData(const void *data, int len);

@@ -59,6 +70,12 @@ private:
    // Send null-terminated C strings
    int sendMsg(const char *msg);
    void init(int socket, bool owned, bool useCmdNum);

    // Sending binary data. The caller should use make sure this is protected
    // from multiple threads entering simultaneously.
    // returns 0 if successful, -1 if there is a 0 byte write and -2 if any other
    // error occurred (use errno to get the error)
    int sendDataLocked(const void *data, int len);
};

typedef android::sysutils::List<SocketClient *> SocketClientCollection;
+39 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#include <sys/types.h>
#include <pthread.h>
#include <string.h>
#include <arpa/inet.h>

#define LOG_TAG "SocketClient"
#include <cutils/log.h>
@@ -78,6 +79,34 @@ int SocketClient::sendMsg(int code, const char *msg, bool addErrno, bool useCmdN
    return ret;
}


int SocketClient::sendBinaryMsg(int code, const void *data, int len) {

    /* 5 bytes for the code & space + 4 bytes for the len */
    char buf[9];
    /* Write the code */
    snprintf(buf, 5, "%.3d ", code);
    /* Write the len */
    uint32_t tmp = htonl(len);
    memcpy(buf + 5, &tmp, sizeof(uint32_t));

    pthread_mutex_lock(&mWriteMutex);
    int result = sendDataLocked(buf, sizeof(buf));
    if (result == 0 && len > 0) {
        result = sendDataLocked(data, len);
    }
    pthread_mutex_unlock(&mWriteMutex);

    return result;
}

// Sends the code (c-string null-terminated).
int SocketClient::sendCode(int code) {
    char buf[5];
    snprintf(buf, 5, "%.3d ", code);
    return sendData(buf, 5);
}

int SocketClient::sendMsg(const char *msg) {
    if (mSocket < 0) {
        errno = EHOSTUNREACH;
@@ -93,6 +122,15 @@ int SocketClient::sendMsg(const char *msg) {
}

int SocketClient::sendData(const void *data, int len) {

    pthread_mutex_lock(&mWriteMutex);
    int rc = sendDataLocked(data, len);
    pthread_mutex_unlock(&mWriteMutex);

    return rc;
}

int SocketClient::sendDataLocked(const void *data, int len) {
    int rc = 0;
    const char *p = (const char*) data;
    int brtw = len;
@@ -101,7 +139,6 @@ int SocketClient::sendData(const void* data, int len) {
        return 0;
    }

    pthread_mutex_lock(&mWriteMutex);
    while (brtw > 0) {
        rc = write(mSocket, p, brtw);
        if (rc > 0) {
@@ -113,7 +150,6 @@ int SocketClient::sendData(const void* data, int len) {
        if (rc < 0 && errno == EINTR)
            continue;

        pthread_mutex_unlock(&mWriteMutex);
        if (rc == 0) {
            SLOGW("0 length write :(");
            errno = EIO;
@@ -122,7 +158,6 @@ int SocketClient::sendData(const void* data, int len) {
        }
        return -1;
    }
    pthread_mutex_unlock(&mWriteMutex);
    return 0;
}