您的位置:首页 > 其它

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

2014-06-19 07:42 309 查看
[b]* Protocol(通信协议)[/b]

Protocol,是asio在网络编程方面最重要的一个concept。在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protocol来确定一些细节。

Protocol的约束摘要如下:

class protocol
{
public:
/// Obtain an identifier for the type of the protocol.
int type() const;

/// Obtain an identifier for the protocol.
int protocol() const;

/// Obtain an identifier for the protocol family.
int family() const;

typedef ... endpoint;
typedef ... socket;
};


符合Protocol约束的类需要提供type/protocol/family三个接口,分别返回协议类型/协议枚举/协议组枚举;还需要提供两个类型定义endpoint/socket,分别表示通信协议一方的地址/继承于asio::basic_socket的类型。

目前,asio中符合Protocol约束的类有:stream_protocol,datagram_protocol,raw_protocol,seq_packet_protocol;
既符合Protocol约束,同时又符合InternetProtocol约束的类有:tcp(TCP协议),udp(UDP协议),icmp(ICMP协议)。

[b]* InternetProtocol(网络通信协议)[/b]

InternetProtocol,是Protocol的约束超集,在Protocol约束的基础上添加了几个新的约束。

InternetProtocol的约束摘要如下:

class InternetProtocol
{
public:
/// Construct to represent the IPv4 internet protocol.
static InternetProtocol v4();

/// Construct to represent the IPv6 internet protocol.
static InternetProtocol v6();

/// Obtain an identifier for the type of the protocol.
int type() const;

/// Obtain an identifier for the protocol.
int protocol() const;

/// Obtain an identifier for the protocol family.
int family() const;

typedef ... endpoint;
typedef ... socket;
typedef ... resolver;
};


其中,type/protocol/family接口和endpoint/socket类型定义都是属于Protocol约束的部分,在此不再赘述。InternetProtocol相对于Protocol新增的约束有:v4/v6两个静态接口,分别返回IPv4/IPv6版本的网络通信协议对象;类型定义resolver,表示继承于basic_resolver的类型。

[b]* ConstBuffer(不可变缓冲区),ConstBufferSequence(不可变缓冲区序列),MutableBuffer(可变缓冲区),MutableBufferSequence(可变缓冲区序列)[/b]

ConstBuffer和MutableBuffer是asio中各种组件通用的缓冲区适配器concept,在asio中以const_buffer和mutable_buffer两个类实现。

ConstBuffer和MutableBuffer的约束摘要如下:

class ConstBuffer
{
private:
friend void const* boost::asio::detail::buffer_cast_helper(const ConstBuffer& b);
friend std::size_t boost::asio::detail::buffer_size_helper(const ConstBuffer& b);
};

class MutableBuffer
{
private:
friend void* boost::asio::detail::buffer_cast_helper(const MutableBuffer& b);
friend std::size_t boost::asio::detail::buffer_size_helper(const MutableBuffer& b);
};


只需能通过buffer_cast_helper和buffer_size_helper这两个自由函数获取缓冲区首地址指针和缓冲区长度即可。这两个concept没有什么扩展的必要,因此asio中并未显式地提及,在后文中我们直接以他们当前的实现const_buffer和mutable_buffer这两个类替代。

ConstBufferSequence和MutableBufferSequence是const_buffer和mutable_buffer的容器约束。它们的约束摘要如下:

class ConstBufferSequence
{
public:
typedef const_buffer value_type;
typedef ... const_iterator;

const_iterator begin() const;
const_iterator end() const;
};

class MutableBufferSequence
{
public:
typedef mutable_buffer value_type;
typedef ... const_iterator;

const_iterator begin() const;
const_iterator end() const;
};


ConstBufferSequence和MutableBufferSequence只需提供begin/end两个接口,返回相应的迭代器即可。

asio中,提供了const_buffer_1和mutable_buffer_1两个类,可以方便地将单个的const_buffer和mutable_buffer封装为容器外观,使其符合ConstBufferSequence和MutableBufferSequence约束。

[b]* Stream(流),AsyncReadStream(支持异步读操作的流),AsyncWriteStream(支持异步写操作的流),SyncReadStream(支持同步写操作的流),SyncWriteStream(支持同步写操作的流) [/b]

Stream,就是大家耳熟能详的“流”。AsyncReadStream,AsyncWriteStream,SyncReadStream,SyncWriteStream四种concept是Stream的子集,在流的基础上添加一些接口。

Stream的约束摘要如下:

class Stream
{
public:
void close();
boost::system::error_code close(boost::system::error_code& ec);
};


Stream的约束非常简单,只需要两个用于关闭流的close接口。

AsyncReadStream的约束摘要如下:

class AsyncReadStream
{
public:
template <typename MutableBufferSequence, typename ReadHandler>
void async_read_some(const MutableBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(ReadHandler) handler);

void close();
boost::system::error_code close(boost::system::error_code& ec);
};


AsyncReadStream在Stream的基础上增加了一个异步读数据的接口async_read_some,第一个参数buffers是一个符合MutableBufferSequence约束的对象,第二个参数是异步操作的回调函数。

AsyncWriteStream的约束摘要如下:

class AsyncWriteStream
{
public:
template <typename ConstBufferSequence, typename WriteHandler>
void async_write_some(const ConstBufferSequence& buffers,
BOOST_ASIO_MOVE_ARG(WriteHandler) handler);

void close();
boost::system::error_code close(boost::system::error_code& ec);
};


AsyncWriteStream在Stream的基础上增加了一个异步写数据的接口async_write_some,第一个参数buffers是一个符合ConstBufferSequence约束的对象,第二个参数是异步操作的回调函数。

SyncReadStream的约束摘要如下:

class SyncReadStream
{
public:
template <typename MutableBufferSequence>
void read_some(const MutableBufferSequence& buffers);

template <typename MutableBufferSequence>
boost::system::error_code read_some(const MutableBufferSequence& buffers, boost::system::error_code& ec);

void close();
boost::system::error_code close(boost::system::error_code& ec);
};


SyncReadStream在Stream的基础上增加了一个异步读数据的接口read_some,第一个参数buffers是一个符合MutableBufferSequence约束的对象。

SyncWriteStream的约束摘要如下:

class SyncWriteStream
{
public:
template <typename ConstBufferSequence>
void write_some(const ConstBufferSequence& buffers);

template <typename ConstBufferSequence>
boost::system::error_code write_some(const ConstBufferSequence& buffers, boost::system::error_code& ec);

void close();
boost::system::error_code close(boost::system::error_code& ec);
};


SyncWriteStream在Stream的基础上增加了一个同步写数据的接口write_some,第一个参数buffers是一个符合ConstBufferSequence约束的对象。

由于本文会实时根据读者反馈的宝贵意见更新,为防其他读者看到过时的文章,因此本系列专题谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: