@@ -140,40 +140,15 @@ inline http::flat_buffer& CLASS::request_buffer() NOEXCEPT
140140// Send.
141141// ----------------------------------------------------------------------------
142142
143- TEMPLATE
144- void CLASS::send_code (const code& ec, result_handler&& handler) NOEXCEPT
145- {
146- send_error ({ .code = ec.value (), .message = ec.message () },
147- std::move (handler));
148- }
149-
150- TEMPLATE
151- void CLASS::send_error (rpc::result_t && error,
152- result_handler&& handler) NOEXCEPT
153- {
154- BC_ASSERT (stranded ());
155- const auto hint = two * error.message .size ();
156- send ({ .jsonrpc = version_, .id = identity_, .error = std::move (error) },
157- hint, std::move (handler));
158- }
159-
160- TEMPLATE
161- void CLASS::send_result (rpc::value_t && result, size_t size_hint,
162- result_handler&& handler) NOEXCEPT
163- {
164- BC_ASSERT (stranded ());
165- send ({ .jsonrpc = version_, .id = identity_, .result = std::move (result) },
166- size_hint, std::move (handler));
167- }
168-
169143// protected
170144TEMPLATE
171- inline void CLASS::send (rpc::response_t && model, size_t size_hint,
145+ template <typename Message>
146+ inline void CLASS::send (Message&& model, size_t size_hint,
172147 result_handler&& handler) NOEXCEPT
173148{
174149 BC_ASSERT (stranded ());
175150 const auto out = assign_message (std::move (model), size_hint);
176- count_handler complete = std::bind (&CLASS::handle_send,
151+ count_handler complete = std::bind (&CLASS::handle_send<Message> ,
177152 shared_from_base<CLASS>(), _1, _2, out, std::move (handler));
178153
179154 if (!out)
@@ -187,33 +162,96 @@ inline void CLASS::send(rpc::response_t&& model, size_t size_hint,
187162
188163// protected
189164TEMPLATE
165+ template <typename Message>
166+ inline rpc::message_ptr<Message> CLASS::assign_message (Message&& message,
167+ size_t size_hint) NOEXCEPT
168+ {
169+ BC_ASSERT (stranded ());
170+ response_buffer_->max_size (size_hint);
171+ const auto ptr = system::to_shared<rpc::message_value<Message>>();
172+ ptr->message = std::move (message);
173+ ptr->buffer = response_buffer_;
174+ return ptr;
175+ }
176+
177+ // protected
178+ TEMPLATE
179+ template <typename Message>
190180inline void CLASS::handle_send (const code& ec, size_t bytes,
191- const rpc::response_cptr& response, const result_handler& handler) NOEXCEPT
181+ const rpc::message_cptr<Message>& message,
182+ const result_handler& handler) NOEXCEPT
192183{
193184 BC_ASSERT (stranded ());
194185 if (ec) stop (ec);
195186
196187 // Typically a noop, but handshake may pause channel here.
197188 handler (ec);
198189
199- LOGA (" Rpc response: (" << bytes << " ) bytes [" << endpoint () << " ] "
200- << response->message .error .value_or (rpc::result_t {}).message );
190+ if constexpr (is_same_type<Message, rpc::response_t >)
191+ {
192+ LOGA (" Rpc response: (" << bytes << " ) bytes [" << endpoint () << " ] "
193+ << message->message .error .value_or (rpc::result_t {}).message );
201194
202- // Continue read loop (does not unpause or restart channel).
203- receive ();
195+ // Continue the read loop (does not unpause or restart).
196+ receive ();
197+ }
198+ else
199+ {
200+ LOGA (" Rpc notification: (" << bytes << " ) bytes [" << endpoint () << " ] "
201+ << message->message .method );
202+ }
204203}
205204
206- // private
207205TEMPLATE
208- inline rpc::response_ptr CLASS::assign_message (rpc::response_t && message,
209- size_t size_hint) NOEXCEPT
206+ inline void CLASS::send_code (const code& ec, result_handler&& handler) NOEXCEPT
207+ {
208+ send_error (
209+ {
210+ .code = ec.value (),
211+ .message = ec.message ()
212+ },
213+ std::move (handler));
214+ }
215+
216+ TEMPLATE
217+ inline void CLASS::send_error (rpc::result_t && error,
218+ result_handler&& handler) NOEXCEPT
210219{
211220 BC_ASSERT (stranded ());
212- response_buffer_->max_size (size_hint);
213- const auto ptr = system::to_shared<rpc::response>();
214- ptr->message = std::move (message);
215- ptr->buffer = response_buffer_;
216- return ptr;
221+ const auto hint = two * error.message .size ();
222+ send (rpc::response_t
223+ {
224+ .jsonrpc = version_,
225+ .id = identity_,
226+ .error = std::move (error)
227+ }, hint, std::move (handler));
228+ }
229+
230+ TEMPLATE
231+ inline void CLASS::send_result (rpc::value_t && result, size_t size_hint,
232+ result_handler&& handler) NOEXCEPT
233+ {
234+ BC_ASSERT (stranded ());
235+ send (rpc::response_t
236+ {
237+ .jsonrpc = version_,
238+ .id = identity_,
239+ .result = std::move (result)
240+ }, size_hint, std::move (handler));
241+ }
242+
243+ TEMPLATE
244+ inline void CLASS::send_notification (const rpc::string_t & method,
245+ rpc::params_t && notification, size_t size_hint,
246+ result_handler&& handler) NOEXCEPT
247+ {
248+ BC_ASSERT (stranded ());
249+ send (rpc::request_t
250+ {
251+ .jsonrpc = version_,
252+ .method = method,
253+ .params = std::move (notification)
254+ }, size_hint, std::move (handler));
217255}
218256
219257BC_POP_WARNING ()
0 commit comments