コード例 #1
0
    protected override async Task StreamWriteToObjectImplAsync(FastStreamBuffer fifo, CancellationToken cancel)
    {
        await TaskUtil.DoAsyncWithTimeout(
            async c =>
        {
            bool flush = false;

            using (MemoryHelper.FastAllocMemoryWithUsing(SendTmpBufferSize, out Memory <byte> buffer))
            {
                while (true)
                {
                    int size = fifo.DequeueContiguousSlowWithLock(buffer);
                    if (size == 0)
                    {
                        break;
                    }

                    var dataToSend = buffer.Slice(0, size);

                    await Stream.WriteAsync(dataToSend, cancel);
                    flush = true;
                }
            }

            if (flush)
            {
                await Stream.FlushAsync(cancel);
            }

            return(0);
        },
            cancel: cancel);
    }
コード例 #2
0
    // 抽象 Stream --> シリアルポート
    protected override async Task StreamWriteToObjectImplAsync(FastStreamBuffer fifo, CancellationToken cancel)
    {
        if (this.SerialPort.IsOpen == false)
        {
            throw new FastBufferDisconnectedException();
        }

        await TaskUtil.DoAsyncWithTimeout(
            async c =>
        {
            bool flush = false;

            using (MemoryHelper.FastAllocMemoryWithUsing(SendTmpBufferSize, out Memory <byte> buffer))
            {
                while (true)
                {
                    int size = fifo.DequeueContiguousSlowWithLock(buffer);
                    if (size == 0)
                    {
                        break;
                    }

                    await Stream.WriteAsync(buffer.Slice(0, size), cancel);
                    flush = true;
                }
            }

            if (flush)
            {
                await Stream.FlushAsync(cancel);
            }

            return(0);
        },
            cancelProc : () =>
        {
            this.SerialPort.Close();
        },
            cancel : cancel);
    }