Example #1
0
        async Task HandleWriting()
        {
            while (true)
            {
                var bb = await WriteBucket.ReadAsync().ConfigureAwait(false);

                if (bb.IsEof)
                {
                    if (!_writeEof)
                    {
                        _writeEof = true;
                    }
                }

                if (bb.Length > 0)
                {
#if NETFRAMEWORK
                    var(arr, offs) = bb.ExpandToArray();

                    await _stream.WriteAsync(arr !, offs, bb.Length).ConfigureAwait(false);
#else
                    await _stream.WriteAsync(bb.Memory).ConfigureAwait(false);
#endif
                    BytesWritten += bb.Length;
                }
            }
        }
Example #2
0
        async Task HandleWriting()
        {
            while (true)
            {
                var bb = await WriteBucket.ReadAsync().ConfigureAwait(false);

                if (bb.IsEof)
                {
                    if (!_writeEof)
                    {
                        _writeEof = true;
                        Socket.Shutdown(SocketShutdown.Send);
                    }
                }

                while (bb.Length > 0)
                {
#if NET6_0_OR_GREATER
                    int written = await Socket.SendAsync(bb.Memory, SocketFlags.None).ConfigureAwait(false);
#else
                    var(arr, offs) = bb.ExpandToArray();

                    int written = await Socket.SendAsync(new ArraySegment <byte>(arr !, offs, bb.Length), SocketFlags.None).ConfigureAwait(false);
#endif

                    if (written > 0)
                    {
                        BytesWritten += written;
                        bb            = bb.Slice(written);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }