Write the specified payload to 'fd'. If writing the whole payload will be * done within 'timeout' milliseconds the operation succeeds and 'size' is * returned. Otherwise the operation fails, -1 is returned, and an unspecified * partial write could be performed against the file descriptor. */
| 47 | * returned. Otherwise the operation fails, -1 is returned, and an unspecified |
| 48 | * partial write could be performed against the file descriptor. */ |
| 49 | ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout) { |
| 50 | ssize_t nwritten, ret = size; |
| 51 | long long start = mstime(); |
| 52 | long long remaining = timeout; |
| 53 | |
| 54 | while(1) { |
| 55 | long long wait = (remaining > SYNCIO__RESOLUTION) ? |
| 56 | remaining : SYNCIO__RESOLUTION; |
| 57 | long long elapsed; |
| 58 | |
| 59 | /* Optimistically try to write before checking if the file descriptor |
| 60 | * is actually writable. At worst we get EAGAIN. */ |
| 61 | nwritten = write(fd,ptr,size); |
| 62 | if (nwritten == -1) { |
| 63 | if (errno != EAGAIN) return -1; |
| 64 | } else { |
| 65 | ptr += nwritten; |
| 66 | size -= nwritten; |
| 67 | } |
| 68 | if (size == 0) return ret; |
| 69 | |
| 70 | /* Wait */ |
| 71 | aeWait(fd,AE_WRITABLE,wait); |
| 72 | elapsed = mstime() - start; |
| 73 | if (elapsed >= timeout) { |
| 74 | errno = ETIMEDOUT; |
| 75 | return -1; |
| 76 | } |
| 77 | remaining = timeout - elapsed; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* Read the specified amount of bytes from 'fd'. If all the bytes are read |
| 82 | * within 'timeout' milliseconds the operation succeed and 'size' is returned. |
no test coverage detected