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
-
相關文章
- OpenRTMFP/Cumulus Primer(9)AMF解析之BinaryReader/Writer
- OpenRTMFP/Cumulus Primer(16)AMF解析之AMFReader
- OpenRTMFP/Cumulus Primer(9)AMF 處理方式解析——BinaryReader/Writer
- OpenRTMFP/Cumulus Primer(18)AMF解析之AMFReader(續2)
- OpenRTMFP/Cumulus Primer(17)AMF解析之AMFReader(續1)
- OpenRTMFP/Cumulus Primer(15)AMF解析之資料型別定義資料型別
- OpenRTMFP/Cumulus Primer(4)CumulusServer啟動流程分析Server
- OpenRTMFP/Cumulus Primer(5)CumulusServer啟動流程分析(續)Server
- OpenRTMFP/Cumulus Primer(13)IO管理之區域性記憶體片記憶體
- OpenRTMFP/Cumulus Primer(8)CumulusServer主程式主迴圈分析Server
- OpenRTMFP/Cumulus Primer(6)CumulusServer啟動流程分析(續2)Server
- OpenRTMFP/Cumulus Primer(7)CumulusServer啟動流程分析(續3)Server
- OpenRTMFP/Cumulus Primer(5)CumulusServer啟動流程分析(續1)Server
- OpenRTMFP/Cumulus Primer(1)入門介紹與部署CumulusServerServer
- OpenRTMFP/Cumulus Primer(7)CumulusServer 啟動流程分析(續3)Server
- OpenRTMFP/Cumulus Primer(2)用Lua編寫HelloWorld應用擴充套件CumulusServer套件Server
- OpenRTMFP/Cumulus Primer(19)獨立使用CumulusLib時的執行緒安全Bug執行緒
- OpenRTMFP/Cumulus Primer(21)經由伺服器的釋出/訂閱流程的關鍵點伺服器
- OpenRTMFP/Cumulus Primer(23)執行緒邏輯分析之二:RTMFPManager對RTMFPServer的影響執行緒Server
- OpenRTMFP/Cumulus Primer(22)執行緒邏輯分析之一:RTMFPServer執行緒的啟動和等待執行緒Server
- Spark原始碼解析之Shuffle WriterSpark原始碼
- Database Writer Process (DBWn) (114)Database
- AMF解析遇上XXE,BurpSuite也躺槍UI
- C++ Primer 課後習題解析C++
- c++ primer 之stringC++
- c++_primer之第四章C++
- Java IO: Reader And WriterJava
- 新手入門AMF協議基礎全解協議
- 簡約MarkDown寫作軟體:iA Writer 啟用中文版 「相容macos14」Mac
- Java IO: Reader和WriterJava
- 【文件學習】tensorboardX——summary writerORB
- 如何使用 LibreOffice Writer 模板
- 試試windows live writer的效果Windows
- Log Writer Process (LGWR) (115)
- 052-014題解析
- A Hadoop PrimerHadoop
- ORACLE SQL解析之硬解析和軟解析OracleSQL
- Archiver Failing With Ora-12571: Tns:Packet Writer Failure (文件 ID 1143523.1)HiveAI