示例#1
0
static int cliReadBulkReply(int fd, int multibulk) {
    sds replylen = cliReadLine(fd);
    char *reply, crlf[2];
    int bulklen, error = 0;

    if (replylen == NULL) return 1;
    if (strcmp(replylen,"nil") == 0) {
        sdsfree(replylen);
        printf("(nil)\n");
        return 0;
    }
    bulklen = atoi(replylen);
    if (multibulk && bulklen == -1) {
        sdsfree(replylen);
        printf("(nil)");
        return 0;
    }
    if (bulklen < 0) {
        bulklen = -bulklen;
        error = 1;
    }
    reply = zmalloc(bulklen);
    anetRead(fd,reply,bulklen);
    anetRead(fd,crlf,2);
    if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
        zfree(reply);
        return 1;
    }
    if (!multibulk && isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
        printf("\n");
    zfree(reply);
    return error;
}
示例#2
0
文件: redis-cli.c 项目: ngmoco/redis
static int cliReadBulkReply(int fd) {
    sds replylen = cliReadLine(fd);
    char *reply, crlf[2];
    int bulklen;

    if (replylen == NULL) return 1;
    bulklen = atoi(replylen);
    if (bulklen == -1) {
        sdsfree(replylen);
        printf("(nil)\n");
        return 0;
    }
    reply = zmalloc(bulklen);
    anetRead(fd,reply,bulklen);
    anetRead(fd,crlf,2);
    if (config.raw_output || !isatty(fileno(stdout))) {
        if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
            zfree(reply);
            return 1;
        }
    } else {
        /* If you are producing output for the standard output we want
         * a more interesting output with quoted characters and so forth */
        printStringRepr(reply,bulklen);
    }
    zfree(reply);
    return 0;
}
示例#3
0
文件: server.c 项目: wudikua/lookup
int serverRead(event *ev, int fd) {
	lookup_log(LOG_DEBUG, "read\n");
	int read = 0;
	char buf[IO_PACKET_SIZE];

	if (ev->wr_pos == 0) {
		read = anetRead(fd, &buf[0], PROTOCOL_HAEDER_SIZE);
		if (read == 0 && errno == ECONNRESET) {
free_conn:
			ev->wr_pos = 0;
			del_event(pool, fd);	
			close(fd);
			zfree(ev->data);
			lookup_log(LOG_DEBUG, "close fd\n");
			return 0;
		}
		ev->data = parse_protocol_header(buf);
	} else {
		int block = (ev->data->body_len + PROTOCOL_HAEDER_SIZE - ev->wr_pos) 
			/ IO_PACKET_SIZE;
		if (block > 0) {
			read = anetRead(fd, &buf[0], IO_PACKET_SIZE);			
		} else {
			read = anetRead(fd, &buf[0], 
				(ev->data->body_len + PROTOCOL_HAEDER_SIZE - ev->wr_pos) % IO_PACKET_SIZE);			
		}
		if (read == 0 && errno == ECONNRESET) {
			goto free_conn;
		}
		if (ev->body_data == NULL) {
			ev->body_data_size = IO_PACKET_SIZE * 2;
			ev->body_data = (char *)zmalloc(ev->body_data_size);
			memcpy(ev->body_data + ev->body_data_wr_pos, buf, read);
			ev->body_data_wr_pos += read;
		} else {
			if ((ev->body_data_wr_pos + read) > ev->body_data_size) {
				//resize
				ev->body_data_size = ev->body_data_size * 2;
				ev->body_data = (char *)zrealloc(ev->body_data, 
					ev->body_data_size);
			}
			memcpy(ev->body_data + ev->body_data_wr_pos, buf, read);
			ev->body_data_wr_pos += read;
		}
	}
	if (read > 0) {
		ev->wr_pos += read;	
	}
	lookup_log(LOG_DEBUG, "pos:%d\n", ev->wr_pos);
	if (ev->wr_pos >= (ev->data->body_len +  PROTOCOL_HAEDER_SIZE)) {
		parse_protocol_body(ev->body_data, ev->data);
		ev->wr_pos = 0;
		processRequest((lookup_protocol *)ev->data);
		ev->cb = serverWrite;
		set_event(pool, fd, EPOLLOUT);
	}
	return 0;
}
示例#4
0
文件: redis-cli.c 项目: ngmoco/redis
static int cliReadReply(int fd) {
    char type;

    if (anetRead(fd,&type,1) <= 0) {
        if (config.shutdown) return 0;
        exit(1);
    }
    switch(type) {
    case '-':
        printf("(error) ");
        cliReadSingleLineReply(fd,0);
        return 1;
    case '+':
        return cliReadSingleLineReply(fd,0);
    case ':':
        printf("(integer) ");
        return cliReadSingleLineReply(fd,0);
    case '$':
        return cliReadBulkReply(fd);
    case '*':
        return cliReadMultiBulkReply(fd);
    default:
        printf("protocol error, got '%c' as reply type byte\n", type);
        return 1;
    }
}
示例#5
0
static int cliReadReply(int fd) {
    char type;
    int nread;

    if ((nread = anetRead(fd,&type,1)) <= 0) {
        if (config.shutdown) return 0;
        if (config.interactive &&
            (nread == 0 || (nread == -1 && errno == ECONNRESET)))
        {
            return ECONNRESET;
        } else {
            printf("I/O error while reading from socket: %s",strerror(errno));
            exit(1);
        }
    }
    switch(type) {
    case '-':
        if (config.tty) printf("(error) ");
        cliReadSingleLineReply(fd,0);
        return 1;
    case '+':
        return cliReadSingleLineReply(fd,0);
    case ':':
        if (config.tty) printf("(integer) ");
        return cliReadSingleLineReply(fd,0);
    case '$':
        return cliReadBulkReply(fd);
    case '*':
        return cliReadMultiBulkReply(fd);
    default:
        printf("protocol error, got '%c' as reply type byte", type);
        return 1;
    }
}
示例#6
0
static redisReply *redisReadBulkReply(int fd) {
    sds replylen = redisReadLine(fd);
    sds buf;
    char crlf[2];
    int bulklen;

    if (replylen == NULL) return redisIOError();
    bulklen = atoi(replylen);
    sdsfree(replylen);
    if (bulklen == -1)
        return createReplyObject(REDIS_REPLY_NIL,sdsempty());

    buf = sdsnewlen(NULL,bulklen);
    anetRead(fd,buf,bulklen);
    anetRead(fd,crlf,2);
    return createReplyObject(REDIS_REPLY_STRING,buf);
}
示例#7
0
static int cliReadBulkReply(int fd) {
    sds replylen = cliReadLine(fd);
    char *reply, crlf[2];
    int bulklen;

    if (replylen == NULL) return 1;
    bulklen = atoi(replylen);
    if (bulklen == -1) {
        sdsfree(replylen);
        printf("(nil)\n");
        return 0;
    }
    reply = zmalloc(bulklen);
    anetRead(fd,reply,bulklen);
    anetRead(fd,crlf,2);
    if (bulklen && fwrite(reply,bulklen,1,stdout) == 0) {
        zfree(reply);
        return 1;
    }
    if (isatty(fileno(stdout)) && reply[bulklen-1] != '\n')
        printf("\n");
    zfree(reply);
    return 0;
}
示例#8
0
文件: redis-cli.c 项目: ngmoco/redis
static int selectDb(int fd) {
    int retval;
    sds cmd;
    char type;

    if (config.dbnum == 0)
        return 0;

    cmd = sdsempty();
    cmd = sdscatprintf(cmd,"SELECT %d\r\n",config.dbnum);
    anetWrite(fd,cmd,sdslen(cmd));
    anetRead(fd,&type,1);
    if (type <= 0 || type != '+') return 1;
    retval = cliReadSingleLineReply(fd,1);
    if (retval) {
        return retval;
    }
    return 0;
}
示例#9
0
static redisReply *redisReadReply(int fd) {
    char type;

    if (anetRead(fd,&type,1) <= 0) return redisIOError();
    switch(type) {
    case '-':
        return redisReadSingleLineReply(fd,REDIS_REPLY_ERROR);
    case '+':
        return redisReadSingleLineReply(fd,REDIS_REPLY_STRING);
    case ':':
        return redisReadIntegerReply(fd);
    case '$':
        return redisReadBulkReply(fd);
    case '*':
        return redisReadMultiBulkReply(fd);
    default:
        printf("protocol error, got '%c' as reply type byte\n", type);
        exit(1);
    }
}