コード例 #1
0
        public async ValueTask <NetSendState> FlushSendBuffer()
        {
            if (!Socket.Connected)
            {
                return(NetSendState.Closed);
            }

            ChunkedMemoryStream sendBuffer = SendBuffer;
            int length = (int)sendBuffer.Length;

            if (length > 0)
            {
                int left  = length;
                int block = 0;
                while (left > 0)
                {
                    Memory <byte> buffer      = sendBuffer.GetBlock(block);
                    int           blockLength = Math.Min(sendBuffer.BlockSize, left);

                    Memory <byte> data  = buffer.Slice(0, blockLength);
                    int           write = await Socket.SendAsync(data, SocketFlags.None).Unchain();

                    if (write == 0)
                    {
                        Close(immediate: false);
                        return(NetSendState.Closing);
                    }

                    BytesSent += write;
                    left      -= write;
                    block++;
                }

                SendBuffer.TrimStart(length);
            }
            return(NetSendState.FullSend);
        }