Beispiel #1
0
        public static async Task CopyTo <TReadOnlyStream>(
            this TReadOnlyStream source,
            IWriteOnlyStream destination,
            byte[] buffer,
            CancellationToken cancellationToken = default) where TReadOnlyStream : IReadOnlyStream
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            int bytesRead;

            while ((bytesRead = await source
                                .ReadAsync(buffer, 0, buffer.Length, cancellationToken)
                                .ConfigureAwait(false)) != 0)
            {
                await destination
                .WriteAsync(buffer, 0, bytesRead, cancellationToken)
                .ConfigureAwait(false);
            }
        }
Beispiel #2
0
        async Task CopyAsync(
            IReadOnlyStream source,
            IWriteOnlyStream destination,
            CancellationToken cancellationToken = default)
        {
            byte[] buffer = this._bufferPool.Rent(4096);
            try
            {
                int bytesRead;
                while ((bytesRead = await source
                                    .ReadAsync(buffer, 0, buffer.Length, cancellationToken)
                                    .ConfigureAwait(false)) != 0)
                {
                    // Transform

                    await destination
                    .WriteAsync(buffer, 0, bytesRead, cancellationToken)
                    .ConfigureAwait(false);
                }
            }
            finally
            {
                this._bufferPool.Return(buffer);
            }
        }
Beispiel #3
0
        public async Task WriteToAsync(IWriteOnlyStream stream, ArrayPool <byte> bufferPool)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (bufferPool == null)
            {
                throw new ArgumentNullException(nameof(bufferPool));
            }

            // | VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
            // |  1  | 1   | X'00' | 1    | Variable | 2        |
            var buffer = bufferPool.Rent(4 + 1 + byte.MaxValue + sizeof(ushort));

            try
            {
                buffer[0] = 5;
                buffer[1] = (byte)this.ReplyType;
                buffer[2] = 0;

                var bindEndPoint         = this.BindEndPoint ?? new IPEndPoint(new IPAddress(0L), 0);
                int endPointBytesWritten = bindEndPoint.ToBytes(buffer, 3);

                await stream.WriteAsync(buffer, 0, 3 + endPointBytesWritten).ConfigureAwait(false);
            }
            finally
            {
                bufferPool.Return(buffer);
            }
        }
