我正在写一个使用Boost Asio的死亡简单玩具键值存储,并且发生了一些非常奇怪的事情。Boost Asio不完整写入插座
基于字符串的协议是这样的:
S <key> <value> // to set a key
G <key> // to get the key value
L // to list all key-value pairs
写入是同步的,使用
升压:: ASIO ::写(socket_,升压:: ASIO ::缓冲液( resp,len));
其中socket_是一个boost :: asio :: ip :: tcp :: socket - 与asynchrounous写故事不会改变,显然。
的问题是,有时它不写入套接字所有假设写,或输出以某种方式错位字节...错位列表输出的
实施例(在本地主机上,使用nc,echo和hexdump):
> echo S a 12 | nc localhost 5000
A
> echo S b 23 | nc localhost 5000
A
> echo L | nc localhost 5000 | hexdump -C
00000000 61 3a 20 31 32 3b 20 62 60 00 00 00 00 00 |a: 12; b`.....|
0000000e
> echo L | nc localhost 5000 | hexdump -C
00000000 61 3a 20 31 32 3b 20 62 3a 20 32 33 3b 20 |a: 12; b: 23; |
0000000e
我从Ubuntu 14.10版本库使用Boost 1.55。 遵循服务于客户端的函数的代码。
非常感谢您的任何提示!
void ClientSession::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
if (!error) {
std::string cmd(data_, bytes_transferred);
cmd = trim_str(cmd);
const char* resp = NULL;
int len = 0;
switch(cmd.at(0)) {
case SET: {
std::size_t k_pos = cmd.find(" ") + 1;
std::size_t v_pos = cmd.find(" ", k_pos+1) + 1;
std::string key = trim_str(cmd.substr(k_pos, v_pos-3));
std::string value = trim_str(cmd.substr(v_pos, cmd.length()-1));
cout << "SET key " << key << ", value " << value << "*" <<endl;
kvs->db[key] = std::atoi(value.c_str());
resp = "A";
len = 1;
break;
}
case GET: {
std::size_t k_pos = cmd.find(" ") + 1;
std::string key = trim_str(cmd.substr(k_pos, cmd.length()));
cout << "GET key " << key << "*" << endl;
int value = kvs->db[key];
char str[5];
sprintf(str, "%d", value);
resp = (const char*) str;
len = strlen(resp);
break;
}
case LIST: {
ostringstream os;
for (std::map<string, int>::iterator iter = kvs->db.begin();
iter != kvs->db.end(); ++iter)
os << iter->first << ": " << iter->second << "; ";
cout << "list: " << os.str().c_str() << endl;
resp = os.str().c_str();
len = strlen(resp);
break;
}
case DEL: {
std::size_t k_pos = cmd.find(" ") + 1;
std::string key = trim_str(cmd.substr(k_pos, cmd.length()));
kvs->db.erase(key);
resp = "A";
len = 1;
break;
}
default: {
resp = "NACK.";
len = 5;
}
}
cout << "resp: " << resp << "*" << endl;
cout << "len: " << len << "*" << endl;
std::size_t written = boost::asio::write(socket_,
boost::asio::buffer(resp, len));
cout << "written: " << written << endl;
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
socket_.close();
} else
delete this;
不够公平,谢谢! - 正如你可能已经猜到的那样,我还没有对那些C微妙的怪癖非常熟悉:/ –
我会说:永远不要习惯C的怪癖!使用C++代替:http://paste.ubuntu.com/10759474/(另外,'enable_shared_from_this'而不是'delete this' code smell;使用Boost'array_source'来减少复制,boost'string_ref'消除它,但这是优化) – sehe
现在通过编译器:[Live Live Coliru](http://coliru.stacked-crooked.com/a/3b5a928e311f3162)。 ** CAVEAT **将'KV_IOTHREADS_NUM'保留为1,直到您执行线程安全 – sehe