コード例 #1
0
        private static KeyValuePair <string, string> MakeWorkItem <T>(string name,
                                                                      IEnumerable <KeyValuePair <string, Action <T> > >
                                                                      builders) where T : IBuilderLite, new()
        {
            T builder = new T();

            foreach (KeyValuePair <string, Action <T> > item in builders)
            {
                item.Value(builder);
            }

            IMessageLite msg   = builder.WeakBuild();
            string       fname = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "unittest_" + name + ".dat");

            File.WriteAllBytes(fname, msg.ToByteArray());
            return
                (new KeyValuePair <string, string>(
                     String.Format("{0},{1}", msg.GetType().FullName, msg.GetType().Assembly.GetName().Name), fname));
        }
コード例 #2
0
 public Packet(ushort len, byte fid, byte module, byte msgid, byte resflag, IMessageLite pb)
 {
     //Debug.Log ("receive packet" );
     msglen       = len;
     flowId       = (byte)fid;
     moduleId     = module;
     this.msgid   = msgid;
     responseFlag = resflag;
     protoBody    = pb;
     MyLib.Util.Log("Packet:: readPacket " + fid);
     MyLib.Util.Log("Packet:: readPacket " + protoBody.GetType().FullName);
 }
コード例 #3
0
        public Packet(byte f, uint len, uint fid, byte module, ushort msg, uint restime, short resflag, IMessageLite pb)
        {
            //Debug.Log ("receive packet" );
            flag         = f;
            msglen       = len;
            flowId       = fid;
            moduleId     = module;
            msgid        = msg;
            responseTime = restime;
            responseFlag = resflag;

            protoBody = pb;
            Debug.Log("Packet:: readPacket " + fid);
            Debug.Log("Packet:: readPacket " + protoBody.GetType().FullName);
        }
コード例 #4
0
        protected void FormatterAssert <TMessage>(TMessage message, params string[] expecting) where TMessage : IMessageLite
        {
            StringWriter sw = new StringWriter();

            JsonFormatWriter.CreateInstance(sw).WriteMessage(message);

            Content = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasXmltest.RegisterAllExtensions(registry);

            IMessageLite copy =
                JsonFormatReader.CreateInstance(Content)
                .Merge(message.WeakCreateBuilderForType(), registry).WeakBuild();

            Assert.AreEqual(typeof(TMessage), copy.GetType());
            Assert.AreEqual(message, copy);
            foreach (string expect in expecting)
            {
                Assert.IsTrue(Content.IndexOf(expect) >= 0);
            }
        }
 /// <summary>
 /// For Lite exceptions that do not known how to enumerate missing fields
 /// </summary>
 public UninitializedMessageException(IMessageLite message)
     : base(String.Format("Message {0} is missing required fields", message.GetType()))
 {
     missingFields = new List <string>();
 }
コード例 #6
0
 /// <summary>
 /// For Lite exceptions that do not known how to enumerate missing fields
 /// </summary>
 public UninitializedMessageException(IMessageLite message)
     : base(String.Format("Message {0} is missing required fields", message.GetType()))
 {
     missingFields = new List<string>();
 }
