CppWAMP
C++11 client library for the WAMP protocol
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
peerdata.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_PEERDATA_HPP
8 #define CPPWAMP_PEERDATA_HPP
9 
10 #include <cassert>
11 #include <chrono>
12 #include <initializer_list>
13 #include <memory>
14 #include <set>
15 #include <string>
16 #include <vector>
17 #include "api.hpp"
18 #include "asiodefs.hpp"
19 #include "config.hpp"
20 #include "options.hpp"
21 #include "payload.hpp"
22 #include "variant.hpp"
23 #include "wampdefs.hpp"
24 #include "./internal/passkey.hpp"
25 
26 //------------------------------------------------------------------------------
30 //------------------------------------------------------------------------------
31 
32 namespace wamp
33 {
34 
35 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 class CPPWAMP_API Abort : public Options<Abort, internal::AbortMessage>
40 {
41 public:
43  Abort(String uri = "");
44 
46  const String& uri() const;
47 
48 private:
50 
51 public:
52  // Internal use only
53  Abort(internal::PassKey, internal::AbortMessage&& msg);
54 };
55 
56 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
59 class CPPWAMP_API Realm : public Options<Realm, internal::HelloMessage>
60 {
61 public:
63  Realm(String uri);
64 
66  const String& uri() const;
67 
70  Realm& captureAbort(Abort& abort);
71 
78  Realm& withAuthMethods(std::vector<String> methods);
79 
81  Realm& withAuthId(String authId);
83 
84 private:
86 
87  Abort* abort_ = nullptr;
88 
89 public:
90  Abort* abort(internal::PassKey); // Internal use only
91 };
92 
93 
94 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
97 class CPPWAMP_API SessionInfo : public Options<SessionInfo,
98  internal::WelcomeMessage>
99 {
100 public:
102  using RoleSet = std::set<String>;
103 
105  using FeatureSet = std::set<String>;
106 
108  using FeatureMap = std::map<String, FeatureSet>;
109 
111  SessionInfo();
112 
114  SessionId id() const;
115 
117  const String& realm() const;
118 
125  String agentString() const;
127 
134  Object roles() const;
135 
137  bool supportsRoles(const RoleSet& roles) const;
138 
140  bool supportsFeatures(const FeatureMap& features) const;
142 
150  Variant authId() const;
151 
153  Variant authRole() const;
154 
156  Variant authMethod() const;
157 
159  Variant authProvider() const;
161 
162 private:
164 
165  String realm_;
166 
167 public:
168  // Internal use only
169  SessionInfo(internal::PassKey, String&& realm,
170  internal::WelcomeMessage&& msg);
171 };
172 
173 CPPWAMP_API std::ostream& operator<<(std::ostream& out,
174  const SessionInfo& info);
175 
176 
177 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 class CPPWAMP_API Reason : public Options<Reason, internal::GoodbyeMessage>
182 {
183 public:
185  Reason(String uri = "");
186 
188  const String& uri() const;
189 
190 private:
192 
193 public:
194  // Internal use only
195  Reason(internal::PassKey, internal::GoodbyeMessage&& msg);
196 };
197 
198 //------------------------------------------------------------------------------
204 //------------------------------------------------------------------------------
205 class CPPWAMP_API Authentication : public Options<Authentication,
206  internal::AuthenticateMessage>
207 {
208 public:
210  Authentication();
211 
213  Authentication(String signature);
214 
216  const String& signature() const;
217 
220  Authentication& withNonce(std::string nonce);
221 
224  Authentication& withChannelBinding(std::string type, std::string data);
225 
226 private:
228 };
229 
230 
231 namespace internal { class Challengee; } // Forward declaration
232 
233 //------------------------------------------------------------------------------
239 //------------------------------------------------------------------------------
240 class CPPWAMP_API Challenge : public Options<Challenge,
241  internal::ChallengeMessage>
242 {
243 public:
245  Challenge();
246 
249  bool challengeeHasExpired() const;
250 
252  const String& method() const;
253 
255  Variant challenge() const;
256 
258  Variant salt() const;
259 
261  Variant keyLength() const;
262 
264  Variant iterations() const;
265 
267  Variant kdf() const;
268 
270  Variant memory() const;
271 
273  void authenticate(Authentication auth);
274 
275 public:
276  // Internal use only
277  using ChallengeePtr = std::weak_ptr<internal::Challengee>;
278  Challenge(internal::PassKey, ChallengeePtr challengee,
279  internal::ChallengeMessage&& msg);
280 
281 private:
283 
284  ChallengeePtr challengee_;
285 };
286 
287 
288 //------------------------------------------------------------------------------
291 //------------------------------------------------------------------------------
292 class CPPWAMP_API Error : public Payload<Error, internal::ErrorMessage>
293 {
294 public:
296  Error();
297 
299  Error(String reason);
300 
303  explicit Error(const error::BadType& e);
304 
306  virtual ~Error();
307 
309  explicit operator bool() const;
310 
312  const String& reason() const;
313 
314 private:
316 
317 public:
318  // Internal use only
319  Error(internal::PassKey, internal::ErrorMessage&& msg);
320 
321  internal::ErrorMessage& errorMessage(internal::PassKey,
322  internal::WampMsgType reqType,
323  RequestId reqId);
324 };
325 
326 
327 //------------------------------------------------------------------------------
330 //------------------------------------------------------------------------------
331 class CPPWAMP_API Topic : public Options<Topic, internal::SubscribeMessage>
332 {
333 public:
335  Topic(String uri);
336 
344  Topic& usingPrefixMatch();
345 
348  Topic& usingWildcardMatch();
350 
352  const String& uri() const;
353 
354 private:
356 };
357 
358 
359 //------------------------------------------------------------------------------
362 //------------------------------------------------------------------------------
363 class CPPWAMP_API Pub : public Payload<Pub, internal::PublishMessage>
364 {
365 public:
367  Pub(String topic);
368 
375  const String& topic();
376 
379  Pub& withExcludedSessions(Array sessionIds);
380 
382  Pub& withExcludedAuthIds(Array authIds);
383 
385  Pub& withExcludedAuthRoles(Array authRoles);
386 
389  Pub& withEligibleSessions(Array sessionIds);
390 
392  Pub& withEligibleAuthIds(Array authIds);
393 
395  Pub& withEligibleAuthRoles(Array authRoles);
397 
405  Pub& withExcludeMe(bool excluded = true);
407 
415  Pub& withDiscloseMe(bool disclosed = true);
417 
418 private:
420 };
421 
422 
423 //------------------------------------------------------------------------------
426 //------------------------------------------------------------------------------
427 class CPPWAMP_API Event : public Payload<Event, internal::EventMessage>
428 {
429 public:
431  Event();
432 
435  bool empty() const;
436 
438  SubscriptionId subId() const;
439 
441  PublicationId pubId() const;
442 
444  AnyIoExecutor executor() const;
445 
452  Variant publisher() const;
454 
461  Variant trustLevel() const;
463 
471  Variant topic() const;
473 
474 private:
476 
477  AnyIoExecutor executor_;
478 
479 public:
480  // Internal use only
481  Event(internal::PassKey, AnyIoExecutor executor,
482  internal::EventMessage&& msg);
483 };
484 
485 CPPWAMP_API std::ostream& operator<<(std::ostream& out, const Event& event);
486 
487 
488 //------------------------------------------------------------------------------
491 //------------------------------------------------------------------------------
492 class CPPWAMP_API Procedure : public Options<Procedure,
493  internal::RegisterMessage>
494 {
495 public:
497  Procedure(String uri);
498 
500  const String& uri() const;
501 
509  Procedure& usingPrefixMatch();
510 
513  Procedure& usingWildcardMatch();
515 
523  Procedure& withDiscloseCaller(bool disclosed = true);
525 
526 private:
528 };
529 
530 
531 //------------------------------------------------------------------------------
534 //------------------------------------------------------------------------------
535 class CPPWAMP_API Rpc : public Payload<Rpc, internal::CallMessage>
536 {
537 public:
539  using CallerTimeoutDuration = std::chrono::steady_clock::duration;
540 
541  static constexpr CallCancelMode defaultCancelMode() noexcept
542  {
543  return CallCancelMode::kill;
544  }
545 
547  Rpc(String uri);
548 
550  const String& procedure() const;
551 
554  Rpc& captureError(Error& error);
555 
562  Rpc& withProgressiveResults(bool enabled = true);
563 
565  bool progressiveResultsAreEnabled() const;
567 
576  Rpc& withDealerTimeout(UInt milliseconds);
577 
580  template <typename R, typename P>
581  Rpc& withDealerTimeout(std::chrono::duration<R, P> timeout)
582  {
583  using namespace std::chrono;
584  auto ms = duration_cast<milliseconds>(timeout).count();
585  return withDealerTimeout(static_cast<Int>(ms));
586  }
587 
590  Rpc& withCallerTimeout(UInt milliseconds);
591 
594  template <typename R, typename P>
595  Rpc& withCallerTimeout(std::chrono::duration<R, P> timeout)
596  {
597  using namespace std::chrono;
598  setCallerTimeout(duration_cast<CallerTimeoutDuration>(timeout));
599  return *this;
600  }
601 
603  CallerTimeoutDuration callerTimeout() const;
604 
606 
614  Rpc& withDiscloseMe(bool disclosed = true);
616 
621  Rpc& withCancelMode(CallCancelMode mode);
622 
624  CallCancelMode cancelMode() const;
626 
627 private:
629 
630  void setCallerTimeout(CallerTimeoutDuration duration);
631 
632  Error* error_ = nullptr;
633  CallerTimeoutDuration callerTimeout_ = {};
634  CallCancelMode cancelMode_ = defaultCancelMode();
635  bool progressiveResultsEnabled_ = false;
636 
637 public:
638  Error* error(internal::PassKey); // Internal use only
639 };
640 
641 
642 //------------------------------------------------------------------------------
645 //------------------------------------------------------------------------------
646 class CPPWAMP_API Result : public Payload<Result, internal::ResultMessage>
647 {
648 public:
650  Result();
651 
654  Result(std::initializer_list<Variant> list);
655 
657  RequestId requestId() const;
658 
665  Result& withProgress(bool progressive = true);
666 
668  bool isProgressive() const;
670 
671 private:
673  Result(RequestId reqId, Object&& details);
674 
675 public:
676  // Internal use only
677  Result(internal::PassKey, internal::ResultMessage&& msg);
678 
679  internal::YieldMessage& yieldMessage(internal::PassKey, RequestId reqId)
680  {
681  message().setRequestId(reqId);
682  return message().transformToYield();
683  }
684 };
685 
686 CPPWAMP_API std::ostream& operator<<(std::ostream& out, const Result& result);
687 
688 
689 //------------------------------------------------------------------------------
693 //------------------------------------------------------------------------------
694 struct Deferment
695 {
696  constexpr Deferment() noexcept = default;
697 };
698 
699 //------------------------------------------------------------------------------
702 //------------------------------------------------------------------------------
703 constexpr CPPWAMP_INLINE_VARIABLE Deferment deferment;
704 
705 //------------------------------------------------------------------------------
708 //------------------------------------------------------------------------------
709 class CPPWAMP_API Outcome
710 {
711 public:
714  enum class Type
715  {
716  deferred,
717  result,
718  error
719  };
720 
723  CPPWAMP_DEPRECATED static Outcome deferred();
724 
726  Outcome();
727 
729  Outcome(Result result);
730 
733  Outcome(std::initializer_list<Variant> args);
734 
736  Outcome(Error error);
737 
740 
742  Outcome(const Outcome& other);
743 
745  Outcome(Outcome&& other);
746 
748  ~Outcome();
749 
751  Type type() const;
752 
754  const Result& asResult() const &;
755 
757  Result&& asResult() &&;
758 
760  const Error& asError() const &;
761 
763  Error&& asError() &&;
764 
766  Outcome& operator=(const Outcome& other);
767 
769  Outcome& operator=(Outcome&& other);
770 
771 private:
772  CPPWAMP_HIDDEN explicit Outcome(std::nullptr_t);
773  CPPWAMP_HIDDEN void copyFrom(const Outcome& other);
774  CPPWAMP_HIDDEN void moveFrom(Outcome&& other);
775  CPPWAMP_HIDDEN void destruct();
776 
777  Type type_;
778 
779  union CPPWAMP_HIDDEN Value
780  {
781  Value() {}
782  ~Value() {}
783 
784  Result result;
785  Error error;
786  } value_;
787 };
788 
789 
790 namespace internal { class Callee; } // Forward declaration
791 
792 //------------------------------------------------------------------------------
798 //------------------------------------------------------------------------------
799 class CPPWAMP_API Invocation : public Payload<Invocation,
800  internal::InvocationMessage>
801 {
802 public:
804  Invocation();
805 
808  bool empty() const;
809 
812  bool calleeHasExpired() const;
813 
815  RequestId requestId() const;
816 
818  AnyIoExecutor executor() const;
819 
821  void yield(Result result = Result()) const;
822 
824  void yield(Error error) const;
825 
832  bool isProgressive() const;
834 
841  Variant caller() const;
843 
850  Variant trustLevel() const;
852 
860  Variant procedure() const;
862 
863 public:
864  // Internal use only
865  using CalleePtr = std::weak_ptr<internal::Callee>;
866  Invocation(internal::PassKey, CalleePtr callee, AnyIoExecutor executor,
867  internal::InvocationMessage&& msg);
868 
869 private:
871 
872  CalleePtr callee_;
873  AnyIoExecutor executor_ = nullptr;
874 
875  template <typename, typename...> friend class CoroInvocationUnpacker;
876 };
877 
878 CPPWAMP_API std::ostream& operator<<(std::ostream& out, const Invocation& inv);
879 
880 
881 //------------------------------------------------------------------------------
884 //------------------------------------------------------------------------------
885 class CPPWAMP_API CallCancellation
887 {
888 public:
891  CallCancelMode cancelMode = Rpc::defaultCancelMode());
892 
894  RequestId requestId() const;
895 
897  CallCancelMode mode() const;
898 
899 private:
901 
902  RequestId requestId_;
903  CallCancelMode mode_;
904 };
905 
908 using Cancellation CPPWAMP_DEPRECATED = CallCancellation;
909 
910 //------------------------------------------------------------------------------
915 //------------------------------------------------------------------------------
916 class CPPWAMP_API Interruption : public Options<Interruption,
917  internal::InterruptMessage>
918 {
919 public:
921  Interruption();
922 
925  bool empty() const;
926 
929  bool calleeHasExpired() const;
930 
932  RequestId requestId() const;
933 
935  AnyIoExecutor executor() const;
936 
938  void yield(Result result = Result()) const;
939 
941  void yield(Error error) const;
942 
943 public:
944  // Internal use only
945  using CalleePtr = std::weak_ptr<internal::Callee>;
946  Interruption(internal::PassKey, CalleePtr callee, AnyIoExecutor executor,
947  internal::InterruptMessage&& msg);
948 
949 private:
951 
952  CalleePtr callee_;
953  AnyIoExecutor executor_ = nullptr;
954 };
955 
956 CPPWAMP_API std::ostream& operator<<(std::ostream& out,
957  const Interruption& cncltn);
958 
959 } // namespace wamp
960 
961 #ifndef CPPWAMP_COMPILED_LIB
962 #include "./internal/peerdata.ipp"
963 #endif
964 
965 #endif // CPPWAMP_PEERDATA_HPP
wamp::error::BadType
Base class for exceptions involving invalid Variant types.
Definition: error.hpp:89
wamp::SessionInfo::RoleSet
std::set< String > RoleSet
A set of role strings.
Definition: peerdata.hpp:102
wamp::Reason
Provides the reason URI and other options contained within GOODBYE messages.
Definition: peerdata.hpp:181
wamp::RequestId
int64_t RequestId
Ephemeral ID associated with a WAMP request.
Definition: wampdefs.hpp:23
wamp::Rpc::withDealerTimeout
Rpc & withDealerTimeout(std::chrono::duration< R, P > timeout)
Requests that the dealer cancel the call after the specified timeout duration.
Definition: peerdata.hpp:581
wamp::AnyIoExecutor
boost::asio::any_io_executor AnyIoExecutor
Polymorphic executor for all I/O objects.
Definition: asiodefs.hpp:27
wamp::Realm
Realm URI and other options contained within WAMP HELLO messages.
Definition: peerdata.hpp:59
wamp::UInt
std::uint64_t UInt
Variant bound type for unsigned integers.
Definition: variantdefs.hpp:48
wamp::CallCancellation
Contains the request ID and options contained within WAMP CANCEL messages.
Definition: peerdata.hpp:885
wamp::Variant
Discriminated union container that represents a JSON value.
Definition: variant.hpp:134
wamp::Authentication
Provides the Signature and Extra dictionary contained within WAMP AUTHENTICATE messages.
Definition: peerdata.hpp:205
wamp::SessionId
int64_t SessionId
Ephemeral ID associated with a WAMP session.
Definition: wampdefs.hpp:22
api.hpp
Defines macros related to exporting/importing APIs.
wamp::Interruption
Contains details within WAMP INTERRUPT messages.
Definition: peerdata.hpp:916
wamp::Payload
Wrapper around a WAMP message containing payload arguments and an options dictionary.
Definition: payload.hpp:33
wamp::Object
std::map< String, Variant > Object
Variant bound type for maps of variants.
Definition: variantdefs.hpp:52
wamp::Error
Provides the reason URI, options, and payload arguments contained within WAMP ERROR messages.
Definition: peerdata.hpp:292
wamp::Topic
Provides the topic URI and other options contained within WAMP ‘SUBSCRIBE’ messages.
Definition: peerdata.hpp:331
wamp::Abort
Provides the reason URI and other options contained within ABORT messages.
Definition: peerdata.hpp:39
wamp::SessionInfo::FeatureMap
std::map< String, FeatureSet > FeatureMap
A dictionary of feature sets to be supported by each role.
Definition: peerdata.hpp:108
wamp::Procedure
Contains the procedure URI and other options contained within WAMP REGISTER messages.
Definition: peerdata.hpp:492
wamp::SubscriptionId
int64_t SubscriptionId
Ephemeral ID associated with an topic subscription.
Definition: wampdefs.hpp:24
wamp::CoroInvocationUnpacker
Wrapper around a call coroutine slot which automatically unpacks positional payload arguments.
Definition: corounpacker.hpp:169
asiodefs.hpp
Common type definitions used by transports that rely on Boost.Asio.
wamp::SessionInfo::FeatureSet
std::set< String > FeatureSet
A set of feature strings.
Definition: peerdata.hpp:105
wamp::deferment
constexpr Deferment deferment
Convenient value of the wamp::Deferment tag type that can be passed to the wamp::Outcome constructor.
Definition: peerdata.hpp:703
wamp
Definition: anyhandler.hpp:36
wamp::Outcome::Type
Type
Enumerators representing the type of outcome being held by this object.
Definition: peerdata.hpp:714
wamp::Array
std::vector< Variant > Array
Variant bound type for arrays of variants.
Definition: variantdefs.hpp:51
wamp::Options
Wrapper around a WAMP message containing an options dictionary.
Definition: options.hpp:29
wamp::Int
std::int64_t Int
Variant bound type for signed integers.
Definition: variantdefs.hpp:47
wamp::Rpc
Contains the procedure URI, options, and payload contained within WAMP CALL messages.
Definition: peerdata.hpp:535
options.hpp
Contains the declaration of the Options class.
wamp::Outcome
Contains the outcome of an RPC invocation.
Definition: peerdata.hpp:709
wamp::Challenge
Provides the AuthMethod and Extra dictionary contained within WAMP CHALLENGE messages.
Definition: peerdata.hpp:240
wamp::PublicationId
int64_t PublicationId
Ephemeral ID associated with an event publication.
Definition: wampdefs.hpp:25
wamp::Pub
Provides the topic URI, options, and payload contained within WAMP PUBLISH messages.
Definition: peerdata.hpp:363
wamp::Result
Contains the remote procedure result options/payload within WAMP RESULT and YIELD messages.
Definition: peerdata.hpp:646
wamp::Rpc::withCallerTimeout
Rpc & withCallerTimeout(std::chrono::duration< R, P > timeout)
Requests that the dealer cancel the call after the specified timeout duration.
Definition: peerdata.hpp:595
wamp::SessionInfo
Session information contained within WAMP WELCOME messages.
Definition: peerdata.hpp:97
variant.hpp
Contains the declaration of Variant and other closely related types/functions.
wampdefs.hpp
Contains type definitions related to WAMP IDs and sessions.
payload.hpp
Contains the declaration of Payload, which bundles together Variant arguments.
wamp::Deferment
Tag type that can be passed to wamp::Outcome to construct a deferred outcome.
Definition: peerdata.hpp:694
wamp::Invocation
Contains payload arguments and other options within WAMP INVOCATION messages.
Definition: peerdata.hpp:799
wamp::Event
Provides the subscription/publication ids, options, and payload contained within WAMP EVENT messages.
Definition: peerdata.hpp:427
wamp::CallCancelMode::kill
@ kill
INTERRUPT sent to callee; RESULT or ERROR returned, depending on callee.
wamp::String
std::string String
Variant bound type for text strings.
Definition: variantdefs.hpp:50
wamp::Rpc::CallerTimeoutDuration
std::chrono::steady_clock::duration CallerTimeoutDuration
The duration type used for caller-initiated timeouts.
Definition: peerdata.hpp:539
wamp::CallCancelMode
CallCancelMode
Enumerates the possible call cancelling modes.
Definition: wampdefs.hpp:49