Skip to content

Commit 2061314

Browse files
Refactor the protocol and handshake (#14)
1 parent 4007ee2 commit 2061314

27 files changed

+978
-767
lines changed

include/signalrclient/signalr_value.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ namespace signalr
5858
*/
5959
value(std::string&& val);
6060

61+
/**
62+
* Create an object representing a value_type::string with the given string value.
63+
*/
64+
value(const char* val);
65+
6166
/**
6267
* Create an object representing a value_type::array with the given vector of value's.
6368
*/

src/signalrclient/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ set (SOURCES
44
connection_impl.cpp
55
default_http_client.cpp
66
default_websocket_client.cpp
7+
handshake_protocol.cpp
78
hub_connection.cpp
89
hub_connection_impl.cpp
10+
json_helpers.cpp
11+
json_hub_protocol.cpp
912
logger.cpp
1013
negotiate.cpp
1114
signalr_client_config.cpp

src/signalrclient/connection_impl.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ namespace signalr
283283
auto transport = connection->m_transport_factory->create_transport(
284284
transport_type::websockets, connection->m_logger, connection->m_signalr_client_config);
285285

286-
transport->on_receive([disconnect_cts, connect_request_done, connect_request_lock, logger, weak_connection, callback](const std::string& message, std::exception_ptr exception)
286+
transport->on_receive([disconnect_cts, connect_request_done, connect_request_lock, logger, weak_connection, callback](std::string&& message, std::exception_ptr exception)
287287
{
288288
if (exception == nullptr)
289289
{
@@ -298,7 +298,7 @@ namespace signalr
298298
auto connection = weak_connection.lock();
299299
if (connection)
300300
{
301-
connection->process_response(message);
301+
connection->process_response(std::move(message));
302302
}
303303
}
304304
else
@@ -433,19 +433,19 @@ namespace signalr
433433
});
434434
}
435435

436-
void connection_impl::process_response(const std::string& response)
436+
void connection_impl::process_response(std::string&& response)
437437
{
438438
m_logger.log(trace_level::messages,
439439
std::string("processing message: ").append(response));
440440

441-
invoke_message_received(response);
441+
invoke_message_received(std::move(response));
442442
}
443443

444-
void connection_impl::invoke_message_received(const std::string& message)
444+
void connection_impl::invoke_message_received(std::string&& message)
445445
{
446446
try
447447
{
448-
m_message_received(message);
448+
m_message_received(std::move(message));
449449
}
450450
catch (const std::exception &e)
451451
{
@@ -618,7 +618,7 @@ namespace signalr
618618
return m_connection_id;
619619
}
620620

621-
void connection_impl::set_message_received(const std::function<void(const std::string&)>& message_received)
621+
void connection_impl::set_message_received(const std::function<void(std::string&&)>& message_received)
622622
{
623623
ensure_disconnected("cannot set the callback when the connection is not in the disconnected state. ");
624624
m_message_received = message_received;

src/signalrclient/connection_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace signalr
4242
connection_state get_connection_state() const noexcept;
4343
std::string get_connection_id() const noexcept;
4444

45-
void set_message_received(const std::function<void(const std::string&)>& message_received);
45+
void set_message_received(const std::function<void(std::string&&)>& message_received);
4646
void set_disconnected(const std::function<void()>& disconnected);
4747
void set_client_config(const signalr_client_config& config);
4848

@@ -53,7 +53,7 @@ namespace signalr
5353
std::shared_ptr<transport> m_transport;
5454
std::unique_ptr<transport_factory> m_transport_factory;
5555

56-
std::function<void(const std::string&)> m_message_received;
56+
std::function<void(std::string&&)> m_message_received;
5757
std::function<void()> m_disconnected;
5858
signalr_client_config m_signalr_client_config;
5959

@@ -72,14 +72,14 @@ namespace signalr
7272
const std::string& url, std::function<void(std::exception_ptr)> callback);
7373
void start_negotiate(const std::string& url, int redirect_count, std::function<void(std::exception_ptr)> callback);
7474

75-
void process_response(const std::string& response);
75+
void process_response(std::string&& response);
7676

7777
void shutdown(std::function<void(std::exception_ptr)> callback);
7878

7979
bool change_state(connection_state old_state, connection_state new_state);
8080
connection_state change_state(connection_state new_state);
8181
void handle_connection_state_change(connection_state old_state, connection_state new_state);
82-
void invoke_message_received(const std::string& message);
82+
void invoke_message_received(std::string&& message);
8383

8484
static std::string translate_connection_state(connection_state state);
8585
void ensure_disconnected(const std::string& error_message) const;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#include "stdafx.h"
5+
#include "handshake_protocol.h"
6+
#include "json_helpers.h"
7+
#include "signalrclient/signalr_exception.h"
8+
9+
namespace signalr
10+
{
11+
namespace handshake
12+
{
13+
std::string write_handshake(std::shared_ptr<hub_protocol>& protocol)
14+
{
15+
auto map = std::map<std::string, signalr::value>
16+
{
17+
{ "protocol", signalr::value(protocol->name()) },
18+
{ "version", signalr::value((double)protocol->version()) }
19+
};
20+
return utility::conversions::to_utf8string(createJson(signalr::value(std::move(map))).serialize()) + record_separator;
21+
}
22+
23+
std::tuple<std::string, signalr::value> parse_handshake(const std::string& response)
24+
{
25+
auto pos = response.find(record_separator);
26+
if (pos == std::string::npos)
27+
{
28+
throw signalr_exception("incomplete message received");
29+
}
30+
auto message = response.substr(0, pos);
31+
auto result = web::json::value::parse(utility::conversions::to_string_t(message));
32+
auto remaining_data = response.substr(pos + 1);
33+
return std::forward_as_tuple(remaining_data, createValue(result));
34+
}
35+
}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#pragma once
5+
6+
#include "signalrclient/signalr_value.h"
7+
#include "hub_protocol.h"
8+
#include <memory>
9+
10+
namespace signalr
11+
{
12+
namespace handshake
13+
{
14+
std::string write_handshake(std::shared_ptr<hub_protocol>&);
15+
std::tuple<std::string, signalr::value> parse_handshake(const std::string&);
16+
}
17+
}

0 commit comments

Comments
 (0)