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

鍾超發表於2012-04-24

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

-

相關文章