Exemplo n.º 1
0
        /// <summary>
        /// LUA带有嵌入式消息读取
        ///
        /// 测试流程:
        ///  1、C#中生成消息实例,并给消息字段赋值
        ///  2、用C#的写入方式写入流中
        ///  3、用lua的解析方式方式从流中读取数据,生成对应lua消息类型实例
        ///
        /// 消息定义文件:
        ///  1、lua部分:
        ///     文件: protoMsgDef.lua.txt
        ///     定义: idMsg=900000, table="lua_message", nestedTable="lua_nested"
        ///
        ///  2、C#部分
        ///     文件: Analys.proto
        ///     定义: message=lua_message, message=lua_nested
        ///
        /// </summary>
        static void luaProtoMsgWithNestR()
        {
            Msg.lua_message msg = new Msg.lua_message();
            msg.value_repeated.Add(new Msg.lua_nested()
            {
                value_int32  = 1,
                value_bool   = true,
                value_bytes  = System.Text.Encoding.UTF8.GetBytes("Hello"),
                value_string = "Oh",
            });

            msg.value_repeated.Add(new Msg.lua_nested()
            {
                value_int32  = 2,
                value_bool   = true,
                value_bytes  = System.Text.Encoding.UTF8.GetBytes("World"),
                value_string = "Yes",
            });

            using (MemoryStream stream = new MemoryStream())
            {
                //序列化
                ProtoBuf.Serializer.Serialize(stream, msg);

                //反序列化
                luaMessage msgRev = null;
                using (var desializeStream = new MemoryStream(stream.ToArray(), 0, (int)stream.Length))
                {
                    using (luaProtoReader reader = luaProtoReader.createLuaProtoReader(desializeStream))
                    {
                        if (reader != null)
                        {
                            msgRev = reader.readLuaMessage(900000);
                        }
                    }

                    //消息发送测试
                    luaProtobuf.getInstance().receiveMsg(900000, desializeStream);
                }

                //打印结果
                string lsss = msgRev.logStr();
                LogSys.Log(lsss);

                if (msgRev != null)
                {
                    LogSys.Log("_LUA_PROTO_R_WITH_NEST_ success!");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// LUA无嵌入式消息读取
        ///
        /// 测试流程:
        ///  1、从lua中读取消息数据表(table)
        ///  2、用C#的写入方式写入流中
        ///  3、用lua的解析方式方式从流中读取数据,生成对应lua消息类型实例
        ///
        /// 消息定义文件:
        ///  1、lua部分:
        ///     文件: protoMsgDef.lua.txt
        ///     定义: idMsg=900001, table="lua_message2"
        ///
        ///  2、C#部分
        ///     文件: Analys.proto
        ///     定义: message=lua_message2
        ///
        /// </summary>
        static void luaProtoMsgWithoutNestR()
        {
            using (MemoryStream mem = new MemoryStream())
            {
                var msg2 = new Msg.lua_message2();
                msg2.value_int32  = 1;
                msg2.value_uint32 = 2;
                msg2.value_int64  = 3;
                msg2.value_uint64 = 4;
                msg2.value_bool   = true;
                msg2.value_bytes  = System.Text.Encoding.UTF8.GetBytes("I have a dream!");
                msg2.value_float  = 5.0f;
                msg2.value_double = 6.0;
                msg2.value_string = "All men are created equal!";
                ProtoBuf.Serializer.Serialize(mem, msg2);

                MemoryStream desializeStream = null;
                luaMessage   msgRev          = null;
                try
                {
                    desializeStream = new System.IO.MemoryStream(mem.ToArray(), 0, (int)mem.Length);
                    using (luaProtoReader reader = luaProtoReader.createLuaProtoReader(desializeStream))
                    {
                        if (reader != null)
                        {
                            msgRev = reader.readLuaMessage(900001);
                        }
                    }
                }
                finally
                {
                    if (desializeStream != null)
                    {
                        desializeStream.Dispose();
                    }

                    desializeStream = null;

                    //打印结果
                    string lsss = msgRev.logStr();
                    LogSys.Log(lsss);

                    if (msgRev != null)
                    {
                        LogSys.Log("_LUA_PROTO_R_WITHOUT_NEST_ success!");
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 抛出流结束符异常
 /// </summary>
 private Exception eof(luaProtoReader reader = null)
 {
     return(new EndOfStreamException());
 }