public bool DoSendResult()
        {
            string commandText = outgoingDataAssembler.GetProtocolText();

            byte[] bufferUTF8  = Encoding.UTF8.GetBytes(commandText);
            int    totalLength = sizeof(int) + bufferUTF8.Length; //获取总大小
            AsyncSendBufferManager asyncSendBufferManager = asyncSocketUserToken.SendBuffer;

            asyncSendBufferManager.StartPacket();
            asyncSendBufferManager.DynamicBufferManager.WriteInt(totalLength, false);       //写入总大小
            asyncSendBufferManager.DynamicBufferManager.WriteInt(bufferUTF8.Length, false); //写入命令大小
            asyncSendBufferManager.DynamicBufferManager.WriteBuffer(bufferUTF8);            //写入命令内容
            asyncSendBufferManager.EndPacket();

            bool result = true;

            if (!sendAsync)
            {
                int packetOffset = 0;
                int packetCount  = 0;
                if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount))
                {
                    sendAsync = true;
                    result    = asyncSocketServer.SendAsyncEvent(asyncSocketUserToken.Socket, asyncSocketUserToken.SendEventArgs,
                                                                 asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount);
                }
            }
            return(result);
        }
Esempio n. 2
0
        public void SendCommand()
        {
            string commandText = m_outgoingDataAssembler.GetProtocolText();

            byte[] bufferUTF8  = Encoding.UTF8.GetBytes(commandText);
            int    totalLength = sizeof(int) + bufferUTF8.Length; //获取总大小

            m_sendBuffer.Clear();
            m_sendBuffer.WriteInt(totalLength, false);                                                 //写入总大小
            m_sendBuffer.WriteInt(bufferUTF8.Length, false);                                           //写入命令大小
            m_sendBuffer.WriteBuffer(bufferUTF8);                                                      //写入命令内容
            m_tcpClient.Client.Send(m_sendBuffer.Buffer, 0, m_sendBuffer.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回
        }