Skip to content

Commit ff2a72d

Browse files
committed
wip
1 parent 5ab0135 commit ff2a72d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1113
-1072
lines changed

include/signalrclient/http_client.h

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,68 @@ namespace signalr
1919
POST
2020
};
2121

22-
class http_request
22+
class http_request final
2323
{
2424
public:
25+
http_method get_method() const;
26+
27+
const std::map<std::string, std::string>& get_headers() const;
28+
29+
const std::string& get_content() const;
30+
31+
std::chrono::seconds get_timeout() const;
32+
private:
33+
friend class negotiate;
34+
35+
http_method m_method;
36+
std::map<std::string, std::string> m_headers;
37+
std::string m_content;
38+
std::chrono::seconds m_timeout;
39+
2540
http_request()
26-
: method(http_method::GET), timeout(std::chrono::seconds(120))
41+
: m_method(http_method::GET), m_timeout(std::chrono::seconds(120))
2742
{ }
2843

29-
http_method method;
30-
std::map<std::string, std::string> headers;
31-
std::string content;
32-
std::chrono::seconds timeout;
44+
void set_timeout(std::chrono::seconds timeout);
45+
void set_content(const std::string& body);
46+
void set_content(std::string&& body);
47+
void set_headers(const std::map<std::string, std::string>& headers);
48+
void set_method(http_method method);
3349
};
3450

35-
class http_response
51+
class http_response final
3652
{
3753
public:
3854
http_response() {}
39-
http_response(http_response&& rhs) noexcept : status_code(rhs.status_code), content(std::move(rhs.content)) {}
40-
http_response(int code, const std::string& content) : status_code(code), content(content) {}
41-
http_response(const http_response& rhs) : status_code(rhs.status_code), content(rhs.content) {}
55+
http_response(http_response&& rhs) noexcept : m_status_code(rhs.m_status_code), m_content(std::move(rhs.m_content)) {}
56+
http_response(int code, const std::string& content) : m_status_code(code), m_content(content) {}
57+
http_response(int code, std::string&& content) : m_status_code(code), m_content(std::move(content)) {}
58+
http_response(const http_response& rhs) : m_status_code(rhs.m_status_code), m_content(rhs.m_content) {}
4259

4360
http_response& operator=(const http_response& rhs)
4461
{
45-
status_code = rhs.status_code;
46-
content = rhs.content;
62+
m_status_code = rhs.m_status_code;
63+
m_content = rhs.m_content;
4764

4865
return *this;
4966
}
5067

5168
http_response& operator=(http_response&& rhs) noexcept
5269
{
53-
status_code = rhs.status_code;
54-
content = std::move(rhs.content);
70+
m_status_code = rhs.m_status_code;
71+
m_content = std::move(rhs.m_content);
5572

5673
return *this;
5774
}
5875

59-
int status_code = 0;
60-
std::string content;
76+
private:
77+
friend class negotiate;
78+
79+
int get_status_code() const;
80+
const std::string& get_content() const;
81+
82+
int m_status_code = 0;
83+
std::string m_content;
6184
};
6285

6386
class http_client

include/signalrclient/hub_connection.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <memory>
99
#include <functional>
1010
#include "connection_state.h"
11-
#include "trace_level.h"
11+
#include "log_level.h"
1212
#include "log_writer.h"
1313
#include "signalr_client_config.h"
1414
#include "signalr_value.h"
@@ -24,8 +24,6 @@ namespace signalr
2424
class hub_connection
2525
{
2626
public:
27-
typedef std::function<void __cdecl (const std::vector<signalr::value>&)> method_invoked_handler;
28-
2927
SIGNALRCLIENT_API ~hub_connection();
3028

3129
hub_connection(const hub_connection&) = delete;
@@ -36,17 +34,17 @@ namespace signalr
3634

3735
SIGNALRCLIENT_API hub_connection& operator=(hub_connection&&) noexcept;
3836

39-
SIGNALRCLIENT_API void __cdecl start(std::function<void(std::exception_ptr)> callback) noexcept;
40-
SIGNALRCLIENT_API void __cdecl stop(std::function<void(std::exception_ptr)> callback) noexcept;
37+
SIGNALRCLIENT_API void start(std::function<void(std::exception_ptr)> callback) noexcept;
38+
SIGNALRCLIENT_API void stop(std::function<void(std::exception_ptr)> callback) noexcept;
4139

42-
SIGNALRCLIENT_API connection_state __cdecl get_connection_state() const;
43-
SIGNALRCLIENT_API std::string __cdecl get_connection_id() const;
40+
SIGNALRCLIENT_API connection_state get_connection_state() const;
41+
SIGNALRCLIENT_API const std::string& get_connection_id() const;
4442

45-
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl(std::exception_ptr)>& disconnected_callback);
43+
SIGNALRCLIENT_API void on_disconnected(std::function<void (std::exception_ptr)> disconnected_callback);
4644

