| 183 | } |
| 184 | |
| 185 | td::Status TcpConnection::receive(td::ChainBufferReader &input, bool &exit_loop) { |
| 186 | if (stop_read_) { |
| 187 | exit_loop = true; |
| 188 | return td::Status::OK(); |
| 189 | } |
| 190 | if (input.size() > 0) { |
| 191 | received_bytes_ = 1; |
| 192 | } |
| 193 | if (!read_len_) { |
| 194 | if (input.size() < 4) { |
| 195 | exit_loop = true; |
| 196 | return td::Status::OK(); |
| 197 | } |
| 198 | |
| 199 | td::MutableSlice s{reinterpret_cast<td::uint8 *>(&len_), 4}; |
| 200 | input.advance(4, s); |
| 201 | |
| 202 | LOG(DEBUG) << "tcp: len=" << len_; |
| 203 | if (len_ > (1 << 24) || len_ < 4) { |
| 204 | return td::Status::Error(ton::ErrorCode::protoviolation, PSTRING() << "Too big packet " << len_); |
| 205 | } |
| 206 | read_len_ = true; |
| 207 | } |
| 208 | if (input.size() < len_ + 4) { |
| 209 | exit_loop = true; |
| 210 | return td::Status::OK(); |
| 211 | } |
| 212 | |
| 213 | td::int32 in_seqno; |
| 214 | td::MutableSlice s{reinterpret_cast<td::uint8 *>(&in_seqno), 4}; |
| 215 | input.advance(4, s); |
| 216 | |
| 217 | if (in_seqno != in_seqno_) { |
| 218 | return td::Status::Error(ton::ErrorCode::protoviolation, |
| 219 | PSTRING() << "bad seqno: expected " << in_seqno_ << " got " << in_seqno); |
| 220 | } |
| 221 | |
| 222 | auto data = input.cut_head(len_).move_as_buffer_slice(); |
| 223 | update_timer(); |
| 224 | |
| 225 | exit_loop = false; |
| 226 | read_len_ = false; |
| 227 | in_seqno_++; |
| 228 | len_ = 0; |
| 229 | if (inited_) { |
| 230 | return receive_packet(std::move(data)); |
| 231 | } else { |
| 232 | return process_init_packet(std::move(data)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void TcpConnection::loop() { |
| 237 | auto status = [&] { |
nothing calls this directly
no outgoing calls
no test coverage detected