Ejemplo n.º 1
0
        public override void process(byte[] datas, MessageLengthEx offset, MessageLengthEx length)
        {
            MessageLengthEx totallen = offset;

            while (length > 0 && expectSize > 0)
            {
                if (state == READ_STATE.READ_STATE_MSGID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;
                        msgid        = stream.readUint16();
                        stream.clear();

                        Message msg = Messages.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else if (msg.msglen == 0)
                        {
                            // 如果是0个参数的消息,那么没有后续内容可读了,处理本条消息并且直接跳到下一条消息
                                                        #if UNITY_EDITOR
                            Dbg.profileStart(msg.name);
                                                        #endif

                            msg.handleMessage(stream);

                                                        #if UNITY_EDITOR
                            Dbg.profileEnd(msg.name);
                                                        #endif

                            state      = READ_STATE.READ_STATE_MSGID;
                            expectSize = 2;
                        }
                        else
                        {
                            expectSize = (MessageLengthEx)msg.msglen;
                            state      = READ_STATE.READ_STATE_BODY;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        msglen = stream.readUint16();
                        stream.clear();

                        // 长度扩展
                        if (msglen >= 65535)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN_EX;
                            expectSize = 4;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = msglen;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN_EX)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        expectSize = stream.readUint32();
                        stream.clear();

                        state = READ_STATE.READ_STATE_BODY;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        stream.append(datas, totallen, expectSize);
                        totallen += expectSize;
                        length   -= expectSize;

                        Message msg = Messages.clientMessages[msgid];

#if UNITY_EDITOR
                        Dbg.profileStart(msg.name);
#endif

                        msg.handleMessage(stream);

#if UNITY_EDITOR
                        Dbg.profileEnd(msg.name);
#endif

                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        stream.append(datas, totallen, length);
                        expectSize -= length;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void process(byte[] datas, MessageLength length)
        {
            MessageLength totallen = 0;

            while (length > 0 && expectSize > 0)
            {
                if (state == READ_STATE.READ_STATE_MSGID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += expectSize;
                        length      -= expectSize;

                        msgid = stream.readUint16();
                        stream.clear();

                        Message msg = Message.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = (MessageLength)msg.msglen;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += expectSize;
                        length      -= expectSize;

                        msglen = stream.readUint16();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_BODY;
                        expectSize = msglen;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += expectSize;
                        length      -= expectSize;

                        Message msg = Message.clientMessages[msgid];

                        msg.handleMessage(stream);
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        expectSize  -= length;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void process(byte[] datas, MessageLength length)
        {
            MessageLength totallen = 0;

            while (length > 0 && expectSize > 0)
            {
                if (state == READ_STATE.READ_STATE_MSGID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        length    -= expectSize;
                        expectSize = 0;

                        msgid = stream.readUint16();
                        stream.clear();

                        Message msg = Message.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = (MessageLength)msg.msglen;
                        }

                        totallen += 2;
                        //Dbg.DEBUG_MSG(string.Format("MessageReader::process(): msgid={0}/{1}/{2}, msgname={3}!",
                        //	msg.id, msg.msglen, expectSize, msg.name));
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        length -= expectSize;

                        msglen = stream.readUint16();
                        stream.clear();

                        // Dbg.DEBUG_MSG(string.Format("MessageReader::process(): msglen={0}!", msglen));

                        state      = READ_STATE.READ_STATE_BODY;
                        expectSize = msglen;
                        totallen  += 2;
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        length   -= expectSize;
                        totallen += expectSize;
                        Message msg = Message.clientMessages[msgid];
                        stream.wpos += expectSize;

                        // Dbg.DEBUG_MSG(string.Format("MessageReader::process(): handleMessage={0}, msglen={1}, rpos({2}), wpos({3}), msg={4}!",
                        //	msg.name, stream.opsize(), stream.rpos, stream.wpos, stream.toString()));

                        msg.handleMessage(stream);
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
            }
        }