本文整理汇总了C++中boost::asio::ip::tcp::socket::async_read_some方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::async_read_some方法的具体用法?C++ socket::async_read_some怎么用?C++ socket::async_read_some使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::ip::tcp::socket
的用法示例。
在下文中一共展示了socket::async_read_some方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: start
inline void connection::start() {
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
示例2: handleRead
/**
* Вызывается всякий раз, когда данные получены.
*/
static void handleRead(
ba::ip::tcp::socket& readFrom,
ba::ip::tcp::socket& writeTo,
char* readBuffer,
size_t bytes,
const boost::system::error_code& e
) {
#ifdef _DEBUG
const std::string data( readBuffer, readBuffer + bytes );
std::cout << data << std::endl;
#endif
// отправляем полученные данные "другой стороне"
writeTo.send( ba::buffer( readBuffer, bytes ) );
// читаем ещё данные с "этой стороны"
readFrom.async_read_some(
ba::buffer( readBuffer, 1024 ),
boost::bind(
handleRead,
boost::ref( readFrom ),
boost::ref( writeTo),
readBuffer,
ba::placeholders::bytes_transferred,
ba::placeholders::error
) );
}
示例3: async_read
void handler::async_read() {
auto self(shared_from_this());
m_socket.async_read_some(boost::asio::buffer(m_buffer),
std::bind(&handler::handle_read, self,
std::placeholders::_1,
std::placeholders::_2));
}
示例4: start
void start()
{
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&session::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
示例5:
void startRead()
{
checkNotReading(true);
reading = true;
readBuff.ensure_write_space(MAX_READ_BYTES_ONCE);
socket.async_read_some(boost::asio::buffer(readBuff.wt_ptr(), readBuff.writeable_bytes()),
boost::bind(&TcpConnection::handleRead, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
示例6: run_socket_io
void run_socket_io(boost::asio::ip::tcp::socket& socket)
{
std::array<char, 1024> read_buffer;
std::array<char, 1024> write_buffer;
socket.async_read_some(boost::asio::buffer(write_buffer, 1024),
[&write_buffer](const boost::system::error_code& ec, std::size_t length)
{
if (!ec)
std::cout.write(write_buffer.data(), length);
});
while (std::cin.getline(read_buffer.data(), 1024))
{
socket.send(
boost::asio::buffer(read_buffer, std::strlen(read_buffer.data())));
}
}
示例7: AsyncLoopSocket
void AsyncLoopSocket(boost::function<TaskState()> doWork, boost::asio::ip::tcp::socket& sock, bool isRead)
{
if (sock.get_io_service().stopped())
{
return;
}
TaskState st = doWork();
if (st == TASK_WORKING)
{
if (isRead)
{
sock.async_read_some(boost::asio::null_buffers(), bind(AsyncLoopSocket, doWork, boost::ref(sock),
isRead));
}
else
{
sock.async_write_some(boost::asio::null_buffers(), bind(AsyncLoopSocket, doWork, boost::ref(sock),
isRead));
}
return;
}
// Work is over. stop any outstanding events.
// NOTE: this isn't 100% reliable, and there may be events that linger in the queue.
// The next time we reset() and run() the io_service, these events will be processed,
// and io_service::stopped() will return false, because we've just done reset() and run().
// The state management (SSHSession::State) is our first step in controlling this, and we
// may need to implement our own cancel mechanism
//
// this io_service feature poses an additional problem - by spreading the implementation
// responsibility across multiple classes, we create scenarios where io_service's queue
// may contain outstanding events referring to objects that were, in the meantime, destroyed.
// this is why we're favouring, for now, the use of AsyncLoopTimer, where we can use an
// io_service for each timer, and destroy it right after being used. this makes sure we
// process no "zombie" outstanding events.
sock.get_io_service().stop();
}
示例8: handle_read
inline void connection::handle_read(const boost::system::error_code& e,
std::size_t bytes_transferred) {
if (!e) {
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
*request_, buffer_.data(), buffer_.data() + bytes_transferred);
if (result) {
try {
request_handler_.handle_request(*request_, reply_);
} catch (webserver::reply rep) {
reply_ = rep;
}
std::cerr << reply_.content << std::endl;
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
} else if (!result) {
reply_ = reply::stock_reply(reply::bad_request);
boost::asio::async_write(socket_, reply_.to_buffers(),
strand_.wrap(
boost::bind(&connection::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
} else {
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
}
// If an error occurs then no new asynchronous operations are started. This
// means that all shared_ptr references to the connection object will
// disappear and the object will be destroyed automatically after this
// handler returns. The connection class's destructor closes the socket.
}
示例9: async_read
void async_read() {
socket_.async_read_some(boost::asio::buffer(buffer_read_.bytes, MAX_BUFFER_SIZE),
boost::bind( &session::handle_read, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
示例10: recv
size_t recv(Func handler,
ARGS&...args)
{
return s_opt.async_read_some(boost::asio::buffer(args...), handler);
}