Example #1
0
        /// <inheritdoc/>
        protected override async Task Listen(string mailslotName, PipeWriter writer)
        {
            using (var server = MailSlot.CreateServer(mailslotName)) {
                while (!_listenCancelToken.IsCancellationRequested)
                {
                    try {
                        var memory = writer.GetMemory(4096);
                        var count  = await server.ReadAsync(memory, _listenCancelToken);

                        if (count == 0)
                        {
                            break;
                        }

                        writer.Advance(count);
                        var writeResult = await writer.FlushAsync(_listenCancelToken);

                        if (writeResult.IsCompleted)
                        {
                            return;
                        }
                    }
                    catch (OperationCanceledException) {
                        break;
                    }
                    catch (Exception ex) {
                        writer.Complete(ex);
                        return;
                    }
                }

                writer.Complete();
            }
        }
        /// <inheritdoc/>
        protected override async Task Listen(string mailslotName, PipeWriter writer)
        {
            var buffer = new byte[4096];

            using (var server = MailSlot.CreateServer(mailslotName)) {
                while (!_cancelToken.IsCancellationRequested)
                {
                    try {
                        var memory = writer.GetMemory(5);
                        var count  = await server.ReadAsync(buffer, 0, buffer.Length, _cancelToken);

                        if (count == 0)
                        {
                            break;
                        }

                        var bufferMemory = new Memory <byte>(buffer, 0, count);
                        bufferMemory.CopyTo(memory);
                        writer.Advance(count);

                        var writeResult = await writer.FlushAsync(_cancelToken);

                        if (writeResult.IsCompleted)
                        {
                            break;
                        }
                    }
                    catch (OperationCanceledException) {
                        break;
                    }
                    catch (Exception ex) {
                        writer.Complete(ex);
                        return;
                    }
                }

                writer.Complete();
            }
        }