jamesge commented on a change in pull request #972: Redis server protocol URL: https://github.com/apache/incubator-brpc/pull/972#discussion_r359141036
########## File path: src/brpc/redis_reply.cpp ########## @@ -37,6 +38,61 @@ const char* RedisReplyTypeToString(RedisReplyType type) { } } +bool RedisReply::SerializeTo(butil::IOBuf* buf) { + butil::IOBufBuilder builder; + switch (_type) { + case REDIS_REPLY_ERROR: + // fall through + case REDIS_REPLY_STATUS: + buf->push_back((_type == REDIS_REPLY_ERROR)? '-' : '+'); + if (_length < sizeof(_data.short_str)) { + buf->append(_data.short_str, _length); + } else { + buf->append(_data.long_str, _length); + } + buf->append("\r\n"); + break; + case REDIS_REPLY_INTEGER: + builder << ':' << _data.integer << "\r\n"; + buf->append(builder.buf()); + break; + case REDIS_REPLY_STRING: + // Since _length is unsigned, we have to int casting _length to + // represent nil string + builder << '$' << (int)_length << "\r\n"; Review comment: 如果这么转的话,其实已经假定_length就是int了,那还不如把默认类型改了。_length用uint32的初衷应该是让可用长度多一些,但实践当中出现>2^31的长度的概率估计也不大。可以查看下所有出现_length的地方评估下是否可改成int。另外如果判断有效长度的代价不是很大,也可以做一下。 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org For additional commands, e-mail: dev-h...@brpc.apache.org