Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New additional asynchronous mechanism #203

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions autobahn/wamp_async.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) Crossbar.io Technologies GmbH and contributors
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////

#ifndef AUTOBAHN_WAMP_ASYNC_HPP
#define AUTOBAHN_WAMP_ASYNC_HPP

#include "boost_config.hpp"

#include <boost/exception_ptr.hpp>
#include <boost/variant.hpp>
#include <functional>

namespace autobahn {

/// Async mechanism, either a promise or handlers.
template <typename T>
class wamp_async
{
public:
using on_success_handler = std::function<void(const T &)>;
using on_exception_handler = std::function<void(const boost::exception_ptr &)>;

/// Use promise
wamp_async();

/// Use handlers
wamp_async(on_success_handler && on_sucess,
on_exception_handler && on_exception);

void set_value(const T &);
void set_value(T &&);
void set_exception(const boost::exception_ptr &);

bool is_promise() const;
boost::promise<T>& promise();
on_success_handler& on_success();
on_exception_handler& on_exception();

private:
using pair_type = std::pair< on_success_handler, on_exception_handler >;

boost::variant< boost::promise<T>, pair_type > m_async;
};

/// Async mechanism, either a promise or handlers, void specialization.
template <>
class wamp_async<void>
{
public:
using on_success_handler = std::function<void()>;
using on_exception_handler = std::function<void(const boost::exception_ptr &)>;

/// Use promise
wamp_async();

/// Use handlers
wamp_async(on_success_handler && on_sucess,
on_exception_handler && on_exception);

void set_value();
void set_exception(const boost::exception_ptr &);

bool is_promise() const;
boost::promise<void>& promise();
on_success_handler& on_success();
on_exception_handler& on_exception();

private:
using pair_type = std::pair< on_success_handler, on_exception_handler >;

boost::variant< boost::promise<void>, pair_type > m_async;
};

} // namespace autobahn

#include "wamp_async.ipp"

#endif // AUTOBAHN_WAMP_ASYNC_HPP
154 changes: 154 additions & 0 deletions autobahn/wamp_async.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) Crossbar.io Technologies GmbH and contributors
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////

namespace autobahn {

template <typename T>
inline wamp_async<T>::wamp_async()
: m_async(boost::promise<T>())
{
}

template <typename T>
inline wamp_async<T>::wamp_async(on_success_handler && on_success,
on_exception_handler && on_exception)
: m_async(pair_type({std::move(on_success), std::move(on_exception)}))
{
}

template <typename T>
inline void wamp_async<T>::set_value(const T & t)
{
if (is_promise()) {
promise().set_value(t);
}
else if (on_success()) {
on_success()(t);
}
}

template <typename T>
inline void wamp_async<T>::set_value(T && t)
{
if (is_promise()) {
promise().set_value(std::forward<T>(t));
}
else if (on_success()) {
on_success()(std::forward<T>(t));
}
}

template <typename T>
inline void wamp_async<T>::set_exception(const boost::exception_ptr & eptr)
{
if (is_promise()) {
promise().set_exception(eptr);
}
else if (on_exception()) {
on_exception()(eptr);
}
}

template <typename T>
inline bool wamp_async<T>::is_promise() const
{
return m_async.which() == 0;
}

template <typename T>
inline boost::promise<T>& wamp_async<T>::promise()
{
return boost::get<boost::promise<T>>(m_async);
}

template <typename T>
inline typename wamp_async<T>::on_success_handler& wamp_async<T>::on_success()
{
return boost::get<pair_type>(m_async).first;
}

template <typename T>
inline typename wamp_async<T>::on_exception_handler& wamp_async<T>::on_exception()
{
return boost::get<pair_type>(m_async).second;
}

inline wamp_async<void>::wamp_async()
: m_async(boost::promise<void>())
{
}

inline wamp_async<void>::wamp_async(on_success_handler && on_success,
on_exception_handler && on_exception)
: m_async(pair_type({std::move(on_success), std::move(on_exception)}))
{
}

inline void wamp_async<void>::set_value()
{
if (is_promise()) {
promise().set_value();
}
else if (on_success()) {
on_success()();
}
}

inline void wamp_async<void>::set_exception(const boost::exception_ptr & eptr)
{
if (is_promise()) {
promise().set_exception(eptr);
}
else if (on_exception()) {
on_exception()(eptr);
}
}

inline bool wamp_async<void>::is_promise() const
{
return m_async.which() == 0;
}

inline boost::promise<void>& wamp_async<void>::promise()
{
return boost::get<boost::promise<void>>(m_async);
}

inline wamp_async<void>::on_success_handler& wamp_async<void>::on_success()
{
return boost::get<pair_type>(m_async).first;
}

inline wamp_async<void>::on_exception_handler& wamp_async<void>::on_exception()
{
return boost::get<pair_type>(m_async).second;
}

} // namespace autobahn
12 changes: 9 additions & 3 deletions autobahn/wamp_call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@
#ifndef AUTOBAHN_WAMP_CALL_HPP
#define AUTOBAHN_WAMP_CALL_HPP

