@@ -70,7 +70,7 @@ DispatcherBase::WeakPtr::~WeakPtr()
7070 m_dispatcher->m_weakPtrs.erase(this);
7171}
7272
73- DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const String & message)
73+ DispatcherBase::Callback::Callback(std::unique_ptr<DispatcherBase::WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage & message)
7474 : m_backendImpl(std::move(backendImpl))
7575 , m_callId(callId)
7676 , m_method(method)
@@ -142,18 +142,14 @@ public:
142142 return std::unique_ptr<ProtocolError>(new ProtocolError(code, errorMessage));
143143 }
144144
145- String serialize () override
145+ String serializeToJSON () override
146146 {
147- std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create();
148- error->setInteger("code", m_code);
149- error->setString("message", m_errorMessage);
150- if (m_data.length())
151- error->setString("data", m_data);
152- std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create();
153- message->setObject("error", std::move(error));
154- if (m_hasCallId)
155- message->setInteger("id", m_callId);
156- return message->serialize();
147+ return serialize()->serializeToJSON();
148+ }
149+
150+ std::vector<uint8_t> serializeToBinary() override
151+ {
152+ return serialize()->serializeToBinary();
157153 }
158154
159155 ~ProtocolError() override {}
@@ -165,6 +161,19 @@ private:
165161 {
166162 }
167163
164+ std::unique_ptr<DictionaryValue> serialize() {
165+ std::unique_ptr<protocol::DictionaryValue> error = DictionaryValue::create();
166+ error->setInteger("code", m_code);
167+ error->setString("message", m_errorMessage);
168+ if (m_data.length())
169+ error->setString("data", m_data);
170+ std::unique_ptr<protocol::DictionaryValue> message = DictionaryValue::create();
171+ message->setObject("error", std::move(error));
172+ if (m_hasCallId)
173+ message->setInteger("id", m_callId);
174+ return message;
175+ }
176+
168177 DispatchResponse::ErrorCode m_code;
169178 String m_errorMessage;
170179 String m_data;
@@ -275,7 +284,7 @@ bool UberDispatcher::canDispatch(const String& in_method)
275284 return !!findDispatcher(method);
276285}
277286
278- void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const String & rawMessage)
287+ void UberDispatcher::dispatch(int callId, const String& in_method, std::unique_ptr<Value> parsedMessage, const ProtocolMessage & rawMessage)
279288{
280289 String method = in_method;
281290 auto redirectIt = m_redirects.find(method);
@@ -304,18 +313,32 @@ std::unique_ptr<InternalResponse> InternalResponse::createNotification(const Str
304313 return std::unique_ptr<InternalResponse>(new InternalResponse(0, notification, std::move(params)));
305314}
306315
307- String InternalResponse::serialize()
316+ String InternalResponse::serializeToJSON()
317+ {
318+ std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
319+ std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
320+ if (m_notification.length()) {
321+ result->setString("method", m_notification);
322+ result->setValue("params", SerializedValue::fromJSON(params->serializeToJSON()));
323+ } else {
324+ result->setInteger("id", m_callId);
325+ result->setValue("result", SerializedValue::fromJSON(params->serializeToJSON()));
326+ }
327+ return result->serializeToJSON();
328+ }
329+
330+ std::vector<uint8_t> InternalResponse::serializeToBinary()
308331{
309332 std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
310333 std::unique_ptr<Serializable> params(m_params ? std::move(m_params) : DictionaryValue::create());
311334 if (m_notification.length()) {
312335 result->setString("method", m_notification);
313- result->setValue("params", SerializedValue::create (params->serialize ()));
336+ result->setValue("params", SerializedValue::fromBinary (params->serializeToBinary ()));
314337 } else {
315338 result->setInteger("id", m_callId);
316- result->setValue("result", SerializedValue::create (params->serialize ()));
339+ result->setValue("result", SerializedValue::fromBinary (params->serializeToBinary ()));
317340 }
318- return result->serialize ();
341+ return result->serializeToBinary ();
319342}
320343
321344InternalResponse::InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params)
0 commit comments