Change the socket to non-blocking mode. 68/139068/5
authorjusung son <jusung07.son@samsung.com>
Mon, 17 Jul 2017 06:53:23 +0000 (15:53 +0900)
committerjusung son <jusung07.son@samsung.com>
Wed, 19 Jul 2017 09:47:00 +0000 (18:47 +0900)
  - In blocking mode, the consumer is blocked when the buffer of the provider is full

  - Related patch
    [amd] https://review.tizen.org/gerrit/#/c/139090/

Change-Id: I5ddd5cfddfb5f1653e24a3c4e139b467e85f6078
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/data_control_internal.c
src/data_control_log.h [changed mode: 0644->0755]
src/data_control_provider.c

index 46a3c0b..2c78d78 100755 (executable)
@@ -94,6 +94,9 @@ static GHashTable *__checked_consumer_hash;
 
 static GDBusConnection *_gdbus_conn = NULL;
 
+/* 100 milliseconds */
+const struct timespec TRY_SLEEP_TIME = { 0, 100 * 1000 * 1000 };
+
 int __datacontrol_get_provider_id(void)
 {
        static int id = 0;
@@ -1296,15 +1299,16 @@ int _write_socket(int fd, void *buffer, unsigned int nbytes,
        *bytes_write = 0;
        while (left && (retry_cnt < MAX_RETRY)) {
                nb = write(fd, buffer, left);
-               _LOGI("_write_socket: ...from %d: nb %d left %d\n", fd, nb, left - nb);
+               _LOGD("from %d: nb %d left %d", fd, nb, left - nb);
 
                if (nb == -1) {
-                       if (errno == EINTR) {
-                               _LOGE("_write_socket: EINTR error continue ...");
+                       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
+                               _LOGE("%d error continue", errno);
                                retry_cnt++;
+                               nanosleep(&TRY_SLEEP_TIME, 0);
                                continue;
                        }
-                       _LOGE("_write_socket: ...error fd %d: errno %d\n", fd, errno);
+                       _LOGE("error fd %d: errno %d", fd, errno);
                        return DATA_CONTROL_ERROR_IO_ERROR;
                }
 
@@ -1314,6 +1318,11 @@ int _write_socket(int fd, void *buffer, unsigned int nbytes,
                retry_cnt = 0;
        }
 
+       if (left != 0) {
+               _LOGE("error fd %d: retry_cnt %d", fd, retry_cnt);
+               return DATA_CONTROL_ERROR_IO_ERROR;
+       }
+
        return DATA_CONTROL_ERROR_NONE;
 }
 
@@ -1323,24 +1332,22 @@ int _read_socket(int fd, char *buffer, unsigned int nbytes,
        unsigned int left = nbytes;
        gsize nb;
        int retry_cnt = 0;
-       const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
 
        *bytes_read = 0;
        while (left && (retry_cnt < MAX_RETRY)) {
                nb = read(fd, buffer, left);
-               _LOGI("_read_socket: ...from %d: nb %d left %d\n", fd, nb, left - nb);
+               _LOGD("from %d: nb %d left %d", fd, nb, left - nb);
                if (nb == 0) {
-                       _LOGE("_read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
+                       _LOGE("read EOF, socket closed %d: nb %d errno %d", fd, nb, errno);
                        return DATA_CONTROL_ERROR_IO_ERROR;
                } else if (nb == -1) {
-                       /* wrt(nodejs) could change socket to none-blocking socket :-( */
                        if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
-                               _LOGE("_read_socket: %d errno, sleep and retry ...", errno);
+                               _LOGE("%d errno, sleep and retry", errno);
                                retry_cnt++;
                                nanosleep(&TRY_SLEEP_TIME, 0);
                                continue;
                        }
-                       _LOGE("_read_socket: ...error fd %d: errno %d\n", fd, errno);
+                       _LOGE("error fd %d: errno %d", fd, errno);
                        return DATA_CONTROL_ERROR_IO_ERROR;
                }
 
@@ -1349,6 +1356,11 @@ int _read_socket(int fd, char *buffer, unsigned int nbytes,
                *bytes_read += nb;
                retry_cnt = 0;
        }
+
+       if (left != 0) {
+               _LOGE("error fd %d: retry_cnt %d", fd, retry_cnt);
+               return DATA_CONTROL_ERROR_IO_ERROR;
+       }
        return DATA_CONTROL_ERROR_NONE;
 }
 
old mode 100644 (file)
new mode 100755 (executable)
index 8da5358..df1a29e
 
 #define _LOGE(fmt, arg...) LOGE(fmt, ##arg)
 #define _LOGI(fmt, arg...) LOGI(fmt, ##arg)
+#define _LOGD(fmt, arg...) LOGD(fmt, ##arg)
 
 #define _SECURE_LOGE(fmt, arg...) SECURE_LOGE(fmt, ##arg)
 #define _SECURE_LOGI(fmt, arg...) SECURE_LOGI(fmt, ##arg)
+#define _SECURE_LOGD(fmt, arg...) SECURE_LOGD(fmt, ##arg)
 
 #endif /* __TIZEN_APPFW_DATA_CONTROL_LOG_H__ */
index 4c6dee4..f2ea2ae 100755 (executable)
@@ -268,9 +268,10 @@ static bundle *__get_bundle_data_from_fd(int fd)
        int len = 0;
        int ret;
        char *buf;
+       unsigned int bytes_read = 0;
 
-       ret = read(fd, &len, sizeof(int));
-       if (ret < sizeof(int)) {
+       ret = _read_socket(fd, (char *)&len, sizeof(int), &bytes_read);
+       if (ret != DATA_CONTROL_ERROR_NONE) {
                _LOGE("read error :%d", ret);
                return NULL;
        }
@@ -282,8 +283,8 @@ static bundle *__get_bundle_data_from_fd(int fd)
                        _LOGE("calloc fail");
                        return NULL;
                }
-               ret = read(fd, buf, len);
-               if (ret < len) {
+               ret = _read_socket(fd, buf, len, &bytes_read);
+               if (ret != DATA_CONTROL_ERROR_NONE) {
                        _LOGE("read error :%d", ret);
                        if (buf)
                                free(buf);
@@ -307,10 +308,12 @@ static data_control_bulk_data_h __get_bulk_data_from_fd(int fd)
        int ret;
        int i;
        bundle *data = NULL;
+       unsigned int bytes_read = 0;
 
        data_control_bulk_data_create(&ret_bulk_data_h);
-       ret = read(fd, &size, sizeof(int));
-       if (ret < sizeof(int)) {
+
+       ret = _read_socket(fd, (char *)&size, sizeof(int), &bytes_read);
+       if (ret != DATA_CONTROL_ERROR_NONE) {
                _LOGE("read error :%d", ret);
                data_control_bulk_data_destroy(ret_bulk_data_h);
                return NULL;