commandWriter.cpp (1388B)
1 #include "commandWriter.h" 2 3 #include <array> 4 5 #include "networkLib/stream.h" 6 7 namespace bridgeLib 8 { 9 CommandWriter::CommandWriter() : m_stream(512 * 1024) 10 { 11 } 12 13 baseLib::BinaryStream& CommandWriter::build(const Command _command) 14 { 15 m_stream.setWritePos(0); 16 m_command = _command; 17 18 return m_stream; 19 } 20 21 baseLib::BinaryStream& CommandWriter::build(const Command _command, const CommandStruct& _data) 22 { 23 return _data.write(build(_command)); 24 } 25 26 void CommandWriter::write(networkLib::Stream& _stream, const bool _flush) 27 { 28 // send command (4 bytes) 29 std::array<char,5> buf; 30 commandToBuffer(buf, m_command); 31 _stream.write(buf.data(), 4); 32 33 // send size (4 bytes) 34 const auto size = m_stream.getWritePos(); 35 _stream.write(&size, sizeof(size)); 36 37 // send data (size bytes) 38 const auto* data = m_stream.getVector().data(); 39 _stream.write(data, size); 40 41 if(_flush) 42 _stream.flush(); 43 } 44 45 void CommandWriter::write(baseLib::BinaryStream& _out) 46 { 47 // send command (4 bytes) 48 std::array<char,5> buf; 49 commandToBuffer(buf, m_command); 50 _out.write(buf[0]); 51 _out.write(buf[1]); 52 _out.write(buf[2]); 53 _out.write(buf[3]); 54 55 // send size (4 bytes) 56 const auto size = m_stream.getWritePos(); 57 _out.write(size); 58 59 // send data (size bytes) 60 const auto* data = m_stream.getVector().data(); 61 for(uint32_t i=0; i<size; ++i) 62 _out.write(m_stream.getVector()[i]); 63 } 64 }