コード例 #1
0
        /// <summary>
        /// Send the data, whose size should less than or equal to OutgoingBufferSize in bytes.
        /// </summary>
        /// <param name="data">Data.</param>
        /// <param name="throwExceptionOnFail"></param>
        public async Task <bool> Send(ClientOutputBuffer data, bool throwExceptionOnFail = false)
        {
            if (Disconnected || data.m_dataLen == 0)
            {
                return(false);
            }

            bool sendOk = true;

            try
            {
                //Log.Info("发送一条消息 Count = " + data.m_dataLen);
                await m_tcpClient.GetStream().WriteAsync(data.m_buffer, 0, data.m_dataLen, m_cancellationToken);
            }
            catch (Exception)
            {
                if (throwExceptionOnFail)
                {
                    throw;
                }

                //Console.WriteLine("Client.Send Exception! MsgId = {0}, Error={1} time={2} BufferCount={3}", BitConverter.ToInt32(data._buffer, sizeof(Int32)), ex.Message, DateTime.Now.ToString(), ClientOutputBuffer.PoolBuffCount);
                sendOk = false;
            }
            finally
            {
                //将buff放入缓存队列中
                ClientOutputBuffer.UnlockSendBuffer(data);
            }
            ///Log.Info($"发送一条消息  {sendOk}  ");
            return(sendOk);
        }