Example #1
0
    /** 解析单次消息内容 */
    public void parseMessage(ByteBuffer socketbuffer)
    {
        int  versionInfo = socketbuffer.readByte();
        bool encryption  = ((versionInfo & 8) != 0);
        bool crc         = ((versionInfo & 4) != 0);
        bool compress    = ((versionInfo & 2) != 0);
        //if(!MiniConnectManager.IsRobot)
        //MonoBehaviour.print ("length=>" + length + "  versionInfo=>" + versionInfo + "  encryption=>" + encryption + "  crc=>" + crc + "  compress=>" + compress);
        ByteBuffer data = new ByteBuffer(length - 1);

        data.write(socketbuffer.toArray(), 0, length - 1);

        //为下次数据处理做判断
        if (socket.Available >= 2)
        {
            byte[] b = new byte[2];
            socket.Receive(b, SocketFlags.None);
            length = ByteKit.readUnsignedShort(b, 0);
        }
        else
        {
            length = 0;
        }

        if (encryption)
        {
            data = encryptionCode(data, _receiveChallengeCode);
        }
        if (compress)
        {
            byte[] bb = ZIPUtil.Decompress(data.toArray());
            data = new ByteBuffer(bb);
        }

        if (crc)
        {
            int        crcValue = data.readInt();
            ByteBuffer data1    = new ByteBuffer();
            data1.writeBytes(data.toArray(), 0, (data.top - data.position));
            int nowCrc = (int)ChecksumUtil.Adler32(data1);
            if (crcValue != nowCrc)
            {
                MonoBehaviour.print("crc is err,crcValue" + crcValue + ",nowCrc=" + nowCrc);
                return;
            }
        }
        ErlKVMessage message = new ErlKVMessage(null);

        message.bytesRead(data);
        if (_portHandler != null)          // _portHandler可以是DataAccess或者ErlTransmitPort,如果要保存funcUid就要设置为DataAccess
        {
            _portHandler.erlReceive(this, message);
        }
    }
Example #2
0
    /** 连接的消息接收方法 */
    public override void receive()
    {
        if (GameManager.Instance.disconnetTest)
        {
            return;
        }

        if (!socket.Connected)
        {
            return;
        }

        ActiveTime = TimeKit.getMillisTime();
        if (socket.Available > 0)
        {
            if (!_isConnectReady)
            {
                //设置 _isConnectReady=true  connect pk receive
                //抛掉前两位
                byte[] b1 = new byte[1];
                socket.Receive(b1, SocketFlags.None);
                byte[] b2 = new byte[1];
                socket.Receive(b2, SocketFlags.None);

                byte[] b3 = new byte[4];
                socket.Receive(b3, SocketFlags.None);
                Array.Reverse(b3);
                int i = BitConverter.ToInt32(b3, 0);

                byte[] b4 = new byte[4];
                socket.Receive(b4, SocketFlags.None);
                Array.Reverse(b4);
                int ii = BitConverter.ToInt32(b4, 0);

                _sendChallengeCode    = getPK(i);
                _receiveChallengeCode = getPK(ii);
                _isConnectReady       = true;
                if (this.CallBack != null)
                {
                    this.CallBack();
                }
            }
            else
            {
                if (length <= 0)
                {
                    if (socket.Available < 2)
                    {
                        return;
                    }
                    //	length = data.readUnsignedShort ();
                    byte[] b = new byte[2];
                    socket.Receive(b, SocketFlags.None);
                    length = ByteKit.readUnsignedShort(b, 0);
                    //length = readLength ();
                }
                if (length > 0 && socket.Available >= length)
                {
                    ByteBuffer data = new ByteBuffer(length);
                    data.setTop(length);
                    socket.Receive(data.getArray(), SocketFlags.None);

                    parseMessage(data);
                }
            }
        }
    }