47-
SIGNALRCLIENT_API void __cdecl set_client_config(const signalr_client_config& config);
45+
//SIGNALRCLIENT_API void __cdecl set_client_config(const signalr_client_config& config);
4846

49-
SIGNALRCLIENT_API void __cdecl on(const std::string& event_name, const method_invoked_handler& handler);
47+
SIGNALRCLIENT_API void on(const std::string& event_name, std::function<void (const std::vector<signalr::value>&)> handler);
5048

5149
SIGNALRCLIENT_API void invoke(const std::string& method_name, const std::vector<signalr::value>& arguments = std::vector<signalr::value>(), std::function<void(const signalr::value&, std::exception_ptr)> callback = [](const signalr::value&, std::exception_ptr) {}) noexcept;
5250

@@ -55,10 +53,10 @@ namespace signalr
5553
private:
5654
friend class hub_connection_builder;
5755

58-
explicit hub_connection(const std::string& url, std::unique_ptr<hub_protocol>&& hub_protocol,
59-
trace_level trace_level = trace_level::info, std::shared_ptr<log_writer> log_writer = nullptr,
60-
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory = nullptr,
61-
std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory = nullptr,
56+
explicit hub_connection(const std::string& url, std::unique_ptr<hub_protocol>&& hub_protocol, signalr_client_config config,
57+
log_level log_level = log_level::info, std::shared_ptr<log_writer> log_writer = nullptr,
58+
std::function<std::unique_ptr<http_client>(const signalr_client_config&)> http_client_factory = nullptr,
59+
std::function<std::unique_ptr<websocket_client>(const signalr_client_config&)> websocket_factory = nullptr,
6260
bool skip_negotiation = false);
6361

6462
std::shared_ptr<hub_connection_impl> m_pImpl;

include/signalrclient/hub_connection_builder.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,38 @@ namespace signalr
1515
class hub_connection_builder final
1616
{
1717
public:
18-
SIGNALRCLIENT_API static hub_connection_builder create(const std::string& url);
18+
SIGNALRCLIENT_API hub_connection_builder(std::string url);
1919

2020
SIGNALRCLIENT_API ~hub_connection_builder();
2121

22-
SIGNALRCLIENT_API hub_connection_builder(const hub_connection_builder&);
22+
SIGNALRCLIENT_API hub_connection_builder(const hub_connection_builder&) = delete;
2323

2424
SIGNALRCLIENT_API hub_connection_builder(hub_connection_builder&&) noexcept;
2525

2626
SIGNALRCLIENT_API hub_connection_builder& operator=(hub_connection_builder&&) noexcept;
2727

28-
SIGNALRCLIENT_API hub_connection_builder& operator=(const hub_connection_builder&);
28+
SIGNALRCLIENT_API hub_connection_builder& operator=(const hub_connection_builder&) = delete;
2929

30-
SIGNALRCLIENT_API hub_connection_builder& with_logging(std::shared_ptr<log_writer> logger, trace_level log_level);
30+
SIGNALRCLIENT_API hub_connection_builder& configure_options(signalr_client_config config) noexcept;
3131

32-
SIGNALRCLIENT_API hub_connection_builder& with_websocket_factory(std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory);
32+
SIGNALRCLIENT_API hub_connection_builder& with_logging(std::shared_ptr<log_writer> logger, log_level log_level) noexcept;
3333

34-
SIGNALRCLIENT_API hub_connection_builder& with_http_client_factory(std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory);
34+
SIGNALRCLIENT_API hub_connection_builder& with_websocket_factory(std::function<std::unique_ptr<websocket_client>(const signalr_client_config&)> websocket_factory) noexcept;
3535

36-
SIGNALRCLIENT_API hub_connection_builder& skip_negotiation(bool skip = true);
36+
SIGNALRCLIENT_API hub_connection_builder& with_http_client_factory(std::function<std::unique_ptr<http_client>(const signalr_client_config&)> http_client_factory) noexcept;
37+
38+
SIGNALRCLIENT_API hub_connection_builder& skip_negotiation(bool skip = true) noexcept;
3739

3840
SIGNALRCLIENT_API hub_connection_builder& with_messagepack_hub_protocol();
3941

4042
SIGNALRCLIENT_API std::unique_ptr<hub_connection> build();
4143
private:
42-
hub_connection_builder(const std::string& url);
43-
4444
std::string m_url;
45+
signalr_client_config m_config;
4546
std::shared_ptr<log_writer> m_logger;
46-
trace_level m_log_level;
47-
std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> m_websocket_factory;
48-
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> m_http_client_factory;
47+
log_level m_log_level;
48+
std::function<std::unique_ptr<websocket_client>(const signalr_client_config&)> m_websocket_factory;
49+
std::function<std::unique_ptr<http_client>(const signalr_client_config&)> m_http_client_factory;
4950
bool m_skip_negotiation = false;
5051
bool m_use_messagepack = false;
5152

include/signalrclient/trace_level.h renamed to include/signalrclient/log_level.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace signalr
88
{
9-
enum class trace_level : int
9+
enum class log_level
1010
{
1111
verbose = 0,
1212
debug = 1,

include/signalrclient/scheduler.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
namespace signalr
1212
{
13-
typedef std::function<void()> signalr_base_cb;
14-
1513
struct scheduler
1614
{
17-
virtual void schedule(const signalr_base_cb& cb, std::chrono::milliseconds delay = std::chrono::milliseconds::zero()) = 0;
15+
virtual void schedule(std::function<void()> cb, std::chrono::milliseconds delay = std::chrono::milliseconds::zero()) = 0;
1816

1917
virtual ~scheduler() {}
2018
};

include/signalrclient/signalr_client_config.h

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,15 @@
2020

2121
namespace signalr
2222
{
23-
class signalr_client_config
23+
class signalr_client_config final
2424
{
2525
public:
26-
#ifdef USE_CPPRESTSDK
27-
SIGNALRCLIENT_API void __cdecl set_proxy(const web::web_proxy &proxy);
28-
// Please note that setting credentials does not work in all cases.
29-
// For example, Basic Authentication fails under Win32.
30-
// As a workaround, you can set the required authorization headers directly
31-
// using signalr_client_config::set_http_headers
32-
SIGNALRCLIENT_API void __cdecl set_credentials(const web::credentials &credentials);
33-
34-
SIGNALRCLIENT_API web::http::client::http_client_config __cdecl get_http_client_config() const;
35-
SIGNALRCLIENT_API void __cdecl set_http_client_config(const web::http::client::http_client_config& http_client_config);
36-
37-
SIGNALRCLIENT_API web::websockets::client::websocket_client_config __cdecl get_websocket_client_config() const noexcept;
38-
SIGNALRCLIENT_API void __cdecl set_websocket_client_config(const web::websockets::client::websocket_client_config& websocket_client_config);
39-
#endif
40-
4126
SIGNALRCLIENT_API signalr_client_config();
4227

43-
SIGNALRCLIENT_API const std::map<std::string, std::string>& __cdecl get_http_headers() const noexcept;
44-
SIGNALRCLIENT_API std::map<std::string, std::string>& __cdecl get_http_headers() noexcept;
45-
SIGNALRCLIENT_API void __cdecl set_http_headers(const std::map<std::string, std::string>& http_headers);
46-
SIGNALRCLIENT_API void __cdecl set_scheduler(std::shared_ptr<scheduler> scheduler);
47-
SIGNALRCLIENT_API const std::shared_ptr<scheduler>& __cdecl get_scheduler() const noexcept;
28+
SIGNALRCLIENT_API const std::map<std::string, std::string>& get_http_headers() const noexcept;
29+
SIGNALRCLIENT_API void set_http_headers(std::map<std::string, std::string> http_headers) noexcept;
30+
SIGNALRCLIENT_API void set_scheduler(std::shared_ptr<scheduler> scheduler) noexcept;
31+
SIGNALRCLIENT_API std::shared_ptr<scheduler> get_scheduler() const noexcept;
4832
SIGNALRCLIENT_API void set_handshake_timeout(std::chrono::milliseconds);
4933
SIGNALRCLIENT_API std::chrono::milliseconds get_handshake_timeout() const noexcept;
5034
SIGNALRCLIENT_API void set_server_timeout(std::chrono::milliseconds);
@@ -53,10 +37,6 @@ namespace signalr
5337
SIGNALRCLIENT_API std::chrono::milliseconds get_keepalive_interval() const noexcept;
5438

5539
private:
56-
#ifdef USE_CPPRESTSDK
57-
web::http::client::http_client_config m_http_client_config;
58-
web::websockets::client::websocket_client_config m_websocket_client_config;
59-
#endif
6040
std::map<std::string, std::string> m_http_headers;
6141
std::shared_ptr<scheduler> m_scheduler;
6242
std::chrono::milliseconds m_handshake_timeout;

include/signalrclient/signalr_value.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212

1313
namespace signalr
1414
{
15-
/**
16-
* An enum defining the types a signalr::value may be.
17-
*/
18-
enum class value_type
19-
{
20-
map,
21-
array,
22-
string,
23-
float64,
24-
null,
25-
boolean,
26-
binary
27-
};
28-
2915
/**
3016
* Represents a value to be provided to a SignalR method as a parameter, or returned as a return value.
3117
*/
3218
class value
3319
{
3420
public:
21+
/**
22+
* An enum defining the types a signalr::value may be.
23+
*/
24+
enum class type
25+
{
26+
map,
27+
array,
28+
string,
29+
float64,
30+
null,
31+
boolean,
32+
binary
33+
};
34+
3535
/**
3636
* Create an object representing a value_type::null value.
3737
*/
@@ -45,7 +45,7 @@ namespace signalr
4545
/**
4646
* Create an object representing a default value for the given value_type.
4747
*/
48-
SIGNALRCLIENT_API value(value_type t);
48+
SIGNALRCLIENT_API value(type t);
4949

5050
/**
5151
* Create an object representing a value_type::boolean with the given bool value.
@@ -200,10 +200,10 @@ namespace signalr
200200
/**
201201
* Returns the signalr::type that represents the stored object.
202202
*/
203-
SIGNALRCLIENT_API value_type type() const;
203+
SIGNALRCLIENT_API type type() const;
204204

205205
private:
206-
value_type mType;
206+
enum type mType;
207207

208208
union storage
209209
{

samples/HubConnectionSample/HubConnectionSample.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class logger : public signalr::log_writer
1818
}
1919
};
2020

21-
void send_message(signalr::hub_connection& connection, const std::string& message)
21+
void send_message(const std::unique_ptr<signalr::hub_connection>& connection, const std::string& message)
2222
{
2323
std::vector<signalr::value> args { std::string("c++"), message };
2424

2525
// if you get an internal compiler error uncomment the lambda below or install VS Update 4
26-
connection.invoke("SendMessage", args, [](const signalr::value& value, std::exception_ptr exception)
26+
connection->invoke("SendMessage", args, [](const signalr::value& value, std::exception_ptr exception)
2727
{
2828
try
2929
{
@@ -50,17 +50,17 @@ void send_message(signalr::hub_connection& connection, const std::string& messag
5050

5151
void chat()
5252
{
53-
signalr::hub_connection connection = signalr::hub_connection_builder::create("http://localhost:5000/default")
54-
.with_logging(std::make_shared <logger>(), signalr::trace_level::verbose)
53+
std::unique_ptr<signalr::hub_connection> connection = signalr::hub_connection_builder("http://localhost:61633/default")
54+
.with_logging(std::make_shared <logger>(), signalr::log_level::verbose)
5555
.build();
5656

57-
connection.on("ReceiveMessage", [](const std::vector<signalr::value>& m)
57+
connection->on("ReceiveMessage", [](const std::vector<signalr::value>& m)
5858
{
5959
std::cout << std::endl << m[0].as_string() << std::endl << "Enter your message: ";
6060
});
6161

6262
std::promise<void> startTask;
63-
connection.start([&connection, &startTask](std::exception_ptr exception)
63+
connection->start([&connection, &startTask](std::exception_ptr exception)
6464
{
6565
if (exception)
6666
{
@@ -79,12 +79,12 @@ void chat()
7979
startTask.get_future().get();
8080

8181
std::cout << "Enter your message:";
82-
while (connection.get_connection_state() == signalr::connection_state::connected)
82+
while (connection->get_connection_state() == signalr::connection_state::connected)
8383
{
8484
std::string message;
8585
std::getline(std::cin, message);
8686

87-
if (message == ":q" || connection.get_connection_state() != signalr::connection_state::connected)
87+
if (message == ":q" || connection->get_connection_state() != signalr::connection_state::connected)
8888
{
8989
break;
9090
}
@@ -93,7 +93,7 @@ void chat()
9393
}
9494

9595
std::promise<void> stopTask;
96-
connection.stop([&stopTask](std::exception_ptr exception)
96+
connection->stop([&stopTask](std::exception_ptr exception)
9797
{
9898
try
9999
{

src/signalrclient/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set (SOURCES
66
default_http_client.cpp
77
default_websocket_client.cpp
88
handshake_protocol.cpp
9+
http_utilities.cpp
910
hub_connection.cpp
1011
hub_connection_builder.cpp
1112
hub_connection_impl.cpp

0 commit comments

Comments
 (0)