Exemple #1
0
    public void copyFrom(MyByteBuffer other)
    {
        if (other.mBuffer.Size <= 0)
        {
            return;
        }

        byte[] bytes = new byte[other.mBuffer.Size];
        other.readBytes(bytes);
        mBuffer.Put(bytes);
    }
Exemple #2
0
    private void onReceive(byte[] bytes, int len)
    {
        try
        {
            if (len > 0 && bytes != null)
            {
                if (len > mByteBuffer.canUse())
                {
                    MyByteBuffer byteBuffer = new MyByteBuffer(mByteBuffer.maxSize() * 2 + len, false);
                    byteBuffer.copyFrom(mByteBuffer);
                    mByteBuffer = byteBuffer;
                }

                mByteBuffer.put(bytes, len);

                do
                {
                    if (mByteBuffer.remaining() < 4)
                    {
                        break;
                    }

                    int tmp    = mByteBuffer.readInt32();
                    int bufLen = (tmp & 0x7FFFFF);
                    if (bufLen > mByteBuffer.maxSize() || bufLen <= 0)
                    {
                        Debug.LogError("数据长度超过限制: " + bufLen);
                        break;
                    }

                    if (mByteBuffer.remaining() < bufLen)
                    {
                        mByteBuffer.resetHead(4);
                        //Log(EventId.Net_Error, "remaining 长度不够:" + bufLen);
                        break;
                    }

                    int msgID = mByteBuffer.readInt32();

                    //
                    byte[] content = null;
                    if (bufLen - 4 > 0)
                    {
                        content = new byte[bufLen - 4];
                        mByteBuffer.readBytes(content);
                    }

                    lock (lockObj)
                    {
                        msgQueue.Enqueue(new MessageData {
                            msgID = msgID, data = content
                        });
                    }

                    // Logger.err("收到服务器数据:" + BitConverter.ToInt32(content, 0));

                    //////////MessageUtils.sendToGameLogic(msgId, content);
                }while (true);
            }
        }
        catch (Exception ex)
        {
            Logger.err(ex.ToString());
        }
    }