Skip to content

Commit

Permalink
Make hub_connection always a shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Sep 26, 2022
1 parent 2fc3982 commit 3eaa50c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 117 deletions.
9 changes: 3 additions & 6 deletions include/signalrclient/hub_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ namespace signalr

SIGNALRCLIENT_API ~hub_connection();

hub_connection(const hub_connection&) = delete;

hub_connection(const hub_connection& rhs) = delete;
hub_connection& operator=(const hub_connection&) = delete;

SIGNALRCLIENT_API hub_connection(hub_connection&&) noexcept;

SIGNALRCLIENT_API hub_connection& operator=(hub_connection&&) noexcept;
hub_connection(hub_connection&&) = delete;
hub_connection& operator=(hub_connection&&) = delete;

SIGNALRCLIENT_API void __cdecl start(std::function<void(std::exception_ptr)> callback) noexcept;
SIGNALRCLIENT_API void __cdecl stop(std::function<void(std::exception_ptr)> callback) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion include/signalrclient/hub_connection_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace signalr
SIGNALRCLIENT_API hub_connection_builder& with_messagepack_hub_protocol();
#endif

SIGNALRCLIENT_API hub_connection build();
SIGNALRCLIENT_API std::shared_ptr<hub_connection> build();
private:
hub_connection_builder(const std::string& url);

Expand Down
16 changes: 8 additions & 8 deletions samples/HubConnectionSample/HubConnectionSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ void send_message(signalr::hub_connection& connection, const std::string& messag

void chat()
{
signalr::hub_connection connection = signalr::hub_connection_builder::create("http://localhost:5000/default")
.with_logging(std::make_shared <logger>(), signalr::trace_level::verbose)
std::shared_ptr<signalr::hub_connection> connection = signalr::hub_connection_builder::create("http://localhost:5000/default")
.with_logging(std::make_shared<logger>(), signalr::trace_level::verbose)
.build();

connection.on("Send", [](const std::vector<signalr::value>& m)
connection->on("Send", [](const std::vector<signalr::value>& m)
{
std::cout << std::endl << m[0].as_string() << std::endl << "Enter your message: ";
});

std::promise<void> task;
connection.start([&connection, &task](std::exception_ptr exception)
connection->start([&connection, &task](std::exception_ptr exception)
{
if (exception)
{
Expand All @@ -77,20 +77,20 @@ void chat()
}

std::cout << "Enter your message:";
while (connection.get_connection_state() == signalr::connection_state::connected)
while (connection->get_connection_state() == signalr::connection_state::connected)
{
std::string message;
std::getline(std::cin, message);

if (message == ":q" || connection.get_connection_state() != signalr::connection_state::connected)
if (message == ":q" || connection->get_connection_state() != signalr::connection_state::connected)
{
break;
}

send_message(connection, message);
//send_message(connection, message);
}

connection.stop([&task](std::exception_ptr exception)
connection->stop([&task](std::exception_ptr exception)
{
try
{
Expand Down
11 changes: 0 additions & 11 deletions src/signalrclient/hub_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ namespace signalr
: m_pImpl(hub_connection_impl::create(url, std::move(hub_protocol), trace_level, log_writer, http_client_factory, websocket_factory, skip_negotiation))
{}

hub_connection::hub_connection(hub_connection&& rhs) noexcept
: m_pImpl(std::move(rhs.m_pImpl))
{}

hub_connection& hub_connection::operator=(hub_connection&& rhs) noexcept
{
m_pImpl = std::move(rhs.m_pImpl);

return *this;
}

// Do NOT remove this destructor. Letting the compiler generate and inline the default dtor may lead to
// undefined behavior since we are using an incomplete type. More details here: http://herbsutter.com/gotw/_100/
hub_connection::~hub_connection()
Expand Down
4 changes: 2 additions & 2 deletions src/signalrclient/hub_connection_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace signalr
}
#endif

hub_connection hub_connection_builder::build()
std::shared_ptr<hub_connection> hub_connection_builder::build()
{
#ifndef USE_CPPRESTSDK
if (m_http_client_factory == nullptr)
Expand All @@ -115,6 +115,6 @@ namespace signalr
hub_protocol = std::unique_ptr<json_hub_protocol>(new json_hub_protocol());
}

return hub_connection(m_url, std::move(hub_protocol), m_log_level, m_logger, m_http_client_factory, m_websocket_factory, m_skip_negotiation);
return std::shared_ptr<hub_connection>(new hub_connection(m_url, std::move(hub_protocol), m_log_level, m_logger, m_http_client_factory, m_websocket_factory, m_skip_negotiation));
}
}
Loading

0 comments on commit 3eaa50c

Please sign in to comment.