OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and API documentation
Loading...
Searching...
No Matches
MulticastSender.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
4// and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable OnixS Software
7// Services Agreement (the Agreement) and Customer end user license agreements granting
8// a non-assignable, non-transferable and non-exclusive license to use the software
9// for it's own data processing purposes under the terms defined in the Agreement.
10//
11// Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
12// of this source code or associated reference material to any other location for further reproduction
13// or redistribution, and any amendments to this copyright notice, are expressly prohibited.
14//
15// Any reproduction or redistribution for sale or hiring of the Software not in accordance with
16// the terms of the Agreement is a violation of copyright law.
17//
18#pragma once
19
20#include <cstddef>
21#include <memory>
22#include <string>
23#include <type_traits>
24#include <unordered_map>
25
34
36
38constexpr const char* const MulticastEgressEnvironmentVariable = "ONIXS_CMEMDH_ALLOW_MULTICAST_EGRESS";
39
41constexpr const char* const AllowAnyMulticastDestinationEnvironmentVariable = "ONIXS_CMEMDH_ALLOW_ANY_MULTICAST_DESTINATION";
42
78
81{
82 template <class Span> using EnableIfByteSpan = typename std::enable_if<std::is_same<Span, ByteSpan>::value, int>::type;
83
84public:
86 static constexpr PacketSize DefaultMaxPacketSize = 1420;
87
89 static constexpr size_t MaxSpansCount = 16;
90
111 NetFeedType::Enum target,
112 const FeedSettings& feedSettings = generateFeedSettings(),
113 Messaging::TimeSpan arbitrageDelay = Messaging::TimeSpan{},
116
119
137 Threading::Event send(const void* data, size_t dataSize);
138
154
173 template <class Span, EnableIfByteSpan<Span> = 0>
175 Threading::Event send(const Span spans[], size_t spansCount)
176 {
177 return sendSpans(spans, spansCount);
178 }
179
200 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
202 Threading::Event send(Collection&& spans)
203 {
204 const auto checked = details::checkedSpans(spans, MaxSpansCount);
205 return send(checked.data, checked.count);
206 }
207
216 template <size_t N>
218 Threading::Event send(const ByteSpan (&spans)[N])
219 {
220 static_assert(N <= MaxSpansCount, "");
221 return send(spans, N);
222 }
223
232 void sendA(const void* data, size_t dataSize);
233
239 void sendA(ByteSpan span);
240
251 template <class Span, EnableIfByteSpan<Span> = 0>
252 void sendA(const Span spans[], size_t spansCount)
253 {
254 sendASpans(spans, spansCount);
255 }
256
268 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
269 void sendA(Collection&& spans)
270 {
271 const auto checked = details::checkedSpans(spans, MaxSpansCount);
272 sendA(checked.data, checked.count);
273 }
274
282 template <size_t N>
283 void sendA(const ByteSpan (&spans)[N])
284 {
285 static_assert(N <= MaxSpansCount, "");
286 sendA(spans, N);
287 }
288
297 void sendB(const void* data, size_t dataSize);
298
304 void sendB(ByteSpan span);
305
316 template <class Span, EnableIfByteSpan<Span> = 0>
317 void sendB(const Span spans[], size_t spansCount)
318 {
319 sendBSpans(spans, spansCount);
320 }
321
333 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
334 void sendB(Collection&& spans)
335 {
336 const auto checked = details::checkedSpans(spans, MaxSpansCount);
337 sendB(checked.data, checked.count);
338 }
339
347 template <size_t N>
348 void sendB(const ByteSpan (&spans)[N])
349 {
350 static_assert(N <= MaxSpansCount, "");
351 sendB(spans, N);
352 }
353
362 void sendPrimary(const void* data, size_t dataSize);
363
370
381 template <class Span, EnableIfByteSpan<Span> = 0>
382 void sendPrimary(const Span spans[], size_t spansCount)
383 {
384 sendPrimarySpans(spans, spansCount);
385 }
386
398 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
399 void sendPrimary(Collection&& spans)
400 {
401 const auto checked = details::checkedSpans(spans, MaxSpansCount);
402 sendPrimary(checked.data, checked.count);
403 }
404
412 template <size_t N>
413 void sendPrimary(const ByteSpan (&spans)[N])
414 {
415 static_assert(N <= MaxSpansCount, "");
416 sendPrimary(spans, N);
417 }
418
427 void sendSecondary(const void* data, size_t dataSize);
428
435
446 template <class Span, EnableIfByteSpan<Span> = 0>
447 void sendSecondary(const Span spans[], size_t spansCount)
448 {
449 sendSecondarySpans(spans, spansCount);
450 }
451
463 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
464 void sendSecondary(Collection&& spans)
465 {
466 const auto checked = details::checkedSpans(spans, MaxSpansCount);
467 sendSecondary(checked.data, checked.count);
468 }
469
477 template <size_t N>
478 void sendSecondary(const ByteSpan (&spans)[N])
479 {
480 static_assert(N <= MaxSpansCount, "");
481 sendSecondary(spans, N);
482 }
483
487
490 NetFeedRole::Enum secondaryFeed() const noexcept;
491
494 bool hasSecondary() const noexcept;
495
497 MulticastTarget& operator=(const MulticastTarget&) = delete;
498
499private:
500 class FeedClient;
501 using FeedClientPtr = std::unique_ptr<FeedClient>;
502 class AsyncSecondarySender;
503
504 static void construct(FeedClientPtr& client, const FeedConnectionSettings& connection, const MulticastTargetOptions& options);
505 static void destruct(FeedClientPtr& client);
506 static void send(FeedClientPtr& client, ByteSpan span);
507 static void send(FeedClientPtr& client, const ByteSpan spans[], size_t spansCount);
508 Threading::Event sendSpans(const ByteSpan spans[], size_t spansCount);
509 void sendASpans(const ByteSpan spans[], size_t spansCount);
510 void sendBSpans(const ByteSpan spans[], size_t spansCount);
511 void sendPrimarySpans(const ByteSpan spans[], size_t spansCount);
512 void sendSecondarySpans(const ByteSpan spans[], size_t spansCount);
513 size_t validateDataSize(const ByteSpan spans[], size_t spansCount);
514
515 FeedClientPtr& getFeed(NetFeedRole::Enum role) noexcept;
516 const FeedClientPtr& getFeed(NetFeedRole::Enum role) const noexcept;
517
518 const Messaging::TimeSpan arbitrageDelay_ = Messaging::TimeSpan{};
519 ReplayDelayer& delayPolicy_;
520 const PacketSize packetMaxSize_;
521 NetFeedRole::Enum primaryFeed_ = NetFeedRole::A;
522
523 FeedClientPtr feedA_;
524 FeedClientPtr feedB_;
525 std::unique_ptr<AsyncSecondarySender> asyncSecondarySender_;
526};
527
528using MulticastTargetPtr = std::unique_ptr<MulticastTarget>;
529
532{
533 template <class Span>
534 using EnableIfByteSpan = typename std::enable_if<std::is_same<Span, ByteSpan>::value, int>::type;
535
536public:
553 const FeedSettings& feedSettings = generateFeedSettings(),
554 Messaging::TimeSpan arbitrageDelay = Messaging::TimeSpan{},
557
560
576 Threading::Event send(NetFeedType::Enum target, const void* data, size_t dataSize);
577
595
616 NetFeedType::Enum target,
617 const FeedSettings& feedSettings,
618 Messaging::TimeSpan arbitrageDelay = Messaging::TimeSpan{},
621
641 template <class Span, EnableIfByteSpan<Span> = 0>
643 Threading::Event send(NetFeedType::Enum target, const Span spans[], size_t spansCount)
644 {
645 return sendSpans(target, spans, spansCount);
646 }
647
668 template <class Collection, details::EnableIfByteSpanCollection<Collection> = 0>
670 Threading::Event send(NetFeedType::Enum target, Collection&& spans)
671 {
672 const auto checked = details::checkedSpans(spans, MulticastTarget::MaxSpansCount);
673 return send(target, checked.data, checked.count);
674 }
675
686 template <size_t N>
689 {
690 static_assert(N <= MulticastTarget::MaxSpansCount, "");
691 return send(target, spans, N);
692 }
693
702
705
706private:
707 struct NetFeedTypeHash
708 {
709 std::size_t operator()(NetFeedType::Enum value) const noexcept
710 {
711 return static_cast<std::size_t>(value);
712 }
713 };
714
715 Threading::Event sendSpans(NetFeedType::Enum target, const ByteSpan spans[], size_t spansCount);
716
717 std::unordered_map<NetFeedType::Enum, MulticastTargetPtr, NetFeedTypeHash> targets_;
718};
719
#define ONIXS_CMEMDH_LTWT
Definition Bootstrap.h:61
#define ONIXS_CME_MDH_TESTING_NAMESPACE_END
Definition Bootstrap.h:84
#define ONIXS_CMEMDH_LTWT_EXPORTED
Definition Bootstrap.h:62
#define ONIXS_CME_MDH_TESTING_NAMESPACE_BEGIN
Definition Bootstrap.h:83
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:163
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
The collection of parameters defining feed connection.
~MulticastSender()
Releases all configured multicast targets.
Threading::Event send(NetFeedType::Enum target, const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to the requested network feed type.
MulticastSender(const MulticastSender &)=delete
Threading::Event send(NetFeedType::Enum target, const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to a network feed type.
Threading::Event send(NetFeedType::Enum target, Collection &&spans)
Sends a contiguous collection of byte views to a network feed type.
MulticastSender & operator=(const MulticastSender &)=delete
Threading::Event send(NetFeedType::Enum target, const void *data, size_t dataSize)
Sends a datagram to the requested network feed type.
static MulticastTargetPtr constructTarget(NetFeedType::Enum target, const FeedSettings &feedSettings, Messaging::TimeSpan arbitrageDelay=Messaging::TimeSpan{}, ReplayDelayer &delayPolicy=CombinedReplayDelayer::service(), const MulticastTargetOptions &options=MulticastTargetOptions{})
Constructs a multicast target for the requested network feed type.
MulticastSender(const FeedSettings &feedSettings=generateFeedSettings(), Messaging::TimeSpan arbitrageDelay=Messaging::TimeSpan{}, ReplayDelayer &delayPolicy=CombinedReplayDelayer::service(), const MulticastTargetOptions &options=MulticastTargetOptions{})
Initializes multicast targets from the feed settings.
Threading::Event send(NetFeedType::Enum target, ByteSpan span)
Sends a byte view as a datagram to the requested network feed type.
MulticastTargetPtr & getTarget(NetFeedType::Enum target)
Returns the multicast target configured for the requested network feed type.
Threading::Event send(Collection &&spans)
Sends a contiguous collection of byte views as one datagram.
~MulticastTarget()
Leaves joined multicast groups and releases owned multicast clients.
void sendSecondary(const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to the secondary feed when configured.
void sendB(const void *data, size_t dataSize)
Sends a datagram to feed B when feed B is configured.
void sendB(const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to feed B.
static constexpr size_t MaxSpansCount
Maximum number of byte spans accepted by a gather send.
void sendSecondary(const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to the secondary feed.
MulticastTarget(NetFeedType::Enum target, const FeedSettings &feedSettings=generateFeedSettings(), Messaging::TimeSpan arbitrageDelay=Messaging::TimeSpan{}, ReplayDelayer &delayPolicy=CombinedReplayDelayer::service(), const MulticastTargetOptions &options=MulticastTargetOptions{})
Initializes the target for one network feed type.
NetFeedRole::Enum secondaryFeed() const noexcept
Returns the feed role used as the secondary feed.
void sendB(Collection &&spans)
Sends a contiguous collection of byte views as one datagram to feed B.
void sendPrimary(const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to the primary feed.
void sendA(const void *data, size_t dataSize)
Sends a datagram to feed A when feed A is configured.
Threading::Event send(const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to the configured feeds.
void sendPrimary(ByteSpan span)
Sends a byte view as a datagram to the primary feed.
void sendPrimary(const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to the primary feed.
Threading::Event send(const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to the configured multicast feeds.
void sendA(ByteSpan span)
Sends a byte view as a datagram to feed A when feed A is configured.
NetFeedRole::Enum primaryFeed() const noexcept
Returns the feed role used as the primary feed.
void sendSecondary(const void *data, size_t dataSize)
Sends a datagram immediately to the secondary feed when it is configured.
bool hasSecondary() const noexcept
Indicates whether the configured layout has a secondary feed.
Threading::Event send(const void *data, size_t dataSize)
Sends a datagram to the primary feed and sends to the secondary feed when it is configured.
void sendA(const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to feed A when configured.
void sendA(Collection &&spans)
Sends a contiguous collection of byte views as one datagram to feed A.
static constexpr PacketSize DefaultMaxPacketSize
Packet-size value matching the default feed settings.
void sendSecondary(Collection &&spans)
Sends a contiguous collection of byte views as one datagram to the secondary feed.
Threading::Event send(ByteSpan span)
Sends a byte view as a datagram to the configured multicast feeds.
void sendPrimary(const void *data, size_t dataSize)
Sends a datagram to the primary feed.
void sendB(const Span spans[], size_t spansCount)
Sends multiple byte views as one datagram to feed B when configured.
void sendSecondary(ByteSpan span)
Sends a byte view immediately to the secondary feed when it is configured.
void sendPrimary(Collection &&spans)
Sends a contiguous collection of byte views as one datagram to the primary feed.
void sendA(const ByteSpan(&spans)[N])
Sends a fixed-size sequence of byte views as one datagram to feed A.
void sendB(ByteSpan span)
Sends a byte view as a datagram to feed B when feed B is configured.
std::uint64_t UInt64
uInt64.
Definition Integral.h:37
UInt8 Byte
Alias for Byte.
Definition Memory.h:36
SpanRange checkedSpans(Collection &spans, std::size_t maxSpansCount, std::size_t minSpansCount=0)
Definition Utils.h:98
constexpr const char *const MulticastEgressEnvironmentVariable
Environment variable authorizing multicast traffic beyond the local host.
FeedSettings generateFeedSettings(ChannelId channel=999)
Generates feed settings for one testing channel.
constexpr const char *const AllowAnyMulticastDestinationEnvironmentVariable
Environment variable authorizing outgoing multicast destinations outside 239.0.0.0/8.
std::unique_ptr< MulticastTarget > MulticastTargetPtr
std::span< const std::uint8_t > ByteSpan
Provides a non-owning read-only view over contiguous byte storage.
Definition ByteSpan.h:44
SharedFuture< void > Event
Completion signal for an asynchronous operation that does not produce a value.
Definition Future.h:208
Messaging::UInt16 PacketSize
Represents a packet size in bytes.
STL namespace.
static ReplayDelayer & service()
Returns the instance reference.
Identifies feed A or feed B within a redundant pair.
Enum
Supported feed roles.
@ A
Feed A in the redundant pair.
Enum
Supported market data feed services.
Configures multicast sockets used by a multicast target.
MulticastTargetOptions()=default
Initializes multicast target options with a TTL of zero.
MulticastTargetOptions & ttl(Messaging::UInt64 value)
Sets the IPv4 multicast time-to-live.
Messaging::Byte ttl() const noexcept
Returns the IPv4 multicast time-to-live.