OpenRTMFP/Cumulus Primer(10)IO 管理之流缓冲区

OpenRTMFP/Cumulus Primer(10)IO 管理之流缓冲区

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Blog: Blog.CSDN.net/Poechant
  • Date: April 24th, 2012

本文主要分析 MemoryStream.h 文件中定义的类。

1 了解 std::streambuf

首先要了解 streambuf 内置了一个 get 指针和一个 put 指针。streambuf 的所有操作基本都是对这两个指针的操作。其一些成员函数的缩写中的 g 和 p 就分别表示 get pointer 和 put pointer。

1.1 单步移动内置指针

  • Increase get pointer: Advances the get pointer by n positions. The get pointer is the internal pointer that points to the next location in the controlled input sequence.

      void gbump ( int n );
    
  • Increase put pointer: Advances the put pointer by n positions. The put pointer is the internal pointer that points to the next location of the controlled output sequence.

      void pbump ( int n );
    

1.2 获取 get 指针和 put 指针的位置

  • Pointer to current position of input sequence: Returns a reference to the current element of the controlled input sequence (i.e., the “get pointer”).

      char * gptr ( ) const;
    
  • Pointer to current position of output sequence: Returns a reference to the current element of the output sequence (the put pointer).

      char * pptr ( ) const;
    

1.3 设置 get 和 put 指针可达区域的上下界

  • Set input sequence pointers: Sets values for the pointers that define both the boundaries of the accessible part of the controlled input sequence and the get pointer itself.

      void setg ( char* gbeg, char* gnext, char* gend );
    
  • gbeg: New value for the pointer to the beginning of the accessible part of the controlled input sequence.
  • gnext: New value for the get pointer, which points to the next element within the controlled input sequence where the next input operation shall be performed.
  • gend: New value for the end pointer, just past the end of the accessible part of the controlled input sequence.
  • Set output sequence pointers: Sets the values that define the boundaries of the accessible part of the controlled output sequence.

      void setp ( char* pbeg, char* pend );
    
  • pbeg: New value for the pointer to the beginning of the accessible part of the controlled output sequenceand put pointer.
  • pend: New value for the end pointer, just past the end of the accessible part of the controlled output sequence.

2 MemoryStreamBuf

类定义:

class MemoryStreamBuf: public std::streambuf {
    friend class ScopedMemoryClip;
public:
    MemoryStreamBuf(char* pBuffer,Poco::UInt32 bufferSize);
    MemoryStreamBuf(MemoryStreamBuf&);
    ~MemoryStreamBuf();

    void            next(Poco::UInt32 size); // Explaint below
    Poco::UInt32    written(); // Explaint below
    void            written(Poco::UInt32 size);
    Poco::UInt32    size();  // Explaint below
    void            resize(Poco::UInt32 newSize); // Explaint below
    char*           begin(); // Explaint below
    void            position(Poco::UInt32 pos=0); // Explaint below
    char*           gCurrent(); // Explaint below
    char*           pCurrent(); // Explaint below

private:
    virtual int overflow(int_type c);
    virtual int underflow();
    virtual int sync();

    Poco::UInt32    _written;
    char*           _pBuffer;
    Poco::UInt32    _bufferSize;

    MemoryStreamBuf();
    MemoryStreamBuf& operator = (const MemoryStreamBuf&);
};

ScopedMemoryClip 是 MemoryStreamBuf 的友元,其内部有 MemoryStreamBuf 的成员,这里暂且不管。构造函数传入的参数是缓冲区的地址和缓冲区大小(字节数)。拷贝构造函数和析构函数不必赘述。

2.1 移动内置的 get 和 put 指针:

put 和 get 指针都移动:

void MemoryStreamBuf::next(UInt32 size) {
    pbump(size);
    gbump(size);
}

2.2 获取 get 和 put 指针当前位置:

封装 streambuf 的 gptr 和 pptr:

inline char* MemoryStreamBuf::gCurrent() {
    return gptr();
}
inline char* MemoryStreamBuf::pCurrent() {
    return pptr();
}

2.3 获取缓冲区的起始位置和大小:

依赖于内置成员变量 pBuffer 和 bufferSize:

inline char* MemoryStreamBuf::begin() {
    return _pBuffer;
}

inline Poco::UInt32 MemoryStreamBuf::size() {
    return _bufferSize;
}

2.4 缓冲区的已写字节数

读取(其中也可能发生设置操作):

UInt32 MemoryStreamBuf::written() {
    int written = pCurrent() - begin(); // 已写字节数
    if (written < 0)
        written = 0;
    if (written > _written) // 保存已写字节数
        _written = (UInt32)written;
    return _written;
}

