Beispiel #1
0
    /// <summary>
    /// 接收服务器的数据
    /// </summary>
    private void ReceiveServerMsgThread()
    {
        ushort wCmdInfoSize = (ushort)Marshal.SizeOf(new CMD_Info());
        ushort wCmdSize     = (ushort)Marshal.SizeOf(new CMD_Command());

        byte[] byCmdInfoArr = new byte[wCmdInfoSize];           // 命令信息数组
        byte[] byCmdArr     = new byte[wCmdSize];               // 命令数组
        byte[] byBodyArr    = null;                             // 数据主体
        byte[] byOneRevBuf  = new byte[1024];                   // 每次接收数据的缓存
        int    nOneRevSize  = 0;                                // 每次接收数据的长度

        byte[] byAllRevBuf = new byte[1024 * 5];                // 总的接收数据缓冲
        ushort nAllRevSize = 0;                                 // 总的接收数据长度
        ushort wPacketSize = 0;                                 // 数据包的长度

        byte[]      byCrevDataArr = null;                       // 解密出来的数据数组
        CMD_Info    stCmdInfo;
        CMD_Command stCmd;

        while (m_ClientSocket.Connected)
        {
            try
            {
                nOneRevSize = m_ClientSocket.Receive(byOneRevBuf);
                if (nOneRevSize <= 0)
                {
                    m_ClientSocket.Close();
                    return;
                }

                // 缓存数据
                Array.Copy(byOneRevBuf, 0, byAllRevBuf, nAllRevSize, nOneRevSize);
                nAllRevSize += (ushort)nOneRevSize;

                while (nAllRevSize > wCmdInfoSize)
                {
                    // 获取数据头部信息
                    Array.Copy(byAllRevBuf, 0, byCmdInfoArr, 0, wCmdInfoSize);//version,checkcode,size
                    stCmdInfo   = ClientConstant.ByteToStructure <CMD_Info>(byCmdInfoArr);
                    wPacketSize = stCmdInfo.wPacketSize;

                    // 缓存的数据是否足够多,来解释出一个数据
                    if (nAllRevSize < wPacketSize)
                    {
                        break;
                    }

//					byte[] tmp = new byte[wPacketSize- wCmdInfoSize];//
                    // 网狐映射解密数据
                    byte byCheckCode = stCmdInfo.cbCheckCode;
                    for (int nI = 4; nI < wPacketSize; nI++)                     //- wCmdInfoSize
                    {
                        byAllRevBuf[nI] = WHMapEncrypt.MapRecvByte(byAllRevBuf[nI]);
                        byCheckCode    += byAllRevBuf[nI];
                    }

                    // 获取命令
                    Array.Copy(byAllRevBuf, wCmdInfoSize, byCmdArr, 0, wCmdSize);
                    stCmd = ClientConstant.ByteToStructure <CMD_Command>(byCmdArr);


                    // 获取数据主体
                    byBodyArr = new byte[wPacketSize - wCmdInfoSize - wCmdSize];
                    Array.Copy(byAllRevBuf, wCmdSize + wCmdInfoSize, byBodyArr, 0, byBodyArr.Length);


                    // 处理接收到的数据
                    HandleServerData(byCheckCode, stCmd.wMainCmdID, stCmd.wSubCmdID, byBodyArr);

                    // 清空这次被使用的数据
                    for (int nI = 0; nI < nAllRevSize - wPacketSize; nI++)
                    {
                        byAllRevBuf[nI] = byAllRevBuf[wPacketSize + nI];
                    }
                    nAllRevSize -= wPacketSize;
                }
            }
            catch (Exception ex)
            {
                ClientDelegate.OnShowMsgEvent("数据异常:" + ex.Message);
                m_ClientSocket.Close();
                break;
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// 向服务器发送数据(直接向服务器发送数据)
    /// </summary>
    public bool SendFirstMessage(ushort wMainCmdID, ushort wSubCmdID, byte[] byBodyArr)
    {
        bool        bResult   = false;
        CMD_Info    stCmdInfo = new CMD_Info();
        CMD_Command stCmd     = new CMD_Command();

        byte[] byEncryBuf   = null;                                 // 需要加密的数据的数组
        byte[] byCmdArr     = null;                                 // 命令数组
        byte[] byCmdInfoArr = null;                                 // 命令信息数组
        byte[] bySendArr    = null;                                 // 需要发送的数据
        ushort wPacketSize  = 0;                                    // 发送数据的包长


        if (m_ClientSocket == null || !m_ClientSocket.Connected)
        {
            ClientDelegate.ShowMsgEvent("Socket已经关闭");
            return(false);
        }

        try
        {
            // 组装命令体
            stCmd.wMainCmdID = wMainCmdID;
            stCmd.wSubCmdID  = wSubCmdID;
            byCmdArr         = ClientConstant.StructureToByte <CMD_Command>(stCmd);

            // 组合需要加密的数据
            byEncryBuf = new byte[byCmdArr.Length + byBodyArr.Length];            //
            Array.Copy(byCmdArr, 0, byEncryBuf, 0, byCmdArr.Length);
            Array.Copy(byBodyArr, 0, byEncryBuf, byCmdArr.Length, byBodyArr.Length - byCmdArr.Length);

            // 网狐映射加密数据
            byte byCheckCode = 0;
            for (int nI = 0; nI < byEncryBuf.Length; nI++)
            {
                byCheckCode   += byEncryBuf[nI];
                byEncryBuf[nI] = WHMapEncrypt.MapSendByte(byEncryBuf[nI]);
            }

            // 数据长度
            wPacketSize = (ushort)(byBodyArr.Length + m_nPkgHeaderSize);

            // 组装命令信息体
            stCmdInfo.cbVersion   = 0x01;         //0x66
            stCmdInfo.cbCheckCode = (byte)(~byCheckCode + (byte)1);
            stCmdInfo.wPacketSize = wPacketSize;
            byCmdInfoArr          = ClientConstant.StructureToByte <CMD_Info>(stCmdInfo);

            // 整合发送的数据
            bySendArr = new byte[byCmdInfoArr.Length + byEncryBuf.Length];
            Array.Copy(byCmdInfoArr, 0, bySendArr, 0, byCmdInfoArr.Length);
            Array.Copy(byEncryBuf, 0, bySendArr, byCmdInfoArr.Length, byEncryBuf.Length);

            m_ClientSocket.Send(bySendArr);
        }
        catch (Exception ex)
        {
            ClientDelegate.ShowMsgEvent("发送数据错误:" + ex.Message);
        }

        bResult = true;
        return(bResult);
    }