Binance C++ Connector 2.0.0
Official C++ connectors for the Binance APIs
Loading...
Searching...
No Matches
binance::common::WebsocketStreamHandle Class Reference

One subscribed stream (WebSocket streams) or user-data subscription (WebSocket API), with untyped callbacks. More...

#include <websocket_stream.hpp>

Public Member Functions

 WebsocketStreamHandle ()=default
 WebsocketStreamHandle (std::string stream, std::shared_ptr< detail::StreamBinding > binding)
 WebsocketStreamHandle (WebsocketStreamHandle &&) noexcept=default
WebsocketStreamHandle & operator= (WebsocketStreamHandle &&other) noexcept
 WebsocketStreamHandle (const WebsocketStreamHandle &)=delete
WebsocketStreamHandle & operator= (const WebsocketStreamHandle &)=delete
 ~WebsocketStreamHandle ()
const std::string & stream () const noexcept
 The stream name, or the subscription's id for user data.
template<class F>
void on_message (F &&callback)
 Adds a callback.
void unsubscribe () noexcept
 Removes this handle's callbacks and, for WebSocket streams, unsubscribes from the stream once no other handle uses it.
void release () noexcept
 Detaches this handle: the subscription and its callbacks stay until the client is destroyed, and the handle becomes empty.
template<class CompletionToken>
auto async_wait_subscribed (CompletionToken &&token)
 Completes once the server has acknowledged the subscription (see WebsocketStreams::async_wait_subscribed), at once for a user-data subscription.
void wait_subscribed (std::optional< std::chrono::milliseconds > timeout={})

Detailed Description

One subscribed stream (WebSocket streams) or user-data subscription (WebSocket API), with untyped callbacks.

Move-only: destroying it unsubscribes, like unsubscribe(); release() keeps the subscription and its callbacks for the client's lifetime. on_message and unsubscribe are thread-safe.

Payloads received before the first callback is added are kept (up to 1000) and delivered to it. A default-constructed handle is empty: its calls do nothing.

Constructor & Destructor Documentation

◆ WebsocketStreamHandle() [1/4]

binance::common::WebsocketStreamHandle::WebsocketStreamHandle ( )
default

◆ WebsocketStreamHandle() [2/4]

binance::common::WebsocketStreamHandle::WebsocketStreamHandle ( std::string stream,
std::shared_ptr< detail::StreamBinding > binding )

◆ WebsocketStreamHandle() [3/4]

binance::common::WebsocketStreamHandle::WebsocketStreamHandle ( WebsocketStreamHandle && )
defaultnoexcept

◆ WebsocketStreamHandle() [4/4]

binance::common::WebsocketStreamHandle::WebsocketStreamHandle ( const WebsocketStreamHandle & )
delete

◆ ~WebsocketStreamHandle()

binance::common::WebsocketStreamHandle::~WebsocketStreamHandle ( )

Member Function Documentation

◆ operator=() [1/2]

WebsocketStreamHandle & binance::common::WebsocketStreamHandle::operator= ( WebsocketStreamHandle && other)
noexcept

◆ operator=() [2/2]

WebsocketStreamHandle & binance::common::WebsocketStreamHandle::operator= ( const WebsocketStreamHandle & )
delete

◆ stream()

const std::string & binance::common::WebsocketStreamHandle::stream ( ) const
nodiscardnoexcept

The stream name, or the subscription's id for user data.

◆ on_message()

template<class F>
void binance::common::WebsocketStreamHandle::on_message ( F && callback)
inline

Adds a callback.

It runs on its associated executor if it has one (see boost::asio::bind_executor), else on the client's executor, so it should not block. Exceptions it throws are logged. No callback starts once unsubscribe() or the client's destructor has returned. The destructor waits for callbacks running on the client's executor, but not for those running on their own.

◆ unsubscribe()

void binance::common::WebsocketStreamHandle::unsubscribe ( )
noexcept

Removes this handle's callbacks and, for WebSocket streams, unsubscribes from the stream once no other handle uses it.

A callback running on another thread of the client's executor is waited for (one on its own executor is not). A user-data subscription is ended on the server (see WebsocketApi::async_subscribe).

◆ release()

void binance::common::WebsocketStreamHandle::release ( )
noexcept

Detaches this handle: the subscription and its callbacks stay until the client is destroyed, and the handle becomes empty.

◆ async_wait_subscribed()

template<class CompletionToken>
auto binance::common::WebsocketStreamHandle::async_wait_subscribed ( CompletionToken && token)
inline

Completes once the server has acknowledged the subscription (see WebsocketStreams::async_wait_subscribed), at once for a user-data subscription.

Fails with WebsocketCancelledError if the handle is empty or unsubscribed, or the client is gone. wait_subscribed blocks and throws WebsocketTimeoutError once timeout passes, if given.

◆ wait_subscribed()

void binance::common::WebsocketStreamHandle::wait_subscribed ( std::optional< std::chrono::milliseconds > timeout = {})