cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
message_serializer.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 <tuple>
23
24#include <msgpack.hpp>
25
30
31namespace msgpack_rpc::messages {
32
37public:
47 template <typename... Parameters>
49 MethodNameView method_name, MessageID message_id,
50 const Parameters&... parameters) {
52 msgpack::packer<impl::SerializationBuffer> packer{buffer};
53 packer.pack_array(4);
54 packer.pack(0);
55 packer.pack(message_id);
56 packer.pack(method_name.name());
57 packer.pack(std::forward_as_tuple(parameters...));
58 return buffer.release();
59 }
60
69 template <typename T>
71 MessageID request_id, const T& result) {
73 msgpack::packer<impl::SerializationBuffer> packer{buffer};
74 packer.pack_array(4);
75 packer.pack(1);
76 packer.pack(request_id);
77 packer.pack_nil();
78 packer.pack(result);
79 return buffer.release();
80 }
81
90 template <typename T>
92 MessageID request_id, const T& error) {
94 msgpack::packer<impl::SerializationBuffer> packer{buffer};
95 packer.pack_array(4);
96 packer.pack(1);
97 packer.pack(request_id);
98 packer.pack(error);
99 packer.pack_nil();
100 return buffer.release();
101 }
102
111 template <typename... Parameters>
113 MethodNameView method_name, const Parameters&... parameters) {
115 msgpack::packer<impl::SerializationBuffer> packer{buffer};
116 packer.pack_array(3);
117 packer.pack(2);
118 packer.pack(method_name.name());
119 packer.pack(std::forward_as_tuple(parameters...));
120 return buffer.release();
121 }
122};
123
124} // namespace msgpack_rpc::messages
static SerializedMessage serialize_error_response(MessageID request_id, const T &error)
Serialize an error response.
static SerializedMessage serialize_notification(MethodNameView method_name, const Parameters &... parameters)
Serialize a notification.
static SerializedMessage serialize_successful_response(MessageID request_id, const T &result)
Serialize a successful response.
static SerializedMessage serialize_request(MethodNameView method_name, MessageID message_id, const Parameters &... parameters)
Serialize a request.
std::string_view name() const noexcept
Get the method name.
Class of serialized message data.
SerializedMessage release() noexcept
Release the buffer as msgpack_rpc::messages::SerializedMessage object.
Definition of MessageID type.
Definition of MethodNameView class.
Namespace of messages.
Definition buffer_view.h:24
std::uint32_t MessageID
Type of message IDs.
Definition message_id.h:27
Definition of SerializationBuffer class.
Definition of SerializedMessage class.