Exemple #1
0
        private void ProcessPackets()
        {
            LinkedListNode <GamePacket> node = mPacketList.First;

            while (node != null)
            {
                GamePacket packet = node.Value;
                LinkedListNode <GamePacket> next = node.Next;
                mPacketList.Remove(node);

                if (packet != null)
                {
                    ProcessPacket(packet);
                    //	if (packet.data != null)
                    //		packet.data = null;
                }

                node = next;
            }
        }
Exemple #2
0
        private void ProcessPackets()
        {
            while (true)
            {
                LinkedListNode <GamePacket> node;
                lock (this) {
                    node = mPacketList.First;
                    mPacketList.RemoveFirst();
                }

                if (node == null)
                {
                    break;
                }
                GamePacket packet = node.Value;
                if (packet != null)
                {
                    if (packet.status == GamePacketStatus.GPNone)
                    {
                        ProcessPacket(packet);
                        // 如果为标记为正在处理
                        if (packet.status == GamePacketStatus.GPProcessing)
                        {
                            lock (this) {
                                mPacketList.AddFirst(node);
                            }
                        }
                    }
                    else if (packet.status == GamePacketStatus.GPProcessing)
                    {
                        lock (this) {
                            mPacketList.AddFirst(node);
                        }
                        break;
                    }
                }
            }
        }
Exemple #3
0
        // 子线程调用
        private void OnThreadBufferProcess(TcpClient tcp)
        {
            if (tcp == null)
            {
                return;
            }

            int recvsize = mTcpClient.GetReadDataNoLock(mRecvBuffer, mRecvSize);

            if (recvsize > 0)
            {
                mRecvSize += recvsize;
                int            recvBufSz = mRecvSize;
                int            i         = 0;
                GamePackHeader header    = new GamePackHeader();

                int    headerSize   = Marshal.SizeOf(header);
                IntPtr headerBuffer = Marshal.AllocHGlobal(headerSize);
                try {
                    while (recvBufSz - i >= headerSize)
                    {
                        Marshal.Copy(mRecvBuffer, i, headerBuffer, headerSize);
                        header = (GamePackHeader)Marshal.PtrToStructure(headerBuffer, typeof(GamePackHeader));
                        #if USE_NETORDER
                        // used Net
                        header.headerCrc32 = (uint)IPAddress.NetworkToHostOrder(header.headerCrc32);
                        header.dataCrc32   = (uint)IPAddress.NetworkToHostOrder(header.dataCrc32);
                        header.header      = IPAddress.NetworkToHostOrder(header.header);
                        header.dataSize    = IPAddress.NetworkToHostOrder(header.dataSize);
                        #endif
                        if ((recvBufSz - i) < (header.dataSize + headerSize))
                        {
                            break;
                        }
                        GamePacket packet = new GamePacket();
                        packet.header = header;
                        if (packet.header.dataSize <= 0)
                        {
                            packet.header.dataSize = 0;
                            packet.data            = null;
                        }
                        else
                        {
                            packet.data = new byte[packet.header.dataSize];
                            Buffer.BlockCopy(mRecvBuffer, i + headerSize, packet.data, 0, packet.header.dataSize);
                        }

                        LinkedListNode <GamePacket> node = new LinkedListNode <GamePacket>(packet);
                        lock (this)
                        {
                            mPacketList.AddLast(node);
                        }

                        i += headerSize + header.dataSize;
                    }
                } finally {
                    Marshal.FreeHGlobal(headerBuffer);
                }

                recvBufSz -= i;
                mRecvSize  = recvBufSz;
                if (recvBufSz > 0)
                {
                    Buffer.BlockCopy(mRecvBuffer, i, mRecvBuffer, 0, recvBufSz);
                }
            }
        }
