Exemple #1
0
    /** 读出一个指定长度的utf字符串 */

    public string readUTF()
    {
        int len = readLength() - 1;

        if (len < 0)
        {
            return(null);
        }
        if (len == 0)
        {
            return(EMPTY_STRING);
        }
        if (len > MAX_DATA_LENGTH)
        {
            throw new Exception(this
                                + " readUTF, data overflow:" + len);
        }
        char[] temp = new char[len];
        int    n    = ByteKit.readUTF(array, position, len, temp);

        if (n < 0)
        {
            throw new Exception(this
                                + " readUTF, format err, len=" + len);
        }
        position += len;
        return(new String(temp, 0, n));
    }
Exemple #2
0
    public int readLength()
    {
        byte[] b1 = new byte[1];
        socket.Receive(b1, SocketFlags.None);
        int n = b1 [0];

        if (n >= 0x80)
        {
            return(n - 0x80);
        }
        else if (n >= 0x40)
        {
            b1 = new byte[1];
            socket.Receive(b1, SocketFlags.None);
            return((n << 8) + ByteKit.readUnsignedByte(b1, 0) - 0x4000);
        }
        else if (n >= 0x20)
        {
            b1 = new byte[3];
            return((n << 24) + (ByteKit.readUnsignedByte(b1, 0) << 16)
                   + ByteKit.readUnsignedByte(b1, 1) - 0x20000000);
        }
        else
        {
            throw new Exception(this.GetType() + ", readLength, invalid number:" + n + ", " + this);
        }
    }
Exemple #3
0
    /** 根据头信息创建字节缓存对象 */
    protected virtual ByteBuffer createDataByHead(ByteBuffer body)
    {
        int        len  = body.length();
        ByteBuffer data = new ByteBuffer();

        ByteKit.writeLength(data, len);
        data.writeBytes(body.toArray());
        return(data);
    }
Exemple #4
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);
        }
    }
Exemple #5
0
    public object bytesReadValue(ByteBuffer data)
    {
        uint len = (uint)data.readUnsignedShort();
        uint p   = (uint)data.position;
        uint tag = (uint)data.readUnsignedByte();

        //if(!MiniConnectManager.IsRobot)
        //MonoBehaviour.print ("bytesReadValue  len=" + len + "  p=" + p + "  tag=" + tag);
        if (tag == VER)
        {
            // 复杂数据结构 (二进制数据)
            return(ByteKit.complexAnalyse(data));
        }
        else
        {
            // 简单数据结构 byte=97 int=98 binary=109(暂时用不到) string=107(非二进制数据内的字符串)
            data.position = (int)p;
            return(ByteKit.simpleAnalyse(data));
        }
    }
Exemple #6
0
 override public void bytesRead(ByteBuffer data)
 {
     base.bytesRead(data);
     if (isTag(_tag))
     {
         int length = data.readInt();
         _value = new ErlType[length];
         ErlType erl;
         for (int i = 0; i < length; i++)
         {
             // length==3  ErlList的循环
             erl = ByteKit.natureAnalyse(data);
             if (!(erl is ErlByte || erl is ErlInt))
             {
                 isString = false;
             }
             _value [i] = erl;
         }
         data.readUnsignedByte();             // 读取列表结尾的空列表的tag标记
     }
 }
Exemple #7
0
    /** 写入一个utf字符串中指定的部分,可以为null */

    public void writeUTF(string str, int index, int length)
    {
        if (str == null)
        {
            writeLength(0);
            return;
        }
        int len = ByteKit.getUTFLength(str, index, length);

        writeLength(len + 1);
        if (len <= 0)
        {
            return;
        }
        int pos = top;

        if (array.Length < pos + len)
        {
            setCapacity(pos + len);
        }
        ByteKit.writeUTF(str, index, length, array, pos);
        top += len;
    }
Exemple #8
0
 override public void bytesRead(ByteBuffer data)
 {
     base.bytesRead(data);
     if (_tag == TAG [0])          // 小元组
     {
         int length = data.readUnsignedByte();
         //MonoBase.print ("small array bytesRead length=" + length);
         _value = new ErlType[length];
         for (int i = 0; i < length; i++)
         {
             _value [i] = ByteKit.natureAnalyse(data);
         }
     }
     else if (_tag == TAG [1])            // 大元组
     {
         int length = data.readInt();
         //MonoBase.print ("big array bytesRead length=" + length);
         _value = new ErlType[length];
         for (int i = 0; i < length; i++)
         {
             _value [i] = ByteKit.natureAnalyse(data);
         }
     }
 }
Exemple #9
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);
                }
            }
        }
    }