TestProtoBuffer.cs

  1. //#define USE_PROTOBUF_NET
  2. using UnityEngine;
  3. using System.Collections;
  4. using LuaInterface;
  5. using System;
  6. using System.IO;
  7. #if USE_PROTOBUF_NET
  8. using ProtoBuf;
  9. [ProtoContract]
  10. class Person
  11. {
  12. [ProtoMember(1, IsRequired = true)]
  13. public int id { get; set; }
  14. [ProtoMember(2, IsRequired = true)]
  15. public string name { get; set; }
  16. [ProtoMember(3, IsRequired = false)]
  17. public string email { get; set; }
  18. }
  19. #endif
  20. public class TestProtoBuffer : LuaClient
  21. {
  22. private string script = @"
  23. local person_pb = require 'Protol.person_pb'
  24. function Decoder()
  25. local msg = person_pb.Person()
  26. msg:ParseFromString(TestProtol.data)
  27. print('person_pb decoder: '..tostring(msg))
  28. end
  29. function Encoder()
  30. local msg = person_pb.Person()
  31. msg.id = 1024
  32. msg.name = 'foo'
  33. msg.email = 'bar'
  34. local pb_data = msg:SerializeToString()
  35. TestProtol.data = pb_data
  36. end
  37. ";
  38. private string tips = "";
  39. //实际应用如Socket.Send(LuaStringBuffer buffer)函数发送协议, 在lua中调用Socket.Send(pb_data)
  40. //读取协议 Socket.PeekMsgPacket() {return MsgPacket}; lua 中,取协议字节流 MsgPack.data 为 LuaStringBuffer类型
  41. //msg = Socket.PeekMsgPacket()
  42. //pb_data = msg.data
  43. new void Awake()
  44. {
  45. #if UNITY_5
  46. Application.logMessageReceived += ShowTips;
  47. #else
  48. Application.RegisterLogCallback(ShowTips);
  49. #endif
  50. base.Awake();
  51. }
  52. protected override LuaFileUtils InitLoader()
  53. {
  54. return new LuaResLoader();
  55. }
  56. protected override void Bind()
  57. {
  58. base.Bind();
  59. luaState.BeginModule(null);
  60. TestProtolWrap.Register(luaState);
  61. luaState.EndModule();
  62. }
  63. //屏蔽,例子不需要运行
  64. protected override void CallMain() { }
  65. protected override void OnLoadFinished()
  66. {
  67. base.OnLoadFinished();
  68. luaState.DoString(script, "TestProtoBuffer.cs");
  69. #if !USE_PROTOBUF_NET
  70. LuaFunction func = luaState.GetFunction("Encoder");
  71. func.Call();
  72. func.Dispose();
  73. func = luaState.GetFunction("Decoder");
  74. func.Call();
  75. func.Dispose();
  76. func = null;
  77. #else
  78. Person data = new Person();
  79. data.id = 2048;
  80. data.name = "foo";
  81. data.email = "bar";
  82. MemoryStream stream = new MemoryStream();
  83. Serializer.Serialize<Person>(stream, data);
  84. byte[] buffer = stream.ToArray();
  85. TestProtol.data = new LuaByteBuffer(buffer);
  86. LuaFunction func = luaState.GetFunction("Decoder");
  87. func.Call();
  88. func.Dispose();
  89. func = null;
  90. func = luaState.GetFunction("Encoder");
  91. func.Call();
  92. func.Dispose();
  93. func = null;
  94. stream = new MemoryStream(TestProtol.data.buffer);
  95. data = Serializer.Deserialize<Person>(stream);
  96. Debugger.Log("Decoder from lua fixed64 is: {0}", data.id);
  97. #endif
  98. }
  99. void ShowTips(string msg, string stackTrace, LogType type)
  100. {
  101. tips = tips + msg + "\r\n";
  102. }
  103. void OnGUI()
  104. {
  105. GUI.Label(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 100, 400, 300), tips);
  106. }
  107. new void OnApplicationQuit()
  108. {
  109. base.Destroy();
  110. #if UNITY_5
  111. Application.logMessageReceived -= ShowTips;
  112. #else
  113. Application.RegisterLogCallback(null);
  114. #endif
  115. }
  116. }

TestProtol.cs

  1. using System;
  2. using LuaInterface;
  3. public static class TestProtol
  4. {
  5. [LuaByteBufferAttribute]
  6. public static byte[] data;
  7. }

TestProtolWrap.cs

  1. //this source code was auto-generated by tolua#, do not modify it
  2. using System;
  3. using LuaInterface;
  4. public class TestProtolWrap
  5. {
  6. public static void Register(LuaState L)
  7. {
  8. L.BeginStaticLibs("TestProtol");
  9. L.RegVar("data", get_data, set_data);
  10. L.EndStaticLibs();
  11. }
  12. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  13. static int get_data(IntPtr L)
  14. {
  15. try
  16. {
  17. LuaDLL.tolua_pushlstring(L, TestProtol.data, TestProtol.data.Length);
  18. return 1;
  19. }
  20. catch(Exception e)
  21. {
  22. return LuaDLL.toluaL_exception(L, e);
  23. }
  24. }
  25. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  26. static int set_data(IntPtr L)
  27. {
  28. try
  29. {
  30. byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
  31. TestProtol.data = arg0;
  32. return 0;
  33. }
  34. catch(Exception e)
  35. {
  36. return LuaDLL.toluaL_exception(L, e);
  37. }
  38. }
  39. }

person.proto

  1. message Person {
  2. required int32 id = 1;
  3. required string name = 2;
  4. optional string email = 3;
  5. extensions 10 to max;
  6. }
  7. message Phone {
  8. extend Person { repeated Phone phones = 10;}
  9. enum PHONE_TYPE{
  10. MOBILE = 1;
  11. HOME = 2;
  12. }
  13. optional string num = 1;
  14. optional PHONE_TYPE type = 2;
  15. }

?