OpenRTMFP/Cumulus Primer(14)AMF解析之PacketReader/Writer

OpenRTMFP/Cumulus Primer(14)AMF解析之PacketReader/Writer

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

1 PacketReader

#define PACKETRECV_SIZE     2048

class PacketReader: public BinaryReader {
public:
    PacketReader(const Poco::UInt8* buffer,Poco::UInt32 size);
    PacketReader(PacketReader&);
    virtual ~PacketReader();

    const Poco::UInt32  fragments;

    Poco::UInt32    available(); // 可读字节数
    Poco::UInt8*    current();
    Poco::UInt32    position(); // 获取当前的相对位置(相对于起始位置的)

    void            reset(Poco::UInt32 newPos = 0); // 设定当前位置
    void            shrink(Poco::UInt32 rest);
    void            next(Poco::UInt32 size);
private:
    MemoryInputStream _memory;
};

1.1 封装 MemoryInputStream

  • available

      inline Poco::UInt32 PacketReader::available() {
          return _memory.available();
      }
    
  • current:当前绝对位置(内存地址)

      inline Poco::UInt8* PacketReader::current() {
          return (Poco::UInt8*)_memory.current();
      }
    
  • position:当前位置(绝对位置)减去缓冲区起始位置

      inline Poco::UInt32 PacketReader::position() {
          return _memory.current() - _memory.begin();
      }
    
  • reset

      inline void PacketReader::reset(Poco::UInt32 newPos) {
          _memory.reset(newPos);
      }
    
  • next

      inline void PacketReader::next(Poco::UInt32 size) {
          return _memory.next(size);
      }
    

1.2 收缩缓冲区

封装了 MemoryInputStream 的 resize。不过由于前面的 if 语句影响,传给 resize 的参数一定不会大于缓冲区的当前大小。

void PacketReader::shrink(UInt32 rest) {
    if (rest > available()) {
        WARN("rest %u more upper than available %u bytes",rest,available());
        rest = available();
    }
    _memory.resize(position() + rest);
}

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

构造函数先调用父类 BinaryReader 的构造函数,并初始化 fragments 和 _memory 输入流的缓冲区。

PacketReader::PacketReader(const UInt8* buffer,UInt32 size)
    : _memory((const char*)buffer, size),
      BinaryReader(_memory),
      fragments(1) {
}

// Consctruction by copy
PacketReader::PacketReader(PacketReader& other)
    : _memory(other._memory),
      BinaryReader(_memory),
      fragments(other.fragments) {
}

PacketReader::~PacketReader() {
}

2 PacketWriter

class PacketWriter: public BinaryWriter {
public:
    PacketWriter(const Poco::UInt8* buffer,Poco::UInt32 size);
    PacketWriter(PacketWriter&);
    virtual ~PacketWriter();

    Poco::UInt8*        begin();
    Poco::UInt32        length();
    Poco::UInt32        position();

    Poco::UInt32        available();

    bool    good();
    void    clear(Poco::UInt32 pos=0);
    void    reset(Poco::UInt32 newPos);
    void    limit(Poco::UInt32 length=0);
    void    next(Poco::UInt32 size);
    void    flush();

private:
    MemoryOutputStream  _memory;
    PacketWriter*       _pOther;
    Poco::UInt32        _size;
};

2.1 封装 MemoryOutputStream

  • available

      inline Poco::UInt32 PacketWriter::available() {
          return _memory.available();
      }
    
  • good:不过 MemoryOutputStream 也是封装的 std::ostream 的 good 函数,True if no error flags are set.

      inline bool PacketWriter::good() {
          return _memory.good();
      }
    
  • written

      inline Poco::UInt32 PacketWriter::length() {
          return _memory.written();
      }
    
  • position

      inline Poco::UInt32 PacketWriter::position() {
          return _memory.current()-(char*)begin();
      }
    
  • reset:设置缓冲区的指针位置,即 position

      inline void PacketWriter::reset(Poco::UInt32 newPos) {
          _memory.reset(newPos);
      }
    
  • next:移动缓冲区指针

      inline void PacketWriter::next(Poco::UInt32 size) {
          return _memory.next(size);
      }
    
  • begin:返回缓冲区的起始地址

      inline Poco::UInt8* PacketWriter::begin() {
          return (Poco::UInt8*)_memory.begin();
      }
    
  • clear:其实就是修改 written 和 position,使得指定位置后面的数据在以后写的时候可以被覆盖,并不是真正的清除。

      void PacketWriter::clear(UInt32 pos) {
          reset(pos);
          _memory.written(pos);
      }
    
  • limit:封装 MemoryOutputStream 的 resize

      void PacketWriter::limit(UInt32 length) {
          if (length == 0)
              length = _size;
          if (length > _size) {
              WARN("Limit '%d' more upper than buffer size '%d' bytes",length,_size);
              length = _size;
          }
          _memory.resize(length);
      }
    

2.2 封装 BinaryWriter

  • flush:封装 BinaryWriter 的 flush,不过 BinaryWriter 的 flush 实际上是从 Poco::BinaryWriter 继承而来的。其作用是“Flushes the underlying stream”。

      void PacketWriter::flush() {
          if (_pOther && _memory.written() > _pOther->_memory.written())
              _pOther->_memory.written(_memory.written());
          BinaryWriter::flush();
      }
    

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

PacketWriter::PacketWriter(const UInt8* buffer, UInt32 size)
    : _memory((char*)buffer, size),
      BinaryWriter(_memory),
      _pOther(NULL),
      _size(size) {
}

// Consctruction by copy
PacketWriter::PacketWriter(PacketWriter& other)
    : _pOther(&other),
      _memory(other._memory),
      BinaryWriter(_memory),
      _size(other._size) {
}

注意析构函数中会进行 flush:

PacketWriter::~PacketWriter() {
    flush();
}

-

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

-

你可能感兴趣的:(Stream,REST,Blog,Class,buffer,email)