Asio是C++的网络库,有boost和非boost这两种版本,这里涉及的都是非boost的版本。Asio官方文档

在使用Asio时可以只包含头文件asio.hpp,如果知道所用接口具体在哪个头文件中定义,也可以直接包含该头文件。

buffer

头文件asio/buffer.hpp

asio::buffer的接口很多,下面这些都是:

mutable_buffers_1 buffer(const mutable_buffer & b);
mutable_buffers_1 buffer(const mutable_buffer & b, std::size_t max_size_in_bytes);
const_buffers_1 buffer(const const_buffer & b);
const_buffers_1 buffer(const const_buffer & b, std::size_t max_size_in_bytes);
mutable_buffers_1 buffer(void * data, std::size_t size_in_bytes);
const_buffers_1 buffer(const void * data, std::size_t size_in_bytes);
// 模板
template< typename PodType, std::size_t N>
mutable_buffers_1 buffer( PodType (&data)[N]); template<typename PodType, std::size_t N>
mutable_buffers_1 buffer(PodType (&data)[N], std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const PodType (&data)[N]); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const PodType (&data)[N], std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
mutable_buffers_1 buffer(boost::array< PodType, N > & data); template<typename PodType, std::size_t N>
mutable_buffers_1 buffer(boost::array< PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
const_buffers_1 buffer(boost::array< const PodType, N > & data); template<typename PodType, std::size_t N>
const_buffers_1 buffer(boost::array< const PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const boost::array< PodType, N > & data); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const boost::array< PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
mutable_buffers_1 buffer(std::array< PodType, N > & data); template<typename PodType, std::size_t N>
mutable_buffers_1 buffer(std::array< PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
const_buffers_1 buffer(std::array< const PodType, N > & data); template<typename PodType, std::size_t N>
const_buffers_1 buffer(std::array< const PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const std::array< PodType, N > & data); template<typename PodType, std::size_t N>
const_buffers_1 buffer(const std::array< PodType, N > & data, std::size_t max_size_in_bytes); template<typename PodType, typename Allocator>
mutable_buffers_1 buffer(std::vector< PodType, Allocator > & data); template<typename PodType, typename Allocator>
mutable_buffers_1 buffer(std::vector< PodType, Allocator > & data, std::size_t max_size_in_bytes); template<typename PodType, typename Allocator>
const_buffers_1 buffer(const std::vector< PodType, Allocator > & data); template<typename PodType, typename Allocator>
const_buffers_1 buffer(const std::vector< PodType, Allocator > & data, std::size_t max_size_in_bytes); template<typename Elem, typename Traits, typename Allocator>
const_buffers_1 buffer(const std::basic_string< Elem, Traits, Allocator > & data); template<typename Elem, typename Traits, typename Allocator>
const_buffers_1 buffer(const std::basic_string< Elem, Traits, Allocator > & data, std::size_t max_size_in_bytes);

一般常用的是这几个:

mutable_buffers_1 buffer(void * data, std::size_t size_in_bytes);

一般用带有指定大小的比较方便,这样可以重复利用buffer,不需要去整一个恰好大小的buffer。

官方的例子:

char d1[128];
size_t bytes_transferred = sock.receive(asio::buffer(d1)); std::vector<char> d2(128);
bytes_transferred = sock.receive(asio::buffer(d2)); std::array<char, 128> d3;
bytes_transferred = sock.receive(asio::buffer(d3)); boost::array<char, 128> d4;
bytes_transferred = sock.receive(asio::buffer(d4));

注意,如果用的是STL容器,如vector,虽然它是可以动态调整大小的,但是buffer不会去调整它,所以传进去的vector的size决定了这个buffer所能容纳的数据量,而不是capacity决定的。buffer的其他接口还有buffer_size获取大小、buffer_cast类型转换、buffer_copy拷贝,这里不谈了。

write

写的接口比较少,只有如下四个:

template<typename AsyncWriteStream, typename ConstBufferSequence, typename WriteHandler>
void-or-deduced async_write(AsyncWriteStream & s, const ConstBufferSequence & buffers, WriteHandler handler); template<typename AsyncWriteStream, typename ConstBufferSequence, typename CompletionCondition, typename WriteHandler>
void-or-deduced async_write(AsyncWriteStream & s, const ConstBufferSequence & buffers, CompletionCondition completion_condition, WriteHandler handler); template<typename AsyncWriteStream, typename Allocator, typename WriteHandler>
void-or-deduced async_write(AsyncWriteStream & s, basic_streambuf< Allocator > & b, WriteHandler handler); template<typename AsyncWriteStream, typename Allocator, typename CompletionCondition, typename WriteHandler>
void-or-deduced async_write(AsyncWriteStream & s, basic_streambuf< Allocator > & b, CompletionCondition completion_condition, WriteHandler handler);

一般可以这样用:

char data[size] = ...;
asio::async_write(sock, asio::buffer(data, size), handler);

Asio基本接口的更多相关文章

  1. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  2. Boost.Asio技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  3. (原创)如何使用boost.asio写一个简单的通信程序(一)

    boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...

  4. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  5. 网络库crash以及boost asio strand dispath分析

    最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...

  6. boost asio tcp server 拆分

    从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...

  7. boost.asio源码剖析(四) ---- asio中的泛型概念(concepts)

    * Protocol(通信协议) Protocol,是asio在网络编程方面最重要的一个concept.在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protoc ...

  8. boost.asio源码剖析(五) ---- 泛型与面向对象的完美结合

    有人说C++是带类的C:有人说C++是面向对象编程语言:有人说C++是面向过程与面向对象结合的语言.类似的评论网上有很多,虽然正确,却片面,是断章取义之言. C++是实践的产物,C++并没有为了成为某 ...

  9. boost.asio源码剖析(二) ---- 架构浅析

    * 架构浅析 先来看一下asio的0层的组件图.                     (图1.0) io_object是I/O对象的集合,其中包含大家所熟悉的socket.deadline_tim ...

随机推荐

  1. HDU6470 ()矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:f[n] = f[n-1] + f[n-2]*2 + n^3; f[1] =1 ; f[2] = 2 ...

  2. c++ 编程调试秘笈

    美.Vladimir Kushnir . O'REILLY. 人邮 .2013.1 c++大部分缺陷来源于c MyClass* object = new MyClass(); delete objec ...

  3. 【研究】缓慢的http拒绝服务攻击

    1 详细描述: 缓慢的http拒绝服务攻击是一种专门针对于Web的应用层拒绝服务攻击,攻击者操纵网络上的肉鸡,对目标Web服务器进行海量http request攻击,直到服务器带宽被打满,造成了拒绝服 ...

  4. Two Sum [easy] (Python)

    由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值:第二遍扫描原数组,对于每个数nums[i]查看 ...

  5. docker jenkins安装(一)

    https://hub.docker.com/r/jenkins/jenkins  jenkins的docker官方镜像地址 https://jenkins.io/  jenkins官方网站 环境: ...

  6. Python+Selenium定位元素的方法

    Python+Selenium有以下八种定位元素的方法: 1. find_element_by_id() eg: find_element_by_id("kw") 2. find_ ...

  7. rancher 2.X 搭建小型web集群+mysql主从复制

    一,环境配置    rancher 2.1.6 二,配置harbor私有仓库 见上文 三,配置私有镜像 01,总文件 dockerfile 为主配置文件,html 为站点文件wordpress.,官网 ...

  8. jQuery.form开发手记

      介绍使用API说明ajaxFormajaxSubmitfieldSerializeresetFormclearFormclearFields参数示例 一般情况下其实提交表单我们将数据$(" ...

  9. JSON转C#实体类

    https://www.bejson.com/convert/json2csharp/

  10. 机器学习——XGBoost

    基础概念 XGBoost(eXtreme Gradient Boosting)是GradientBoosting算法的一个优化的版本,针对传统GBDT算法做了很多细节改进,包括损失函数.正则化.切分点 ...