Beispiel #4
0
        public async Task WriteToAsync(IWriteOnlyStream stream, ArrayPool <byte> bufferPool)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (bufferPool == null)
            {
                throw new ArgumentNullException(nameof(bufferPool));
            }

            // | VER | METHOD |
            // |  1  |    1   |
            var buffer = bufferPool.Rent(2);

            try
            {
                buffer[0] = 5;
                buffer[1] = (byte)this.SelectedAuthenticationMethod;
                await stream.WriteAsync(buffer, 0, 2).ConfigureAwait(false);
            }
            finally
            {
                bufferPool.Return(buffer);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HandleSendNotificationProtocol"/> class.
        /// </summary>
        /// <param name="eventStream">The Notification Event Stream.</param>
        /// <param name="sagaStream">The Notification Saga Stream.</param>
        /// <param name="getAudienceProtocol">Executes a <see cref="GetAudienceOp"/>.</param>
        /// <param name="getDeliveryChannelConfigsProtocol">Executes a <see cref="GetDeliveryChannelConfigsOp"/>.</param>
        /// <param name="prepareToSendOnChannelProtocol">Executes a <see cref="PrepareToSendOnChannelOp"/>.</param>
        /// <param name="cannotPrepareToSendOnChannelAction">Specifies what to do when we encounter a situation where we cannot prepare to send on a channel.</param>
        /// <param name="channelToOperationStreamMap">A map of delivery channel to the channel's operation stream.</param>
        /// <param name="buildSendNotificationRequestedEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="SendNotificationRequestedEvent"/> into the Notification Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildCouldNotGetOrUseAudienceEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="CouldNotGetOrUseAudienceEvent"/> into the Notification Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildCouldNotGetOrUseDeliveryChannelConfigsEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="CouldNotGetOrUseDeliveryChannelConfigsEvent"/> into the Notification Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildPrepareToSendNotificationEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="PrepareToSendNotificationEventBase"/> into the Notification Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildExecuteProcessSendNotificationSagaEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="ProcessSendNotificationSagaOp"/> into the Notification Saga Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        public HandleSendNotificationProtocol(
            IWriteOnlyStream eventStream,
            IWriteOnlyStream sagaStream,
            IGetAudienceProtocol getAudienceProtocol,
            IGetDeliveryChannelConfigsProtocol getDeliveryChannelConfigsProtocol,
            IPrepareToSendOnChannelProtocol prepareToSendOnChannelProtocol,
            CannotPrepareToSendOnChannelAction cannotPrepareToSendOnChannelAction,
            IReadOnlyDictionary <IDeliveryChannel, IWriteOnlyStream> channelToOperationStreamMap,
            IBuildTagsProtocol <SendNotificationRequestedEvent> buildSendNotificationRequestedEventTagsProtocol = null,
            IBuildTagsProtocol <CouldNotGetOrUseAudienceEvent> buildCouldNotGetOrUseAudienceEventTagsProtocol   = null,
            IBuildTagsProtocol <CouldNotGetOrUseDeliveryChannelConfigsEvent> buildCouldNotGetOrUseDeliveryChannelConfigsEventTagsProtocol = null,
            IBuildTagsProtocol <PrepareToSendNotificationEventBase> buildPrepareToSendNotificationEventTagsProtocol = null,
            IBuildTagsProtocol <ExecuteOpRequestedEvent <long, ProcessSendNotificationSagaOp> > buildExecuteProcessSendNotificationSagaEventTagsProtocol = null)
        {
            new { eventStream }.AsArg().Must().NotBeNull();
            new { sagaStream }.AsArg().Must().NotBeNull();
            new { getAudienceProtocol }.AsArg().Must().NotBeNull();
            new { getDeliveryChannelConfigsProtocol }.AsArg().Must().NotBeNull();
            new { prepareToSendOnChannelProtocol }.AsArg().Must().NotBeNull();
            new { cannotPrepareToSendOnChannelAction }.AsArg().Must().NotBeEqualTo(CannotPrepareToSendOnChannelAction.Unknown);
            new { channelToOperationStreamMap }.AsArg().Must().NotBeNullNorEmptyDictionaryNorContainAnyNullValues();

            this.eventStream         = eventStream;
            this.sagaStream          = sagaStream;
            this.getAudienceProtocol = getAudienceProtocol;
            this.getDeliveryChannelConfigsProtocol  = getDeliveryChannelConfigsProtocol;
            this.prepareToSendOnChannelProtocol     = prepareToSendOnChannelProtocol;
            this.cannotPrepareToSendOnChannelAction = cannotPrepareToSendOnChannelAction;
            this.channelToOperationStreamMap        = channelToOperationStreamMap;
            this.buildSendNotificationRequestedEventTagsProtocol = buildSendNotificationRequestedEventTagsProtocol;
            this.buildCouldNotGetOrUseAudienceEventTagsProtocol  = buildCouldNotGetOrUseAudienceEventTagsProtocol;
            this.buildCouldNotGetOrUseDeliveryChannelConfigsEventTagsProtocol = buildCouldNotGetOrUseDeliveryChannelConfigsEventTagsProtocol;
            this.buildPrepareToSendNotificationEventTagsProtocol          = buildPrepareToSendNotificationEventTagsProtocol;
            this.buildExecuteProcessSendNotificationSagaEventTagsProtocol = buildExecuteProcessSendNotificationSagaEventTagsProtocol;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SendNotificationProtocol"/> class.
        /// </summary>
        /// <param name="clientOperationStream">The client operation stream.</param>
        /// <param name="buildExecuteSendNotificationEventTagsProtocol">OPTIONAL protocol that executes a <see cref="BuildTagsOp{TEvent}"/>.  DEFAULT is null (no tags added when putting operation into stream).</param>
        public SendNotificationProtocol(
            IWriteOnlyStream clientOperationStream,
            IBuildTagsProtocol <ExecuteOpRequestedEvent <long, SendNotificationOp> > buildExecuteSendNotificationEventTagsProtocol = null)
        {
            new { clientOperationStream }.AsArg().Must().NotBeNull();

            this.clientOperationStream = clientOperationStream;
            this.buildExecuteSendNotificationEventTagsProtocol = buildExecuteSendNotificationEventTagsProtocol;
        }
Beispiel #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HandleProcessSendNotificationSagaProtocol"/> class.
        /// </summary>
        /// <param name="notificationEventStream">The event stream.</param>
        /// <param name="channelToEventStreamMap">A map of delivery channel to the channel's event stream.</param>
        /// <param name="buildAttemptToSendNotificationEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="AttemptToSendNotificationEventBase"/> into the Notification Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        public HandleProcessSendNotificationSagaProtocol(
            IWriteOnlyStream notificationEventStream,
            IReadOnlyDictionary <IDeliveryChannel, IReadOnlyStream> channelToEventStreamMap,
            IBuildTagsProtocol <AttemptToSendNotificationEventBase> buildAttemptToSendNotificationEventTagsProtocol = null)
        {
            new { notificationEventStream }.AsArg().Must().NotBeNull();
            new { channelToEventStreamMap }.AsArg().Must().NotBeNullNorEmptyDictionaryNorContainAnyNullValues();

            this.notificationEventStream = notificationEventStream;
            this.channelToEventStreamMap = channelToEventStreamMap;
            this.buildAttemptToSendNotificationEventTagsProtocol = buildAttemptToSendNotificationEventTagsProtocol;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HandleUploadFileToSlackProtocol"/> class.
        /// </summary>
        /// <param name="uploadFileToSlackProtocol">Protocol to upload a file to Slack.</param>
        /// <param name="slackEventStream">The Slack event stream.</param>
        /// <param name="buildUploadFileToSlackRequestedEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="UploadFileToSlackRequestedEvent{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildUploadFileToSlackResponseEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="UploadFileToSlackResponseEventBase{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        public HandleUploadFileToSlackProtocol(
            IUploadFileToSlackProtocol uploadFileToSlackProtocol,
            IWriteOnlyStream slackEventStream,
            IBuildTagsProtocol <UploadFileToSlackRequestedEvent <long> > buildUploadFileToSlackRequestedEventTagsProtocol   = null,
            IBuildTagsProtocol <UploadFileToSlackResponseEventBase <long> > buildUploadFileToSlackResponseEventTagsProtocol = null)
        {
            new { uploadFileToSlackProtocol }.AsArg().Must().NotBeNull();
            new { slackEventStream }.AsArg().Must().NotBeNull();

            this.uploadFileToSlackProtocol = uploadFileToSlackProtocol;
            this.slackEventStream          = slackEventStream;
            this.buildUploadFileToSlackRequestedEventTagsProtocol = buildUploadFileToSlackRequestedEventTagsProtocol;
            this.buildUploadFileToSlackResponseEventTagsProtocol  = buildUploadFileToSlackResponseEventTagsProtocol;
        }
