cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
message_parser.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <utility>
23
28
29namespace msgpack_rpc::messages {
30
33
35
40
41void MessageParser::consumed(std::size_t num_bytes) {
42 parser_.buffer_consumed(num_bytes);
43}
44
45std::optional<ParsedMessage> MessageParser::try_parse() {
46 msgpack::object_handle object;
47 try {
48 if (!parser_.next(object)) {
49 return std::nullopt;
50 }
51 } catch (const msgpack::unpack_error&) {
53 StatusCode::INVALID_MESSAGE, "Failed to parse a message.");
54 }
55
56 return impl::parse_message_from_object(std::move(object));
57}
58
59} // namespace msgpack_rpc::messages
Class of exceptions in cpp-msgpack-rpc library.
Class of configuration of parsers of messages.
Class of buffers without memory management.
Definition buffer_view.h:29
std::size_t read_buffer_size_
Buffer size to read at once.
std::optional< ParsedMessage > try_parse()
Try to parse a message and return it if parsed, throw an exception if the message data is invalid.
void consumed(std::size_t num_bytes)
Set some bytes to be consumed.
MessageParser(const config::MessageParserConfig &config)
Constructor.
BufferView prepare_buffer()
Prepare a buffer.
msgpack::unpacker parser_
Parser.
Definition of MessageParser class.
Definition of MsgpackRPCException class.
Namespace of configurations.
ParsedMessage parse_message_from_object(msgpack::object_handle object)
Parse a message from an object in msgpack library.
Namespace of messages.
Definition buffer_view.h:24
Definition of parse_message_from_object function.
Definition of ParsedMessage type.
Definition of StatusCode enumeration.