コード例 #7
0
    //该类的核心就是process函数,一种基于if else的状态机机制,而且if else具有优先级的特性,可以继续下去
    //datas是整个缓冲区的大小,length是其中数据的长度,flowHandler是可能的处理函数
    public void process(byte[] datas, uint length, System.Collections.Generic.Dictionary <uint, MessageHandler> flowHandler)
    {
        //Debug.LogError("process receive Data " + length + " state " + state+" expect "+expectSize);
        uint totallen = 0;  //这里把totallen设置为局部变量就表明

        while (length > 0 && expectSize > 0)
        {
            if (state == READ_STATE.READ_STATE_MSGLEN)                                   //读取msg长度的模式
            {
                if (length >= expectSize)                                                //如果缓冲区数据长度大于可读取长度
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize); //够长就直接拷贝数据Copy(Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
                    totallen    += expectSize;                                           //totallen是记录从网络上读取缓冲区的长度
                    stream.wpos += (int)expectSize;                                      //stream.wpos是写入目标缓冲区的偏移量
                    length      -= expectSize;                                           //length是网络数据缓冲区的剩余长度

                    msglen = stream.readUint16();                                        //读取msglen
                    stream.clear();                                                      //清空本地缓冲区

                    state      = READ_STATE.READ_STATE_FLOWID;                           //改变接下来要读取的状态
                    expectSize = 1;                                                      //接下来要读取的数据长度大小
                }
                else                                                                     //如果不够读取的话,先读取length长度的数据,更新wpos和expectSize的大小,等待下一次读取
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else if (state == READ_STATE.READ_STATE_FLOWID)
            {
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    flowId = stream.readUint8();
                    stream.clear();

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

                    moduleId = stream.readUint8();
                    stream.clear();

                    state      = READ_STATE.READ_STATE_MSGID;
                    expectSize = 1;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
            else 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.readUint8();
                    stream.clear();

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

                    responseFlag = stream.readUint8();
                    stream.clear();

                    state = READ_STATE.READ_STATE_BODY;
                    //expectSize = msglen - 4 - 1 - 2 - 4 - 2;
                    expectSize = (uint)(msglen - 1 - 1 - 1 - 1);//flowId moduleId msgId
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }

            /*
             * body Can be empty
             */
            if (state == READ_STATE.READ_STATE_BODY)   //真正的Body消息体
            {
                //Debug.LogError("body expect BodySize:"+length+" expSize "+expectSize);
                if (length >= expectSize)
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                    totallen    += expectSize;
                    stream.wpos += (int)expectSize;
                    length      -= expectSize;

                    /*
                     * No Handler Or PushMessage  forward To IPacketHandler
                     * Call Who's RPC Method Or Register Many RPC Method to Handle It ?
                     * [PushHandler]
                     * void GCPushSpriteInfo(Packet packet) {
                     * }
                     *
                     * PacketHandler namespace
                     * IPacketHandler---->GCPushSpriteInfo
                     */
                    IMessageLite pbmsg = MyLib.Util.GetMsg(moduleId, msgid, stream.getBytString());  //通过moduleId,msgid找到对于的proto结构,然后把stream中的内容调用proto的库转换为proto结构
                    //Debug.LogError("expect msgType: "+pbmsg.GetType());
                    Packet p        = new Packet(msglen, flowId, moduleId, msgid, responseFlag, pbmsg);
                    var    fullName = pbmsg.GetType().FullName;
                    mainLoop.queueInLoop(() =>     //这里的mainloop就是用MonoBehaviour开出来的模拟线程
                    {
                        Debug.Log("ReadPacket: " + p.protoBody.ToString());
                    });

                    mainLoop.queueInLoop(() =>   //放到Mainloop中进行消息处理
                    {
                        MessageHandler handler = null;
                        if (flowHandler == null)
                        {
                            handler = msgHandle;                  //msgHandle由RemoteClient提供
                        }
                        else if (flowHandler.ContainsKey(flowId)) //如果需要对该flowId特殊处理,在这里完成,对于由客户端发起带有Flowid的消息,服务器会回一个同样Flowid的消息
                        {
                            handler = flowHandler[flowId];
                            flowHandler.Remove(flowId);
                            if (handler == null)
                            {
                                Debug.LogError("FlowHandlerIsNull: " + flowId);
                            }
                        }
                        else
                        {
                            handler = msgHandle;   //,默认也是由RemoteClient提供msgHandle处理
                        }
                        if (handler != null)
                        {
                            handler(p);
                        }
                        else
                        {
                            Debug.LogError("MessageReader::process No handler for flow Message " + msgid + " " + flowId + " " + pbmsg.GetType() + " " + pbmsg);
                        }
                    });


                    //fullName是检测handlePB()中返回的继承该IMessageLite接口的类是否带有Push字段,带有该字段的协议是由服务器主动发起,

                    //Debug.LogError("HandlerIs: "+flowId+" h "+handler);
                    if (fullName.Contains("Push"))
                    {
                        //Log.Net("MessageReader Handler PushMessage");
                        if (mainLoop != null)
                        {
                            mainLoop.queueInLoop(delegate
                            {
                                var handlerName = fullName.Replace("MyLib", "PacketHandler");
                                var tp          = Type.GetType(handlerName); //通过type的名字找到该Type
                                if (tp == null)
                                {
                                    Debug.LogError("PushMessage noHandler " + handlerName);
                                }
                                else
                                {
                                    //Debug.Log("Handler Push Message here "+handlerName);
                                    var ph = (IPacketHandler)Activator.CreateInstance(tp); //Activator .CreateInstance()用于创建类的实例
                                    ph.HandlePacket(p);                                    //IPacketHandler提供了HandlePacket的方法,该IPacketHandler接口主要是对协议中的Push字段(服务器单方消息)进行处理的接口
                                }
                            });
                        }
                    }

                    stream.clear();
                    state      = READ_STATE.READ_STATE_MSGLEN;
                    expectSize = 2;
                }
                else
                {
                    Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                    stream.wpos += (int)length;
                    expectSize  -= length;
                    break;
                }
            }
        }

        if (responseFlag != 0)
        {
            Debug.LogError("MessageReader:: read Error Packet " + responseFlag);
        }

        //Log.Net("current state after " + state + " msglen " + msglen + " " + length);
        //Log.Net("MessageReader::  prop  flag" + flag + "  msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + responseTime + " responseFlag " + responseFlag + " expectSize " + expectSize);
    }
