Skip to content

Commit

Permalink
client wrongly offers json & msgpack during connect, issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenjs committed Dec 24, 2017
1 parent 0a4266d commit c09a2b4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ unreleased
generated Makefile, causing break when configure done inside source tree
(issue #3, @sashakh)

- rawsocket client wrongly offers multiple serialisers on connect (issue #18)

version 1.5
===========

Expand Down
20 changes: 9 additions & 11 deletions examples/basic/demo_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
#include <memory>
#include <iostream>

using namespace wampcc;

int main(int argc, char** argv)
{
try {
/* Create the wampcc kernel. */

kernel the_kernel;
wampcc::kernel the_kernel;

/* Create the TCP socket and attempt to connect. */

std::unique_ptr<tcp_socket> socket(new tcp_socket(&the_kernel));
std::unique_ptr<wampcc::tcp_socket> socket(new wampcc::tcp_socket(&the_kernel));
socket->connect("127.0.0.1", 55555).wait_for(std::chrono::seconds(3));

if (!socket->is_connected())
Expand All @@ -31,8 +29,8 @@ int main(int argc, char** argv)
/* With the connected socket, create a wamp session & logon to the realm
* called 'default_realm'. */

auto session = wamp_session::create<websocket_protocol>(&the_kernel,
std::move(socket));
auto session = wampcc::wamp_session::create<wampcc::websocket_protocol>(&the_kernel,
std::move(socket));

session->hello("default_realm").wait_for(std::chrono::seconds(3));

Expand All @@ -42,11 +40,11 @@ int main(int argc, char** argv)
/* Subscribe to a topic. */

session->subscribe("random_number", {},
[](wamp_session&, subscribed_info info) {
[](wampcc::wamp_session&, wampcc::subscribed_info info) {
std::cout << "subscribed " << (info ? "ok" : "failed")
<< std::endl;
},
[](wamp_session&, event_info info) {
[](wampcc::wamp_session&, wampcc::event_info info) {
for (auto& x : info.args.args_list)
std::cout << "got update: " << x << " ";
std::cout << std::endl;
Expand All @@ -55,15 +53,15 @@ int main(int argc, char** argv)
/* Register a procedure that can sum an array of numbers. */

session->provide("math.service.add", {},
[](wamp_session&, registered_info info) {
[](wampcc::wamp_session&, wampcc::registered_info info) {
if (info)
std::cout << "procedure registered with id "
<< info.registration_id << std::endl;
else
std::cout << "procedure registration failed, error "
<< info.error_uri << std::endl;
},
[](wamp_session& ws, invocation_info info) {
[](wampcc::wamp_session& ws, wampcc::invocation_info info) {
int total = 0;
for (auto& item : info.args.args_list)
if (item.is_int())
Expand All @@ -74,7 +72,7 @@ int main(int argc, char** argv)
/* Call a remote procedure. */

session->call("math.service.add", {}, {{100, 200}, {}},
[](wamp_session&, result_info result) {
[](wampcc::wamp_session&, wampcc::result_info result) {
if (result)
std::cout << "got result: " << result.args.args_list[0] << std::endl;
});
Expand Down
4 changes: 4 additions & 0 deletions include/wampcc/rawsocket_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class rawsocket_protocol : public protocol
struct options : public protocol::options
{
max_msg_size_flag inbound_max_msg_size;

/** Default serialiser for client initiated connection */
static const int default_client_serialiser = static_cast<int>(serialiser_type::json);

options()
: protocol::options(),
inbound_max_msg_size(rawsocket_protocol::default_max_rxmsg_size)
Expand Down
4 changes: 4 additions & 0 deletions include/wampcc/websocket_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class websocket_protocol : public protocol
public:

struct options : public protocol::options {

/** Default serialiser for client initiated connection */
static const int default_client_serialiser = all_serialisers;

options(std::string __request_uri = "/")
: host_header(host_header_mode::automatic),
request_uri(std::move(__request_uri)) { }
Expand Down
8 changes: 7 additions & 1 deletion libs/wampcc/rawsocket_protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ void rawsocket_protocol::initiate(t_initiate_cb cb)
codecs |= (m_options.serialisers & serialiser_type::json)? e_JSON : 0;
codecs |= (m_options.serialisers & serialiser_type::msgpack)? e_MSGPACK : 0;

/* During rawsocket client initiation the client cannot advertise more than
* one protocol (like websocket is able to do). So reject attempt if the
* protocol is not uniquely specified.*/
if (codecs != e_JSON && codecs != e_MSGPACK)
throw std::runtime_error("rawsocket client must choose only one serialiser");

format_handshake( handshake, m_options.inbound_max_msg_size, codecs);

std::pair<const char*, size_t> buf;
Expand Down Expand Up @@ -163,7 +169,7 @@ void rawsocket_protocol::io_on_read(char* src, size_t len)
os << " (" << err_str << ")";
throw handshake_error(os.str());
}

create_codec(m_options.serialisers & to_serialiser(serializer));
if (!m_codec)
throw handshake_error("failed to negotiate rawsocket message serialiser");
Expand Down
11 changes: 6 additions & 5 deletions tests/wampcc/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class internal_server
m_salt{"saltxx",32, 1500}
{
std::random_device rd; // used for seed
std::mt19937 gen(rd());
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(-1000, 1000);

m_salt.iterations += dis(gen);
Expand Down Expand Up @@ -277,7 +277,7 @@ std::unique_ptr<tcp_socket> tcp_connect(kernel& k, int port)
std::shared_ptr<wamp_session> establish_session(
std::unique_ptr<kernel>& the_kernel, int port,
int protocols = wampcc::all_protocols,
int serialisers = wampcc::all_serialisers)
int serialisers = static_cast<int>(serialiser_type::none))
{
static int count = 0;
count++;
Expand All @@ -295,11 +295,12 @@ std::shared_ptr<wamp_session> establish_session(
throw std::runtime_error("tcp connect failed during establish_session()");

websocket_protocol::options ws_opts;
ws_opts.serialisers = serialisers;
ws_opts.serialisers = (serialisers==static_cast<int>(serialiser_type::none))?
websocket_protocol::options::default_client_serialiser : serialisers;

rawsocket_protocol::options rs_opts;
rs_opts.serialisers = serialisers;

rs_opts.serialisers = (serialisers==static_cast<int>(serialiser_type::none))?
rawsocket_protocol::options::default_client_serialiser : serialisers;

/* attempt to create a session */
std::shared_ptr<wamp_session> session;
Expand Down
16 changes: 13 additions & 3 deletions utils/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ struct user_options
websocket,
} session_transport = transport::websocket;

int serialisers = wampcc::all_serialisers;
/* We cannot define a general purpose default serialiser for a wamp client to
* use because the concept of a default varies between WAMP transports. */
int serialisers = static_cast<int>(wampcc::serialiser_type::none);

wampcc::user_optional<std::string> username;
wampcc::user_optional<std::string> password;
Expand All @@ -52,6 +54,14 @@ struct user_options
bool use_ssl = false;

std::string request_uri_path = "/";

/* Get actual client serialiser to use, which depends on what options the user
* has provided and the default for the given transport */
template<typename T>
int get_serialiser() {
return (serialisers == static_cast<int>(wampcc::serialiser_type::none))?
T::options::default_client_serialiser : serialisers;
}
} uopts;


Expand Down Expand Up @@ -447,7 +457,7 @@ int main_impl(int argc, char** argv)
switch (uopts.session_transport) {
case user_options::transport::websocket: {
wampcc::websocket_protocol::options proto_opts(uopts.request_uri_path);
proto_opts.serialisers = uopts.serialisers;
proto_opts.serialisers = uopts.get_serialiser<wampcc::websocket_protocol>();
ws = wampcc::wamp_session::create<wampcc::websocket_protocol>(
g_kernel.get(),
std::move(sock),
Expand All @@ -458,7 +468,7 @@ int main_impl(int argc, char** argv)
}
case user_options::transport::rawsocket: {
wampcc::rawsocket_protocol::options proto_opts;
proto_opts.serialisers = uopts.serialisers;
proto_opts.serialisers = uopts.get_serialiser<wampcc::rawsocket_protocol>();
ws = wampcc::wamp_session::create<wampcc::rawsocket_protocol>(
g_kernel.get(),
std::move(sock),
Expand Down

0 comments on commit c09a2b4

Please sign in to comment.