/// <summary>
        /// 组包发送
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public bool SendCommand()
        {
            //OutDataParser.GetProtocolText()结合使用 下
            OutDataParser.Clear();
            OutDataParser.AddResponse();
            OutDataParser.AddValue(ProtocolKeys.UserName, "admin");
            OutDataParser.AddValue(ProtocolKeys.Password, HelpCommonLib.ComminClass.MD5Encrypt("admin", 16));

            string commandText = OutDataParser.GetProtocolText();

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

            UserToken.SendBuffer.StartPacket();
            UserToken.SendBuffer.m_dynamicBufferManager.WriteInt(totalLength);   //写入总大小
            UserToken.SendBuffer.m_dynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容
            UserToken.SendBuffer.EndPacket();

            int packetOffset = 0;
            int packetCount  = 0;

            if (UserToken.SendBuffer.GetFirstPacket(ref packetOffset, ref packetCount))
            {
                m_tcpClient.Client.Send(UserToken.SendBuffer.m_dynamicBufferManager.Buffer, 0, UserToken.SendBuffer.m_dynamicBufferManager.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回
                while (UserToken.SendBuffer.GetFirstPacket(ref packetOffset, ref packetCount))
                {
                    UserToken.SendBuffer.ClearFirstPacket();
                    m_tcpClient.Client.Send(UserToken.SendBuffer.m_dynamicBufferManager.Buffer, 0, UserToken.SendBuffer.m_dynamicBufferManager.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回
                }
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// 发送返回值
        /// </summary>
        /// <returns>bool</returns>
        public bool DoSendResult()
        {
            try
            {
                string commandText = OutDataParser.GetProtocolText(); //已经添加了返回信息          插入-1
                byte[] bufferUTF8  = Encoding.UTF8.GetBytes(commandText);
                int    totalLength = bufferUTF8.Length;               //获取总大小
                AsyncSendBufferManager asyncSendBufferManager = socketUserToken.SendBuffer;
                asyncSendBufferManager.StartPacket();
                asyncSendBufferManager.m_dynamicBufferManager.WriteInt(totalLength);   //写入总大小
                asyncSendBufferManager.m_dynamicBufferManager.WriteBuffer(bufferUTF8); //写入命令内容
                asyncSendBufferManager.EndPacket();

                bool result = true;
                if (!m_sendAsync)
                {
                    int packetOffset = 0;
                    int packetCount  = 0;
                    if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount))
                    {
                        m_sendAsync = true;
                        result      = m_asyncSocketServer.SendAsyncEvent(socketUserToken.ConnectSocket, socketUserToken.SendEventArgs,
                                                                         socketUserToken.ReceiveBuffer.Buffer, packetOffset, packetCount);
                    }
                }
            }
            catch { m_asyncSocketServer.CloseClientSocket(socketUserToken); }
            return(true);
        }