コード例 #8
0
        public void process(byte[] datas, MessageLength length, Dictionary <uint, MessageHandler> flowHandler)
        {
            //Log.Net("process receive Data " + length + " state " + state);
            MessageLength totallen = 0;

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

                        flag = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGLEN;
                        expectSize = 4;
                    }
                    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.readUint32();
                        stream.clear();

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

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

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

                        moduleId = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else 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();

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

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

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

                        responseFlag = stream.readInt16();
                        stream.clear();

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

                /*
                 * body Can be empty
                 */
                if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        /*
                         * No Handler Or PushMessage  forward To IPacketHandler
                         * Call Who's RPC Method Or Register Many RPC Method to Handle It ?
                         * [PushHandler]
                         * void GCPushSpriteInfo(Packet packet) {
                         * }
                         *
                         * PacketHandler namespace
                         * IPacketHandler---->GCPushSpriteInfo
                         */
                        MessageHandler handler = null;
                        if (flowHandler == null)
                        {
                            handler = msgHandle;
                        }
                        else if (flowHandler.ContainsKey(flowId))
                        {
                            handler = flowHandler [flowId];
                            flowHandler.Remove(flowId);
                        }



                        //Message msg = new Message();
                        IMessageLite pbmsg    = KBEngine.Message.handlePB(moduleId, msgid, stream);
                        Packet       p        = new Packet(flag, msglen, flowId, moduleId, msgid, responseTime, responseFlag, pbmsg);
                        var          fullName = pbmsg.GetType().FullName;
                        //Bundle.recvMsg.Add("recvMsg " + fullName + " : " + flowId);
                        //Log.Net("RecvMsg: "+fullName+" f "+flowId);

                        if (fullName.Contains("Push"))
                        {
                            //Log.Net("MessageReader Handler PushMessage");
                            if (mainLoop != null)
                            {
                                mainLoop.queueInLoop(delegate
                                {
                                    var handlerName = fullName.Replace("ChuMeng", "PacketHandler");
                                    var tp          = Type.GetType(handlerName);
                                    if (tp == null)
                                    {
                                        Debug.LogError("PushMessage noHandler " + handlerName);
                                    }
                                    else
                                    {
                                        //Debug.Log("Handler Push Message here "+handlerName);
                                        var ph = (PacketHandler.IPacketHandler)Activator.CreateInstance(tp);
                                        ph.HandlePacket(p);
                                    }
                                });
                            }
                        }
                        else if (handler != null)
                        {
                            mainLoop.queueInLoop(() => {
                                handler(p);
                            });
                        }
                        else
                        {
                            //flowHandler.Remove(flowId);
                            Debug.LogError("MessageReader::process No handler for flow Message " + msgid + " " + flowId + " " + pbmsg.GetType() + " " + pbmsg);
                        }



                        stream.clear();

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

            if (responseFlag != 0)
            {
                Debug.LogError("MessageReader:: read Error Packet " + responseFlag);
            }

            //Log.Net("current state after " + state + " msglen " + msglen + " " + length);
            //Log.Net("MessageReader::  prop  flag" + flag + "  msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + responseTime + " responseFlag " + responseFlag + " expectSize " + expectSize);
        }