SSDB network protocol development
SSDB’s network protocol is super simple!
Message
Packet := Block+ '\n'Block := Size '\n' Data '\n'Size := literal_integer Data := size_bytes_of_data
Request
Request := Cmd BlocksCmd := Block
Request commands include: get, set, del, ...
##ResponseResponse := Status BlockStatus := Block
Response status codes include: ok, not_found, error, fail, client_error
3get3keyYou will see something like Such a response:
2 ok 3 val
High-performance SSDB protocol parser
#include <stdlib.h>#include <string.h>int len = buffer->size();char ptr = buffer->data();while(len > 0){char data = (char *)memchr(ptr, '\n', len);if(data == NULL){break; } data += 1;int num = data - ptr;if(num == 1 || (num == 2 && ptr[0] == '\r')){// Packet received.return OK; }// Size receivedint size = (int)strtol(ptr, NULL, 10); len -= num + size; ptr += num + size;if(len >= 1 && ptr[0] = '\n'){ len -= 1; ptr += 1; }else if(len >= 2 && ptr[0] == '\r' && ptr[1] == '\n'){ len -= 2; ptr += 2; }else{break; }// Data received}
Suggestions for SDK developers: Data
can contain any characters, including \r, \n, \0. ..
, so youdon’tthink that these characters will not appear in Data
.