Пример #1
0
    /// <summary>
    /// 封装数据包
    /// </summary>
    /// <param name="data">要发送的数据</param>
    /// <returns>封装好的数据</returns>
    private byte[] MakeDatas(byte[] data, int protoCode)
    {
        int  leng = 0;
        bool isCompress;

        if (data == null)
        {
            leng       = 5;
            isCompress = false;
        }
        else
        {
            //data = EncryptUtil.NetEncrypt(data, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected);
            isCompress = data.Length >= COMPRESS_LENGTH;
            if (isCompress)
            {
                //data = GZipCompress.Compress(data);
            }
            leng = data.Length + 5;
        }
        byte[] head = new byte[4];
        head[0] = (byte)((leng & 0xFF000000) >> 24);
        head[1] = (byte)((leng & 0x00FF0000) >> 16);
        head[2] = (byte)((leng & 0x0000FF00) >> 8);
        head[3] = (byte)((leng & 0x000000FF));

        byte[] protoCodeBuffer = BitConverter.GetBytes(protoCode);
        Array.Reverse(protoCodeBuffer);

        using (MemoryStreamExt ms = new MemoryStreamExt())
        {
            ms.Write(head, 0, 4);
            ms.WriteBool(isCompress);
            ms.Write(protoCodeBuffer, 0, protoCodeBuffer.Length);
            if (data != null)
            {
                ms.Write(data, 0, data.Length);
            }
            return(ms.ToArray());
        }
        return(null);
    }
Пример #2
0
    /// <summary>
    /// 接收数据回调
    /// </summary>
    /// <param name="ar"></param>
    private void ReceiveCallBack(IAsyncResult ar)
    {
        if (Socket == null || !Socket.Connected)
        {
            //Debug.Log("接收数据回调返回");
            return;
        }
        try
        {
            int nLength = Socket.EndReceive(ar);
            //Debug.Log("从套接字缓冲区读取了" + nLength.ToString() + "长度的数据");
            if (nLength > 0)
            {
                m_ReceiveMemoryStream.Position = m_ReceiveMemoryStream.Length;
                m_ReceiveMemoryStream.Write(m_ReceiveBuffer, 0, nLength);



                if (m_ReceiveMemoryStream.Length > DATA_HEAD_LENGTH)
                {
                    while (true)
                    {
                        m_ReceiveMemoryStream.Position = 0;

                        m_ReceiveMemoryStream.Read(m_HeadData, 0, DATA_HEAD_LENGTH);
                        int currentMsgLen     = ((m_HeadData[0] & 0xff) << 24) + ((m_HeadData[1] & 0xff) << 16) + ((m_HeadData[2] & 0xff) << 8) + (m_HeadData[3] & 0xff);
                        int currentFullMsgLen = DATA_HEAD_LENGTH + currentMsgLen;
                        if (m_ReceiveMemoryStream.Length >= currentFullMsgLen) // 说明至少接收到了一个整包
                        {
                            Debug.Log("客户端收到了一个" + currentFullMsgLen + "长度的消息");
                            byte[] buffer = new byte[currentMsgLen];
                            m_ReceiveMemoryStream.Position = DATA_HEAD_LENGTH;
                            m_ReceiveMemoryStream.Read(buffer, 0, currentMsgLen);
                            //buffer = EncryptUtil.NetEncrypt(buffer, SystemProxy.Instance.NetKey, SystemProxy.Instance.NetCorrected);
                            m_ReceiveQueue.Enqueue(buffer);


                            //===============处理剩余字节数组======================

                            int remainLen = (int)m_ReceiveMemoryStream.Length - currentFullMsgLen;
                            if (remainLen > 0)
                            {
                                m_ReceiveMemoryStream.Position = currentFullMsgLen;
                                byte[] remainBuffer = new byte[remainLen];
                                m_ReceiveMemoryStream.Read(remainBuffer, 0, remainLen);
                                m_ReceiveMemoryStream.Position = 0;
                                m_ReceiveMemoryStream.SetLength(0);

                                m_ReceiveMemoryStream.Write(remainBuffer, 0, remainBuffer.Length);
                                remainBuffer = null;
                            }
                            else // 没有剩余字节
                            {
                                m_ReceiveMemoryStream.Position = 0;
                                m_ReceiveMemoryStream.SetLength(0);
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                ReceiveMessage();
            }
            else
            {
                Debug.LogWarning(string.Format("{0}断开连接", m_isClientClose ? "客户端断的" : "服务器断的"));
                Close(false);
            }
        }
        catch (Exception ex)
        {
            Debug.LogWarning(string.Format("{0}断开连接,{1}", m_isClientClose ? "客户端断的" : "服务器断的", ex));
            Close(false);
        }
    }