注: 本文档主要根据原作者的英文文档protocol.en.txt写成,做了一些翻译工作和添加了一些例子以及一些需要注意的地方。如果本文档对你有所帮助,欢迎关注我的新浪微博:http://weibo.com/u/1857063732 如果有建议,欢迎发送到我的邮箱xiaoxuye1988@163.com 目前就职于 欢聚时代(YY), 从事于后台开发工作。
Request and Response
英文原文如下
Requestand Response
-The HandlerSocket protocol is a simple request/response protocol. After a
connection is established, the client sidesends a request, and then the
server side sends a response.
-A request/response consists of a single line.
-Requests can be pipelined; That is, you can send multiple requests (ie.
lines) at onetime, and receive responses for them at one time.
-Handlersocket协议是一个简单的请求/回应协议。在一个连接建立之后,客户端发出 一个请求,让后服务端回应一个应答。
-一个请求/回应 组成了一个单独的行
-请求可以是基于管道的。也就是说,你同一时间可以发送多个请求,并且同一时间收到它们所对应的回应。
Responsesyntax
HandlerSocketreturns a response of the following syntax for each request.
<errorcode> <numcolumns><r1> ... <rn>
-<errorcode> indicates whether the request has successfully executed ornot.
'0' means success. Non-zero means an error.
-<numcolumns> indicates the number of columns of the result set.
-<r1> ... <rn> is the result set. The length of <r1> ...<rn> is always a
multiple of <numcolumns>. It ispossible that <r1> ... <rn> is empty.
If<errorcode> is non-zero, <numcolumns> is always 1 and <r1>indicates a
human-readable error message, though sometimes<r1> is not provided.
回应的语法:
HandlerSocket 对于每个请求都返回以下形式的返回值:
<errorcode><numcolumns> <r1> ... <rn>
- <errorcode>表示这个请求是否成功的执行了,‘0’表示成功,非0表示错误。
- <numcolumns> 表示返回结果中的列的个数。
-<r1> ... <rn>是返回的结果。<r1>... <rn> 常常是多组,也有可能是空的。
发送请求和收到回应处理的代码基本形式如下,
do {//发送request_buf_exec_generic
//发送请求
if (cli->request_send() != 0) {
fprintf(stderr, "request_send:%s\n", cli->get_error().c_str());
break;
}
//收到回应并查看是否请求成功执行
if ((code = cli->response_recv(numflds)) !=0) {
fprintf(stderr,"response_recv: %s\n", cli->get_error().c_str());
break;
}
//输出结果
while (true) {
conststring_ref * const row = cli->get_next_row();
if(row == 0) {
break;
}
for(size_t i = 0; i < numflds; ++i) {
conststring val(row[i].begin(), row[i].size());
printf(" %s",val.c_str());
}
printf("\n");
}
} while (false);