Exemple #4
0
        public bool Execute()
        {
            //	string threadId = Thread.CurrentThread.ManagedThreadId.ToString ();
            //	Console.WriteLine (threadId);

            if (mTcpClient == null)
            {
                Stop();
                return(false);
            }

            if (mConnecting)
            {
                eClientState state = mTcpClient.GetState();
                if ((state == eClientState.eClient_STATE_CONNECTING) ||
                    (state == eClientState.eCLIENT_STATE_NONE))
                {
                    return(true);
                }

                if ((state == eClientState.eClient_STATE_CONNECT_FAIL) ||
                    (state == eClientState.eClient_STATE_ABORT))
                {
                    mConnecting = false;
                    mAbort      = false;
                    mTcpClient.Release();
                    mTcpClient = null;

                    // Call Event Error
                    if (mStateEvents != null)
                    {
                        mStateEvents(state);
                    }

                    return(false);
                }
                else if (state == eClientState.eClient_STATE_CONNECTED)
                {
                    mConnecting = false;
                    mAbort      = false;

                    // Call Event Success
                    if (mStateEvents != null)
                    {
                        mStateEvents(state);
                    }

                    return(true);
                }

                mConnecting = false;
            }

            if (mTcpClient.HasReadData())
            {
                int recvsize = mTcpClient.GetReadData(mRecvBuffer, mRecvSize);
                if (recvsize > 0)
                {
                    mRecvSize += recvsize;
                    int            recvBufSz = mRecvSize;
                    int            i         = 0;
                    GamePackHeader header    = new GamePackHeader();

                    int    headerSize   = Marshal.SizeOf(header);
                    IntPtr headerBuffer = Marshal.AllocHGlobal(headerSize);
                    try {
                        while (recvBufSz - i >= headerSize)
                        {
                            Marshal.Copy(mRecvBuffer, i, headerBuffer, headerSize);
                            header = (GamePackHeader)Marshal.PtrToStructure(headerBuffer, typeof(GamePackHeader));
#if USE_NETORDER
                            // used Net
                            header.headerCrc32 = (uint)IPAddress.NetworkToHostOrder(header.headerCrc32);
                            header.dataCrc32   = (uint)IPAddress.NetworkToHostOrder(header.dataCrc32);
                            header.header      = IPAddress.NetworkToHostOrder(header.header);
                            header.dataSize    = IPAddress.NetworkToHostOrder(header.dataSize);
#endif
                            if ((recvBufSz - i) < (header.dataSize + headerSize))
                            {
                                break;
                            }
                            GamePacket packet = new GamePacket();
                            packet.header = header;
                            if (packet.header.dataSize <= 0)
                            {
                                packet.header.dataSize = 0;
                                packet.data            = null;
                            }
                            else
                            {
                                packet.data = new byte[packet.header.dataSize];
                                Buffer.BlockCopy(mRecvBuffer, i + headerSize, packet.data, 0, packet.header.dataSize);
                            }

                            LinkedListNode <GamePacket> node = new LinkedListNode <GamePacket>(packet);
                            mPacketList.AddLast(node);

                            i += headerSize + header.dataSize;
                        }
                    } finally {
                        Marshal.FreeHGlobal(headerBuffer);
                    }

                    recvBufSz -= i;
                    mRecvSize  = recvBufSz;
                    if (recvBufSz > 0)
                    {
                        Buffer.BlockCopy(mRecvBuffer, i, mRecvBuffer, 0, recvBufSz);
                    }

                    ProcessPackets();
                    return(true);
                }
            }
            else
            {
                if (!mAbort)
                {
                    eClientState state = mTcpClient.GetState();
                    if (state == eClientState.eClient_STATE_ABORT)
                    {
                        mAbort = true;

                        // Call Event Abort
                        if (mStateEvents != null)
                        {
                            mStateEvents(state);
                        }

                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #5
0
        public bool Execute_QiPai()
        {
            if (mTcpClient == null)
            {
                canUpdate = false;
                return(false);
            }

            if (mConnecting)
            {
                eClientState state = mTcpClient.GetState();
                if ((state == eClientState.eClient_STATE_CONNECTING) ||
                    (state == eClientState.eCLIENT_STATE_NONE))
                {
                    return(true);
                }

                if ((state == eClientState.eClient_STATE_CONNECT_FAIL) ||
                    (state == eClientState.eClient_STATE_ABORT))
                {
                    mConnecting = false;
                    mAbort      = false;
                    mTcpClient.Release();
                    mTcpClient = null;

                    // Call Event Error
                    if (mStateEvents != null)
                    {
                        mStateEvents(state);
                    }

                    return(false);
                }
                else if (state == eClientState.eClient_STATE_CONNECTED)
                {
                    mConnecting = false;
                    mAbort      = false;

                    // Call Event Success
                    if (mStateEvents != null)
                    {
                        mStateEvents(state);
                    }

                    return(true);
                }

                mConnecting = false;
            }

            if (mTcpClient.HasReadData())
            {
                int recvsize = mTcpClient.GetReadData(mRecvBuffer, mRecvSize);
                if (recvsize > 0)
                {
                    mRecvSize += recvsize;
                    int recvBufSz = mRecvSize;
                    int i         = 0;
                    GamePackHeader_QiPai header = new GamePackHeader_QiPai();
                    int    headerSize           = Marshal.SizeOf(header);
                    IntPtr headerBuffer         = Marshal.AllocHGlobal(headerSize);
                    try
                    {
                        //可能粘包,用循环来切包
                        while (recvBufSz - i >= headerSize)
                        {
                            Marshal.Copy(mRecvBuffer, i, headerBuffer, headerSize);
                            header = (GamePackHeader_QiPai)Marshal.PtrToStructure(headerBuffer, typeof(GamePackHeader_QiPai));
                            string descStr = System.Text.Encoding.Default.GetString(header.desc);

                            if (descStr == "GTV1")
                            {
                            }

                            int msgId     = (header.msgId[0] << 8) + header.msgId[1];
                            int msgLength = (header.msgLength[0] << 8) + header.msgLength[1];

                            //断包,break掉,继续接收
                            if ((recvBufSz - i) < (msgLength + headerSize))
                            {
                                break;
                            }

                            byte[] dataBytes = new byte[msgLength];
                            Buffer.BlockCopy(mRecvBuffer, i + headerSize, dataBytes, 0, msgLength);

                            dataBytes = codeData(dataBytes);

                            GamePacket packet = new GamePacket();
                            packet.header = header;
                            packet.data   = dataBytes;

                            LinkedListNode <GamePacket> node = new LinkedListNode <GamePacket>(packet);
                            mPacketList.AddLast(node);

                            i += headerSize + msgLength;//当前包已处理完,偏移量移动,开始处理下一个包
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(headerBuffer);
                    }

                    recvBufSz -= i;
                    mRecvSize  = recvBufSz;
                    if (recvBufSz > 0)
                    {
                        Buffer.BlockCopy(mRecvBuffer, i, mRecvBuffer, 0, recvBufSz); //剩余的数据往前移
                    }
                    ProcessPackets();
                    return(true);
                }
            }
            else
            {
                if (!mAbort)
                {
                    eClientState state = mTcpClient.GetState();
                    if (state == eClientState.eClient_STATE_ABORT)
                    {
                        mAbort = true;

                        // Call Event Abort
                        if (mStateEvents != null)
                        {
                            mStateEvents(state);
                        }

                        return(false);
                    }
                }
            }

            return(true);
        }