ASIO: sending QString (UTF-16)
Hello, I am learning ASIO, and I need to send a QString (from QT) thru async_write. The "buffer" function does not supports QString, so I wrote my own, is it correct? "utf16()" returns a pointer to a ushort (16 bit values). namespace boost { namespace asio { inline const_buffers_1 buffer(const QString &data) { return const_buffers_1(const_buffer(data.utf16(), data.size()*sizeof(ushort))); } } }; As an example, I am trying to send HTTP header and data, using a simple HTTP server sample. As the header needs to be ASCII, I am sending a vector, with a std::string and a QString, is the way it is done right? (The "message_" had a Japanese string for effective test that was removed to put on this email) ba::ip::tcp::socket socket_; std::vectorba::const_buffer lbuf; static std::string header_; static QString message_; std::string connection::header_="HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-16\r\n\r\n"; QString connection::message_=QString::fromUtf16(L"<html><head><title>test</title>" L"</head><body><h1>Test</h1><p>This is a test!</p>" L"</body></html>"); void handle_read(const boost::system::error_code& error, size_t bytes_transferred) { lbuf.clear(); lbuf.push_back(ba::buffer(header_)); lbuf.push_back(ba::buffer(message_)); ba::async_write(socket_, lbuf, boost::bind(&connection::handle_write, shared_from_this(), ba::placeholders::error, ba::placeholders::bytes_transferred)); }
The "buffer" function does not supports QString, so I wrote my own, is it correct? "utf16()" returns a pointer to a ushort (16 bit values).
It should be ok, if you don't modify (or destroy) the message_ until handle_write() is called. (BTW, probably it's better to put such functions in your own namespace, not in the ASIO one.)
participants (2)
-
Igor R
-
Rangel [LISTAS]