Beispiel #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HandleSendEmailProtocol"/> class.
        /// </summary>
        /// <param name="sendEmailProtocol">Protocol to send an email.</param>
        /// <param name="emailEventStream">The email event stream.</param>
        /// <param name="buildSendEmailRequestedEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="SendEmailRequestedEvent{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildSendEmailResponseEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="SendEmailResponseEventBase{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        public HandleSendEmailProtocol(
            ISendEmailProtocol sendEmailProtocol,
            IWriteOnlyStream emailEventStream,
            IBuildTagsProtocol <SendEmailRequestedEvent <long> > buildSendEmailRequestedEventTagsProtocol   = null,
            IBuildTagsProtocol <SendEmailResponseEventBase <long> > buildSendEmailResponseEventTagsProtocol = null)
        {
            new { sendEmailProtocol }.AsArg().Must().NotBeNull();
            new { emailEventStream }.AsArg().Must().NotBeNull();

            this.sendEmailProtocol = sendEmailProtocol;
            this.emailEventStream  = emailEventStream;
            this.buildSendEmailRequestedEventTagsProtocol = buildSendEmailRequestedEventTagsProtocol;
            this.buildSendEmailResponseEventTagsProtocol  = buildSendEmailResponseEventTagsProtocol;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HandleSendSlackMessageProtocol"/> class.
        /// </summary>
        /// <param name="sendSlackMessageProtocol">Protocol to send a slack message.</param>
        /// <param name="slackEventStream">The Slack event stream.</param>
        /// <param name="buildSendSlackMessageRequestedEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="SendSlackMessageRequestedEvent{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        /// <param name="buildSendSlackMessageResponseEventTagsProtocol">OPTIONAL protocol to builds the tags to use when putting the <see cref="SendSlackMessageResponseEventBase{TId}"/> into the Email Event Stream.  DEFAULT is to not add any tags; tags will be null.  Consider using <see cref="UseInheritableTagsProtocol{TEvent}"/> to just use the inheritable tags.</param>
        public HandleSendSlackMessageProtocol(
            ISendSlackMessageProtocol sendSlackMessageProtocol,
            IWriteOnlyStream slackEventStream,
            IBuildTagsProtocol <SendSlackMessageRequestedEvent <long> > buildSendSlackMessageRequestedEventTagsProtocol   = null,
            IBuildTagsProtocol <SendSlackMessageResponseEventBase <long> > buildSendSlackMessageResponseEventTagsProtocol = null)
        {
            new { sendSlackMessageProtocol }.AsArg().Must().NotBeNull();
            new { slackEventStream }.AsArg().Must().NotBeNull();

            this.sendSlackMessageProtocol = sendSlackMessageProtocol;
            this.slackEventStream         = slackEventStream;
            this.buildSendSlackMessageRequestedEventTagsProtocol = buildSendSlackMessageRequestedEventTagsProtocol;
            this.buildSendSlackMessageResponseEventTagsProtocol  = buildSendSlackMessageResponseEventTagsProtocol;
        }
Beispiel #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExecuteOpOnScheduleProtocol"/> class.
        /// </summary>
        /// <param name="scheduleExecutionReadStream">The schedule execution read stream.</param>
        /// <param name="scheduleExecutionWriteStream">The schedule execution write stream.</param>
        /// <param name="protocolFactory">The factory to determine the appropriate protocol to execute the scheduled operation.</param>
        /// <param name="evaluateScheduleProtocol">The protocol to evaluate schedules.</param>
        public ExecuteOpOnScheduleProtocol(
            IReadOnlyStream scheduleExecutionReadStream,
            IWriteOnlyStream scheduleExecutionWriteStream,
            IProtocolFactory protocolFactory,
            ISyncAndAsyncReturningProtocol <EvaluateScheduleOp, bool> evaluateScheduleProtocol)
        {
            scheduleExecutionReadStream.MustForArg(nameof(scheduleExecutionReadStream)).NotBeNull();
            scheduleExecutionWriteStream.MustForArg(nameof(scheduleExecutionWriteStream)).NotBeNull();
            protocolFactory.MustForArg(nameof(protocolFactory)).NotBeNull();
            evaluateScheduleProtocol.MustForArg(nameof(evaluateScheduleProtocol)).NotBeNull();

            this.scheduleExecutionReadStream  = scheduleExecutionReadStream;
            this.scheduleExecutionWriteStream = scheduleExecutionWriteStream;
            this.protocolFactory          = protocolFactory;
            this.evaluateScheduleProtocol = evaluateScheduleProtocol;
        }