1 #include "scd_command_reader.h"
5 #include "scd_logging.h"
8 scd_command_reader::scd_command_reader():
9 _command(NULL), _is_reading(false), _success(false) {}
12 scd_command_reader::~scd_command_reader()
14 if (_is_reading || _success)
21 void scd_command_reader::set_socket(scd_socket& sock) { _socket = &sock; }
24 void scd_command_reader::read()
33 _remaining = SCD_CM_HEADER;
35 _command = new scd_command;
42 size_t read = _socket->recv(_header_buf + _off, _remaining);
48 // read values from buffer
49 uint16_t* intbuf = reinterpret_cast<uint16_t*>(_header_buf);
50 _command->_type = ntohs( intbuf[0] );
51 _command->_subtype = ntohs( intbuf[1] );
52 _command->_msglen = ntohs( intbuf[2] );
56 // allocate buffer for further receiption
57 if (_command->_msglen > 0)
59 if (_command->_msglen > SCD_CM_MAXLEN)
62 _command->_msglen = 0;
68 _command->_msg = new char[_command->_msglen];
69 _remaining = _command->_msglen;
75 // command without payload
76 _command->_msglen = 0; // to prevent negative values
85 if (_header_read && _is_reading)
87 size_t read = _socket->recv(_command->_msg + _off, _remaining);
101 bool scd_command_reader::is_reading() { return _is_reading; }
104 bool scd_command_reader::has_command() { return _success; }
107 scd_command* scd_command_reader::get_command()