Esempio n. 1
0
    private static void SendMessage(object _data)
    {
        DP_Msg data = (DP_Msg)_data;

        while (!data.m_bError)
        {
            Thread.Sleep(10);

            // lock (data.m_Lock)
            {
                Queue <WfPacket> local_RecvQueue        = data.m_RecvQueue;
                Queue <WfPacket> local_SendQueue        = data.m_SendQueue;
                object           local_Lock             = data.m_Lock;
                PacketHandlerMgr local_PacketHandlerMgr = data.m_PacketHandlerMgr;
                Socket           myClientSocket         = data.clientSocket;

                WfPacket msgPacket = null;
                string   text      = null;
                //处理发送消息

                while (local_SendQueue.Count > 0 || msgPacket != null)
                {
                    int num2 = 0;
                    if (msgPacket != null)
                    {
                        text = SendAll(data, msgPacket, ref num2);
                        if (num2 == msgPacket.GetOffset())
                        {
                            msgPacket = null;
                        }
                    }
                    if (text != null || msgPacket != null || local_SendQueue.Count <= 0)
                    {
                        break;
                    }
                    num2      = 0;
                    msgPacket = local_SendQueue.Dequeue();
                    msgPacket.SetHeadLength();
                }
            }
        }
        Log.print("线程{0}SendMessage结束。", data.clientSocket.RemoteEndPoint.ToString());
    }
Esempio n. 2
0
    private void WfNetworkThread()
    {
        HeaderBytes headbytes = PooledClassManager <HeaderBytes> .CreateClass();

        DataBytes databytes  = null;
        int       recvedNum  = 0;
        WfPacket  sendPacket = null;
        int       sendedNum  = 0;

        while (m_Thread.IsAlive)
        {
            try
            {
                Thread.Sleep(10);
                if (m_ConnectState == EConnectState.PostConnecting)
                {
                    databytes = null;
                    recvedNum = 0;
                    if (sendPacket != null)
                    {
                        sendPacket.DestroyClass();
                        sendPacket = null;
                    }
                    sendedNum = 0;
                    ConnectServer();
                }
                if (m_ConnectState == EConnectState.Connected)
                {
                    string errorText = null;
                    bool   bWaiting  = false;
                    do
                    {
                        errorText = RecvAll(ref headbytes, ref databytes, ref recvedNum, ref bWaiting);
                    }while (errorText == null && !bWaiting);
                    if (errorText == null)
                    {
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            while (m_SendQueue.Count > 0 || sendPacket != null)
                            {
                                if (sendPacket != null)
                                {
                                    errorText = SendAll(sendPacket, ref sendedNum);
                                    if (sendedNum == sendPacket.GetOffset())
                                    {
                                        sendPacket.DestroyClass();
                                        sendPacket = null;
                                    }
                                }
                                if (errorText != null || sendPacket != null || m_SendQueue.Count <= 0)
                                {
                                    break;
                                }
                                sendedNum  = 0;
                                sendPacket = m_SendQueue.Dequeue();
                                sendPacket.SetHeadLength();
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                    if (errorText != null)
                    {
                        Debug.LogError(errorText);
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            if (m_ConnectState != EConnectState.PostConnecting)
                            {
                                m_ConnectState     = EConnectState.Disconnected;
                                m_bNetStateChanged = true;
                                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message + ex.StackTrace);
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    if (m_ConnectState != EConnectState.PostConnecting)
                    {
                        m_ConnectState     = EConnectState.Disconnected;
                        m_bNetStateChanged = true;
                        //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                    }
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
        }
        m_Thread.Abort();
        m_Thread.Join();
    }