设置:

void MemoryStreamBuf::written(UInt32 size) {
    _written=size;
}

2.5 显式设定 put 和 get 指针位置

设定 put 和 get 指针为以缓冲区首地址为开始偏移量为 pos 的位置:

void MemoryStreamBuf::position(UInt32 pos) {

    // 保存已写字节数
    written(); // Save nb char written

    // 移动 put 指针
    setp(_pBuffer, _pBuffer + _bufferSize);
    if (pos > _bufferSize)
        pos = _bufferSize;
    pbump((int) pos);

    // 移动 get 指针
    setg(_pBuffer, _pBuffer + pos, _pBuffer + _bufferSize);
}

2.6 修改缓冲区大小

void MemoryStreamBuf::resize(UInt32 newSize) {
    // 大小标识
    _bufferSize = newSize;

    // gptr 当前位置
    int pos = gCurrent() - _pBuffer;
    if (pos > _bufferSize) pos = _bufferSize;

    // 设置 gptr 可达范围和当前位置
    setg(_pBuffer, _pBuffer + pos, _pBuffer + _bufferSize); 
    // pptr 当前位置
    pos = pCurrent() - _pBuffer;
    if (pos > _bufferSize) pos = _bufferSize;

    // 设置 pptr 可达范围和当前位置
    setp(_pBuffer,_pBuffer + _bufferSize);
        pbump(pos);
}

2.7 构造函数、拷贝构造函数和析构函数

构造函数会设定 pptr 和 gptr,并初始化 pBuffer 和 bufferSize

MemoryStreamBuf::MemoryStreamBuf(char* pBuffer, UInt32 bufferSize):     _pBuffer(pBuffer),_bufferSize(bufferSize),_written(0) {
    setg(_pBuffer, _pBuffer,_pBuffer + _bufferSize);
    setp(_pBuffer, _pBuffer + _bufferSize);
}

析构函数会拷贝对方的 pBuffer、bufferSizse、_written,并设定 gptr、pptr。注意设定 pptr 时,要分别调用 setp 和 pbump,因为 setp 仅将 pptr 设定为传入的首个参数值(与可达范围的首地址相同)。

MemoryStreamBuf::MemoryStreamBuf(MemoryStreamBuf& other):   _pBuffer(other._pBuffer),_bufferSize(other._bufferSize),_written(other._written) {
    setg(_pBuffer, other.gCurrent(), _pBuffer + _bufferSize);
    setp(_pBuffer, _pBuffer + _bufferSize);
    pbump((int)(other.pCurrent()-_pBuffer));
}

析构函数:

MemoryStreamBuf::~MemoryStreamBuf() {
}

Reference

  1. http://www.cplusplus.com/reference/iostream/streambuf/gbump/
  2. http://www.cplusplus.com/reference/iostream/streambuf/pbump/

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好,以下是Cumulus交换机配置V-STP的步骤: 1. 首先,确认您的交换机支持V-STP功能,并且已经启用了STP协议。 2. 进入交换机的配置模式。 3. 输入以下命令启用V-STP功能: ``` switch(config)# spanning-tree protocol-version rstp-mst switch(config)# spanning-tree vlan 1-4094 mode mstp ``` 其中,vlan 1-4094表示启用所有VLAN的MSTP模式。 4. 配置V-STP实例。您可以为每个VLAN配置一个实例,或者将多个VLAN分配到同一个实例中。 ``` switch(config)# spanning-tree mst configuration switch(config-mst)# name instance1 switch(config-mst)# revision 1 switch(config-mst)# instance 1 vlan 10-20 switch(config-mst)# instance 2 vlan 30-40 ``` 其中,instance1表示实例名称,revision 1表示版本号,vlan 10-20和vlan 30-40表示将这些VLAN分配到实例1和实例2中。 5. 配置V-STP端口。将每个端口配置为所属的VLAN和实例。 ``` switch(config)# interface swp1 switch(config-if)# switchport switch(config-if)# switchport mode trunk switch(config-if)# switchport trunk allowed vlan 10-20 switch(config-if)# spanning-tree mst instance 1 vlan 10-20 ``` 其中,swp1表示端口名称,switchport mode trunk表示将端口配置为trunk模式,switchport trunk allowed vlan 10-20表示允许VLAN 10-20通过该端口,spanning-tree mst instance 1 vlan 10-20表示将该端口配置为实例1中的VLAN 10-20。 6. 最后,保存并应用配置。 ``` switch# save switch# exit ``` 以上就是在Cumulus交换机上配置V-STP的步骤,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值