]>
git.saurik.com Git - redis.git/blob - 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.
8 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
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.
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.
38 int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
) {
39 ssize_t nwritten
, ret
= size
;
40 time_t start
= time(NULL
);
44 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
45 nwritten
= write(fd
,ptr
,size
);
46 if (nwritten
== -1) return -1;
50 if ((time(NULL
)-start
) > timeout
) {
58 int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
) {
59 ssize_t nread
, totread
= 0;
60 time_t start
= time(NULL
);
64 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
65 nread
= read(fd
,ptr
,size
);
66 if (nread
<= 0) return -1;
71 if ((time(NULL
)-start
) > timeout
) {
79 int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
86 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
89 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';