cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parse_message_from_object.h
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 */
20#pragma once
21
22#include <cstdint>
23#include <cstdlib>
24#include <memory>
25#include <string_view>
26#include <utility>
27
28#include <msgpack.hpp>
29
41
43
51 const msgpack::object& object) {
52 try {
53 const auto val = object.as<std::uint64_t>();
54 switch (val) {
55 case 0U:
57 case 1U:
59 case 2U:
61 default:
62 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
63 "Invalid message type in a message.");
64 }
65 } catch (const msgpack::type_error&) {
67 StatusCode::INVALID_MESSAGE, "Invalid message type in a message.");
68 }
69}
70
78 const msgpack::object& object) {
79 try {
80 return object.as<MessageID>();
81 } catch (const msgpack::type_error&) {
83 StatusCode::INVALID_MESSAGE, "Invalid message ID in a message.");
84 }
85}
86
94 const msgpack::object& object) {
95 try {
96 return MethodNameView(object.as<std::string_view>());
97 } catch (const msgpack::type_error&) {
99 StatusCode::INVALID_MESSAGE, "Invalid method name in a message.");
100 }
101}
102
110 msgpack::object_handle object) {
111 constexpr std::uint32_t num_elements_in_root_array = 4;
112 if (object->via.array.size != num_elements_in_root_array) {
113 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
114 "Invalid size of the array of a message.");
115 }
116
117 const auto message_id =
118 parse_message_id_from_object(object->via.array.ptr[1]);
119 auto method_name = parse_method_name_from_object(object->via.array.ptr[2]);
120 auto parameters =
121 ParsedParameters(object->via.array.ptr[3], std::move(object.zone()));
122 return ParsedRequest(message_id, method_name, std::move(parameters));
123}
124
132 msgpack::object_handle object) {
133 constexpr std::uint32_t num_elements_in_root_array = 4;
134 if (object->via.array.size != num_elements_in_root_array) {
135 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
136 "Invalid size of the array of a message.");
137 }
138
139 const auto message_id =
140 parse_message_id_from_object(object->via.array.ptr[1]);
141 const bool has_error = !(object->via.array.ptr[2].is_nil());
142 if (has_error) {
143 return ParsedResponse(message_id,
145 object->via.array.ptr[2], std::move(object.zone())));
146 }
147 return ParsedResponse(message_id,
149 object->via.array.ptr[3], std::move(object.zone())));
150}
151
159 msgpack::object_handle object) {
160 constexpr std::uint32_t num_elements_in_root_array = 3;
161 if (object->via.array.size != num_elements_in_root_array) {
162 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
163 "Invalid size of the array of a message.");
164 }
165
166 auto method_name = parse_method_name_from_object(object->via.array.ptr[1]);
167 auto parameters =
168 ParsedParameters(object->via.array.ptr[2], std::move(object.zone()));
169 return ParsedNotification(method_name, std::move(parameters));
170}
171
179 msgpack::object_handle object) {
180 if (object->type != msgpack::type::ARRAY) {
182 StatusCode::INVALID_MESSAGE, "Invalid type of a message.");
183 }
184 constexpr std::uint32_t min_elements_in_root_array = 3;
185 if (object->via.array.size < min_elements_in_root_array) {
186 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
187 "Invalid size of the array of a message.");
188 }
189
190 const auto message_type =
191 parse_message_type_from_object(object->via.array.ptr[0]);
192 switch (message_type) {
194 return parse_request_from_object(std::move(object));
196 return parse_response_from_object(std::move(object));
198 return parse_notification_from_object(std::move(object));
199 }
200 // This line won't be executed without a bug.
201 std::abort();
202}
203
204} // namespace msgpack_rpc::messages::impl
Definition of CallResult class.
Class of exceptions in cpp-msgpack-rpc library.
static CallResult create_error(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an error result.
Definition call_result.h:57
static CallResult create_result(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an successful result.
Definition call_result.h:44
MethodNameView(const MethodName &name) noexcept
Constructor.
ParsedNotification(MethodNameView method_name, ParsedParameters parameters)
Constructor.
ParsedParameters(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Constructor.
ParsedRequest(MessageID id, MethodNameView method_name, ParsedParameters parameters) noexcept
Constructor.
ParsedResponse(MessageID id, CallResult result)
Constructor.
Definition of MessageID type.
Definition of MessageType enumeration.
Definition of MethodNameView class.
Definition of MsgpackRPCException class.
Namespace of internal implementations.
MessageID parse_message_id_from_object(const msgpack::object &object)
Parse a message ID from an object in msgpack library.
MethodNameView parse_method_name_from_object(const msgpack::object &object)
Parse a method name from an object in msgpack library.
ParsedNotification parse_notification_from_object(msgpack::object_handle object)
Parse a notification from an object in msgpack library.
ParsedMessage parse_message_from_object(msgpack::object_handle object)
Parse a message from an object in msgpack library.
ParsedResponse parse_response_from_object(msgpack::object_handle object)
Parse a response from an object in msgpack library.
MessageType parse_message_type_from_object(const msgpack::object &object)
Parse a message type from an object in msgpack library.
ParsedRequest parse_request_from_object(msgpack::object_handle object)
Parse a request from an object in msgpack library.
std::variant< ParsedRequest, ParsedResponse, ParsedNotification > ParsedMessage
Type of parsed messages.
MessageType
Enumeration of message types.
std::uint32_t MessageID
Type of message IDs.
Definition message_id.h:27
Definition of ParsedMessage type.
Definition of ParsedNotification class.
Definition of ParsedParameters class.
Definition of ParsedRequest class.
Definition of ParsedResponse class.
Definition of StatusCode enumeration.