123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #ifndef BHO_MYSQL_IMPL_INTERNAL_CHANNEL_MESSAGE_PARSER_IPP
- #define BHO_MYSQL_IMPL_INTERNAL_CHANNEL_MESSAGE_PARSER_IPP
- #pragma once
- #include <asio2/bho/mysql/impl/internal/channel/message_parser.hpp>
- #include <asio2/bho/mysql/impl/internal/protocol/protocol.hpp>
- void bho::mysql::detail::message_parser::parse_message(read_buffer& buff, result& res) noexcept
- {
- while (true)
- {
- if (state_.reading_header)
- {
-
- if (buff.pending_size() < HEADER_SIZE)
- {
- res.set_required_size(HEADER_SIZE - buff.pending_size());
- return;
- }
-
- buff.move_to_current_message(HEADER_SIZE);
-
- auto header = deserialize_frame_header(
- span<const std::uint8_t, frame_header_size>(buff.pending_first() - HEADER_SIZE, HEADER_SIZE)
- );
-
- if (state_.is_first_frame)
- {
- state_.seqnum_first = header.sequence_number;
- state_.seqnum_last = header.sequence_number;
- }
- else
- {
- std::uint8_t expected_seqnum = state_.seqnum_last + 1;
- if (header.sequence_number != expected_seqnum)
- {
- state_.has_seqnum_mismatch = true;
- }
- state_.seqnum_last = expected_seqnum;
- }
-
- state_.remaining_bytes = header.size;
- state_.more_frames_follow = (state_.remaining_bytes == max_frame_size_);
-
- if (state_.is_first_frame)
- {
-
-
- buff.move_to_reserved(HEADER_SIZE);
- }
- else
- {
- buff.remove_current_message_last(HEADER_SIZE);
- }
- state_.is_first_frame = false;
- state_.reading_header = false;
- }
- if (!state_.reading_header)
- {
-
- std::size_t new_bytes = (std::min)(buff.pending_size(), state_.remaining_bytes);
-
- buff.move_to_current_message(new_bytes);
-
- state_.remaining_bytes -= new_bytes;
- if (state_.remaining_bytes == 0)
- {
- state_.reading_header = true;
- }
- else
- {
- res.set_required_size(state_.remaining_bytes);
- return;
- }
-
- if (!state_.remaining_bytes && !state_.more_frames_follow)
- {
- std::size_t message_size = buff.current_message_size();
- buff.move_to_reserved(message_size);
- res.set_message({
- state_.seqnum_first,
- state_.seqnum_last,
- message_size,
- state_.has_seqnum_mismatch,
- });
- state_ = state_t();
- return;
- }
- }
- }
- }
- #endif
|