]> git.saurik.com Git - redis.git/blob - src/syncio.c
synchronous I/O networking functions originally used just for replication refactored...
[redis.git] / src / syncio.c
1 /* Synchronous socket I/O operations, with timeouts.
2 * Redis performs most of the I/O in a nonblocking way, with the exception
3 * of the SYNC command where the slave does it in a blocking way, and
4 * the MIGRATE command that must be blocking in order to be atomic from the
5 * point of view of the two instances (one migrating the key and one receiving
6 * the key). This is why need the following blocking I/O functions.
7 *
8 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the name of Redis nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include "redis.h"
37
38 int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
39 ssize_t nwritten, ret = size;
40 time_t start = time(NULL);
41
42 timeout++;
43 while(size) {
44 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
45 nwritten = write(fd,ptr,size);
46 if (nwritten == -1) return -1;
47 ptr += nwritten;
48 size -= nwritten;
49 }
50 if ((time(NULL)-start) > timeout) {
51 errno = ETIMEDOUT;
52 return -1;
53 }
54 }
55 return ret;
56 }
57
58 int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
59 ssize_t nread, totread = 0;
60 time_t start = time(NULL);
61
62 timeout++;
63 while(size) {
64 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
65 nread = read(fd,ptr,size);
66 if (nread <= 0) return -1;
67 ptr += nread;
68 size -= nread;
69 totread += nread;
70 }
71 if ((time(NULL)-start) > timeout) {
72 errno = ETIMEDOUT;
73 return -1;
74 }
75 }
76 return totread;
77 }
78
79 int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
80 ssize_t nread = 0;
81
82 size--;
83 while(size) {
84 char c;
85
86 if (syncRead(fd,&c,1,timeout) == -1) return -1;
87 if (c == '\n') {
88 *ptr = '\0';
89 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
90 return nread;
91 } else {
92 *ptr++ = c;
93 *ptr = '\0';
94 nread++;
95 }
96 }
97 return nread;
98 }