#include "wamp_call_result.hpp"
#include "boost_config.hpp"
#include "wamp_async.hpp"
#include "wamp_call_result.hpp"

namespace autobahn {

/// An outstanding wamp call.
class wamp_call
{
public:
using on_success_handler = wamp_async<wamp_call_result>::on_success_handler;
using on_exception_handler = wamp_async<wamp_call_result>::on_exception_handler;

wamp_call();
wamp_call(on_success_handler&& on_success,
on_exception_handler&& on_exception);

boost::promise<wamp_call_result>& result();
wamp_async<wamp_call_result>& result();
void set_result(wamp_call_result&& value);

private:
boost::promise<wamp_call_result> m_result;
wamp_async<wamp_call_result> m_result;
};

} // namespace autobahn
Expand Down
8 changes: 7 additions & 1 deletion autobahn/wamp_call.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ inline wamp_call::wamp_call()
{
}

inline boost::promise<wamp_call_result>& wamp_call::result()
inline wamp_call::wamp_call(on_success_handler&& on_success,
on_exception_handler&& on_exception)
: m_result(std::move(on_success), std::move(on_exception))
{
}

inline wamp_async<wamp_call_result>& wamp_call::result()
{
return m_result;
}
Expand Down
32 changes: 28 additions & 4 deletions autobahn/wamp_rawsocket_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,25 @@ class wamp_rawsocket_transport :
*/
virtual boost::future<void> connect() override;

/*!
* @copydoc wamp_transport::connect(on_success_handler&& on_success,
on_exception_handler&& on_exception)
*/
virtual void connect(on_success_handler&& on_success,
on_exception_handler&& on_exception) override;

/*!
* @copydoc wamp_transport::disconnect()
*/
virtual boost::future<void> disconnect() override;

/*!
* @copydoc wamp_transport::disconnect(on_success_handler&& on_success,
on_exception_handler&& on_exception)
*/
virtual void disconnect(on_success_handler&& on_success,
on_exception_handler&& on_exception) override;

/*!
* @copydoc wamp_transport::is_connected()
*/
Expand Down Expand Up @@ -149,11 +163,21 @@ class wamp_rawsocket_transport :
*/
virtual bool has_handler() const override;

protected:

wamp_async<void>& connect_async() const;

wamp_async<void>& disconnect_async() const;

protected:
socket_type& socket();

private:

void do_connect();

void do_disconnect();

void handshake_reply_handler(
const boost::system::error_code& error_code,
std::size_t /* bytes_transferred */);
Expand All @@ -180,14 +204,14 @@ class wamp_rawsocket_transport :
endpoint_type m_remote_endpoint;

/*!
* The promise that is fulfilled when the connect attempt is complete.
* The async operation that is fulfilled when the connect attempt is complete.
*/
boost::promise<void> m_connect;
wamp_async<void> m_connect;

/*!
* The promise that is fulfilled when the disconnect attempt is complete.
* The async operation that is fulfilled when the disconnect attempt is complete.
*/
boost::promise<void> m_disconnect;
wamp_async<void> m_disconnect;

/*!
* The handler to be called when pausing.
Expand Down
Loading