Exemple #1
0
        // 发送消息
        public void Send()
        {
#if NET_MULTHREAD
            using (MLock mlock = new MLock(m_sendMutex))
#endif
            {
                if (!checkAndUpdateConnect())
                {
                    return;
                }

                //checkThread();

                if (m_socket == null)
                {
                    return;
                }

                if (m_clientBuffer.sendBuffer.bytesAvailable == 0)            // 如果发送缓冲区没有要发送的数据
                {
                    if (m_clientBuffer.sendTmpBuffer.circularBuffer.size > 0) // 如果发送临时缓冲区有数据要发
                    {
                        m_clientBuffer.getSocketSendData();
                    }

                    if (m_clientBuffer.sendBuffer.bytesAvailable == 0)        // 如果发送缓冲区中确实没有数据
                    {
#if NET_MULTHREAD
                        m_msgSendEndEvent.Set();        // 通知等待线程,所有数据都发送完成
#endif
                        return;
                    }
                }

                try
                {
                    Ctx.m_instance.m_logSys.log(string.Format("开始发送字节数 {0} ", m_clientBuffer.sendBuffer.bytesAvailable));

                    IAsyncResult asyncSend = m_socket.BeginSend(m_clientBuffer.sendBuffer.dynBuff.buff, (int)m_clientBuffer.sendBuffer.position, (int)m_clientBuffer.sendBuffer.bytesAvailable, 0, new System.AsyncCallback(SendCallback), 0);
                    //bool success = asyncSend.AsyncWaitHandle.WaitOne(m_sendTimeout, true);
                    //if (!success)
                    //{
                    //    Ctx.m_instance.m_logSys.asyncLog(string.Format("SendMsg Timeout {0} ", m_sendTimeout));
                    //}
                }
                catch (System.Exception e)
                {
#if NET_MULTHREAD
                    m_msgSendEndEvent.Set();        // 发生异常,通知等待线程,所有数据都发送完成,防止等待线程不能解锁
#endif
                    // 输出日志
                    Ctx.m_instance.m_logSys.error(e.Message);
                    //Disconnect(0);
                }
            }
        }