| 160 | }; |
| 161 | |
| 162 | void message_oriented_connection_impl::read_loop() |
| 163 | { |
| 164 | VERIFY_CORRECT_THREAD(); |
| 165 | const int BUFFER_SIZE = 16; |
| 166 | const int LEFTOVER = BUFFER_SIZE - sizeof(message_header); |
| 167 | static_assert(BUFFER_SIZE >= sizeof(message_header), "insufficient buffer"); |
| 168 | |
| 169 | no_parallel_execution_guard guard( &_read_loop_in_progress ); |
| 170 | |
| 171 | _connected_time = fc::time_point::now(); |
| 172 | |
| 173 | fc::oexception exception_to_rethrow; |
| 174 | bool call_on_connection_closed = false; |
| 175 | bool io_error = false; |
| 176 | |
| 177 | try |
| 178 | { |
| 179 | message m; |
| 180 | char buffer[BUFFER_SIZE]; |
| 181 | while( true ) |
| 182 | { |
| 183 | try { |
| 184 | _sock.read(buffer, BUFFER_SIZE); |
| 185 | } catch ( const fc::canceled_exception& ) { |
| 186 | io_error = true; |
| 187 | throw; |
| 188 | } |
| 189 | _bytes_received += BUFFER_SIZE; |
| 190 | memcpy((char*)&m, buffer, sizeof(message_header)); |
| 191 | FC_ASSERT( m.size.value() <= MAX_MESSAGE_SIZE, "", ("m.size",m.size.value())("MAX_MESSAGE_SIZE",MAX_MESSAGE_SIZE) ); |
| 192 | |
| 193 | size_t remaining_bytes_with_padding = 16 * ((m.size.value() - LEFTOVER + 15) / 16); |
| 194 | m.data.resize(LEFTOVER + remaining_bytes_with_padding); //give extra 16 bytes to allow for padding added in send call |
| 195 | std::copy(buffer + sizeof(message_header), buffer + sizeof(buffer), m.data.begin()); |
| 196 | if (remaining_bytes_with_padding) |
| 197 | { |
| 198 | try { |
| 199 | _sock.read(&m.data[LEFTOVER], remaining_bytes_with_padding); |
| 200 | } catch ( const fc::canceled_exception& ) { |
| 201 | io_error = true; |
| 202 | throw; |
| 203 | } |
| 204 | _bytes_received += remaining_bytes_with_padding; |
| 205 | } |
| 206 | m.data.resize(m.size.value()); // truncate off the padding bytes |
| 207 | |
| 208 | _last_message_received_time = fc::time_point::now(); |
| 209 | |
| 210 | try |
| 211 | { |
| 212 | // message handling errors are warnings... |
| 213 | _delegate->on_message(_self, m); |
| 214 | } |
| 215 | /// Dedicated catches needed to distinguish from general fc::exception |
| 216 | catch ( const fc::canceled_exception& e ) { throw; } |
| 217 | catch ( const fc::eof_exception& e ) { throw; } |
| 218 | catch ( const fc::exception& e) |
| 219 | { |
nothing calls this directly
no test coverage detected