| 152 | } |
| 153 | |
| 154 | int FlowControlProtocol::sendRegisterReq() { |
| 155 | if (_registered) { |
| 156 | logger_->log_debug("Already registered"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | uint16_t port = this->_serverPort; |
| 161 | |
| 162 | if (this->_socket <= 0) |
| 163 | this->_socket = connectServer(_serverName.c_str(), port); |
| 164 | |
| 165 | if (this->_socket <= 0) |
| 166 | return -1; |
| 167 | |
| 168 | // Calculate the total payload msg size |
| 169 | const auto payloadSize = FlowControlMsgIDEncodingLen(FLOW_SERIAL_NUMBER, 0) + FlowControlMsgIDEncodingLen(FLOW_YML_NAME, gsl::narrow<int>(this->_controller->getName().size() + 1)); |
| 170 | const size_t size = sizeof(FlowControlProtocolHeader) + payloadSize; |
| 171 | |
| 172 | std::vector<uint8_t> buffer; |
| 173 | buffer.resize(size); |
| 174 | uint8_t *data = buffer.data(); |
| 175 | |
| 176 | // encode the HDR |
| 177 | FlowControlProtocolHeader hdr; |
| 178 | hdr.msgType = REGISTER_REQ; |
| 179 | hdr.payloadLen = payloadSize; |
| 180 | hdr.seqNumber = this->_seqNumber; |
| 181 | hdr.status = RESP_SUCCESS; |
| 182 | data = this->encode(data, hdr.msgType); |
| 183 | data = this->encode(data, hdr.seqNumber); |
| 184 | data = this->encode(data, hdr.status); |
| 185 | data = this->encode(data, hdr.payloadLen); |
| 186 | |
| 187 | // encode the serial number |
| 188 | data = this->encode(data, FLOW_SERIAL_NUMBER); |
| 189 | data = this->encode(data, this->_serialNumber, 8); |
| 190 | |
| 191 | // encode the YAML name |
| 192 | data = this->encode(data, FLOW_YML_NAME); |
| 193 | data = this->encode(data, this->_controller->getName()); |
| 194 | |
| 195 | // send it |
| 196 | int status = sendData(buffer.data(), gsl::narrow<int>(size)); |
| 197 | buffer.clear(); |
| 198 | if (status <= 0) { |
| 199 | utils::file::FileUtils::close(_socket); |
| 200 | _socket = 0; |
| 201 | logger_->log_error("Flow Control Protocol Send Register Req failed"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | // Looking for register respond |
| 206 | status = readHdr(&hdr); |
| 207 | |
| 208 | if (status <= 0) { |
| 209 | utils::file::FileUtils::close(_socket); |
| 210 | _socket = 0; |
| 211 | logger_->log_error("Flow Control Protocol Read Register Resp header failed"); |
no test coverage detected