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

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

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

                    var data  = buffer.Slice(0, blockLength);
                    int write = await Socket.SendAsync(data, SocketFlags.None).ConfigureAwait(false);

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

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

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