C# 在Unity iOS中使用protobuf网络

C# 在Unity iOS中使用protobuf网络,c#,ios,xcode,unity3d,protobuf-net,C#,Ios,Xcode,Unity3d,Protobuf Net,我是一名iOS开发者,正在开发一款应用程序,它使用protobuf将Unity中生成的一些消息作为字节数组传递给iOS。我使用protobuf-net()时 MarcGravell参与了我的Unity项目(我不是Unity开发者) 我遵循回购协议自述中的说明。我正在从这里下载dll: 我像这样装饰我的班级: using ProtoBuf; [ProtoContract] public class AppToPortal { [ProtoMember(1)] public u

我是一名iOS开发者,正在开发一款应用程序,它使用protobuf将Unity中生成的一些消息作为字节数组传递给iOS。我使用protobuf-net()时 MarcGravell参与了我的Unity项目(我不是Unity开发者)

我遵循回购协议自述中的说明。我正在从这里下载dll:

我像这样装饰我的班级:

using ProtoBuf;  

[ProtoContract]
public class AppToPortal
{
    [ProtoMember(1)]
    public uint timestamp_sec { get; set; }
    [ProtoMember(2)]
    public Command command { get; set; }
    [ProtoMember(3)]
    public byte[] accessory_message { get; set; }
}

[ProtoContract]
public class PortalToApp
{
    [ProtoMember(1)]
    public uint timestamp_ms { get; set; }

    [ProtoMember(2)]
    public Event @event { get; set; }

    [ProtoMember(3)]
    public DeviceInfo info { get; set; }

    [ProtoMember(4)]
    public CommandResponse cmdRsp { get; set; }

    [ProtoMember(5)]
    public byte[] accessory_message { get; set; }
}

[ProtoContract]
public class Command
{
    [ProtoMember(1)]
    public CommandType type { get; set; }

    [ProtoMember(2)]
    public byte[] ota_signature { get; set; }

    [ProtoContract]
    public enum CommandType
    {
        ExampleModeOne = 1,
        ExampleModeTwo = 2,
        RequestDeviceInfo = 3,
        TestMode = 4,
        Reset = 5,
        StartOTA = 6,
    }
}  
我的序列化程序/反序列化程序如下所示

public class Proto
{
    public static byte[] Serialize<T>(T  obj) where T : class
    {
        if (null ==  obj) return null;
        try
        {
            using (var stream = new System.IO.MemoryStream())
            {
                ProtoBuf.Serializer.Serialize<T>(stream,  obj);
                return stream.ToArray();
            }
        }
        catch
        {
            // Log error
            throw;
        }
    }

    public static T Deserialize<T>(byte[] bytes) where T : class
    {
        if (null == bytes) return null;
        try
        {
            using (var stream = new System.IO.MemoryStream(bytes))
            {
                return ProtoBuf.Serializer.Deserialize<T>(stream);
            }
        }
        catch
        {
            // Log error
            throw;
        }
    }
}  
公共类协议
{
公共静态字节[]序列化(T obj),其中T:class
{
if(null==obj)返回null;
尝试
{
使用(var stream=new System.IO.MemoryStream())
{
ProtoBuf.Serializer.Serialize(流,obj);
返回流ToArray();
}
}
抓住
{
//日志错误
投掷;
}
}
公共静态T反序列化(字节[]字节),其中T:class
{
if(null==字节)返回null;
尝试
{
使用(var stream=new System.IO.MemoryStream(字节))
{
返回ProtoBuf.Serializer.Deserialize(流);
}
}
抓住
{
//日志错误
投掷;
}
}
}  
撰写邮件时,我正在执行以下操作:

// Compose message with protobuf
TimeSpan timestamp = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
Command command = new Command();
command.type = Command.CommandType.RequestDeviceInfo;

AppToPortal message = new AppToPortal();
message.command = command;
message.timestamp_sec = Convert.ToUInt32(timestamp.TotalSeconds);

Debug.Log(message);

// Serialize message
byte[] bytes = Proto.Serialize<AppToPortal>(message);  
PortalToApp message = Proto.Deserialize<PortalToApp>(bytes);
Debug.Log("Did deserialize: " + message + " - firmware_version: " + message.info.firmware_version + " - battery_level: " + message.info.battery_level);
//用protobuf编写消息
TimeSpan timestamp=DateTime.Now.Subtract(新的DateTime(1970,1,1,0,0,DateTimeKind.Utc));
命令=新命令();
command.type=command.CommandType.RequestDeviceInfo;
AppToPortal消息=新建AppToPortal();
message.command=命令;
message.timestamp_sec=Convert.ToUInt32(timestamp.TotalSeconds);
Debug.Log(消息);
//序列化消息
字节[]字节=协议序列化(消息);
当我反序列化接收到的字节数组时,我正在执行以下操作:

// Compose message with protobuf
TimeSpan timestamp = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
Command command = new Command();
command.type = Command.CommandType.RequestDeviceInfo;

AppToPortal message = new AppToPortal();
message.command = command;
message.timestamp_sec = Convert.ToUInt32(timestamp.TotalSeconds);

Debug.Log(message);

// Serialize message
byte[] bytes = Proto.Serialize<AppToPortal>(message);  
PortalToApp message = Proto.Deserialize<PortalToApp>(bytes);
Debug.Log("Did deserialize: " + message + " - firmware_version: " + message.info.firmware_version + " - battery_level: " + message.info.battery_level);
PortalToApp message=Proto.Deserialize(字节);
Debug.Log(“Did反序列化:“+message+”-固件版本:“+message.info.firmware\u版本+”-电池级别:“+message.info.battery\u级别”);
当我在Mac上运行这个程序时,我能够序列化/反序列化任何消息,一点错误都没有。但是,当我将Unity项目导出到iOS时,我在Xcode控制台中收到以下错误消息:

(Filename: /Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h Line: 43)

NotSupportedException: /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(19) :  Unsupported internal call for IL2CPP:DynamicMethod::create_dynamic_method - System.Reflection.Emit is not supported.
    at System.Reflection.Emit.DynamicMethod.CreateDynMethod () [0x00000] in <filename unknown>:0 
    at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Compiler.CompilerContext.BuildSerializer (IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer..ctor (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer.Wrap (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (Int32 key, System.Object value, ProtoBuf.ProtoReader source) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializer.Deserialize[T] (System.IO.Stream source) [0x00000] in <filename unknown>:0 
    at MCPP.Proto.Deserialize[T] (System.Byte[] bytes) [0x00000] in <filename unknown>:0 
    at MenuPanel.OnReceiveData (MCPP.Notification notification) [0x00000] in <filename unknown>:0 
    at MCPP.MCPPKit.PostNotification (MCPP.Notification aNotification) [0x00000] in <filename unknown>:0 
    at MCPP.IOSPlugin.didReceiveDataCallback (IntPtr ptr, Int32 length) [0x00000] in <filename unknown>:0 
Rethrow as InvalidOperationException: It was not possible to prepare a serializer for: MCPP.PortalToApp
    at ProtoBuf.Compiler.CompilerContext.BuildSerializer (IProtoSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer..ctor (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializers.CompiledSerializer.Wrap (IProtoTypeSerializer head, ProtoBuf.Meta.TypeModel model) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.MetaType.get_Serializer () [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.RuntimeTypeModel.Deserialize (Int32 key, System.Object value, ProtoBuf.ProtoReader source) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Meta.TypeModel.Deserialize (System.IO.Stream source, System.Object value, System.Type type, ProtoBuf.SerializationContext context) [0x00000] in <filename unknown>:0 
    at ProtoBuf.Serializer.Deserialize[T] (System.IO.Stream source) [0x00000] in <filename unknown>:0 
    at MCPP.Proto.Deserialize[T] (System.Byte[] bytes) [0x00000] in <filename unknown>:0 
    at MenuPanel.OnReceiveData (MCPP.Notification notification) [0x00000] in <filename unknown>:0 
    at MCPP.MCPPKit.PostNotification (MCPP.Notification aNotification) [0x00000] in <filename unknown>:0 
    at MCPP.IOSPlugin.didReceiveDataCallback (IntPtr ptr, Int32 length) [0x00000] in <filename unknown>:0 
MCPP.MCPPKit:PostNotification(Notification)
MCPP.IOSPlugin:didReceiveDataCallback(IntPtr, Int32)

(Filename: currently not available on il2cpp Line: -1)

NotSupportedException: /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(24) :  Unsupported internal call for IL2CPP:DynamicMethod::destroy_dynamic_method - System.Reflection.Emit is not supported.
    at System.Reflection.Emit.DynamicMethod.Finalize () [0x00000] in <filename unknown>:0 
UnityEngine.UnhandledExceptionHandler:PrintException(String, Exception)
UnityEngine.UnhandledExceptionHandler:HandleUnhandledException(Object, UnhandledExceptionEventArgs)

(Filename: currently not available on il2cpp Line: -1)
(文件名:/Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h行:43)
不支持异常:/Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(19):不支持对il2cpp的内部调用:DynamicMethod::create_dynamic_method-System.Reflection.Emit。
位于:0中的System.Reflection.Emit.DynamicMethod.CreateDynMethod()[0x00000]处
在:0中的System.Reflection.Emit.DynamicMethod.CreateDelegate(System.Type delegateType)[0x00000]处
在ProtoBuf.Compiler.CompilerContext.BuildSerializer(IProtoSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在ProtoBuf.Serializers.CompiledSerializer..ctor(IProtoTypeSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在ProtoBuf.Serializers.CompiledSerializer.Wrap(IProtoTypeSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在:0中的ProtoBuf.Meta.MetaType.get_序列化程序()[0x00000]处
在ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32键,System.Object值,ProtoBuf.ProtoReader源)[0x00000]中:0
在ProtoBuf.Meta.TypeModel.Deserialize(System.IO.Stream源、System.Object值、System.Type类型、ProtoBuf.SerializationContext上下文)[0x00000]处,输入:0
在ProtoBuf.Serializer.Deserialize[T](System.IO.Stream source)[0x00000]处,输入:0
在MCPP.Proto.Deserialize[T](System.Byte[]bytes)[0x00000]处,输入:0
在0中的MenuPanel.OnReceiveData(MCPP.Notification Notification)[0x00000]处
在0中的MCPP.mcpkit.PostNotification(MCPP.Notification aNotification)[0x00000]处
在MCPP.IOSPlugin.didReceiveDataCallback(IntPtr ptr,Int32长度)[0x00000]中:0
以InvalidOperationException重新显示:无法为:MCPP.PortalToApp准备序列化程序
在ProtoBuf.Compiler.CompilerContext.BuildSerializer(IProtoSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在ProtoBuf.Serializers.CompiledSerializer..ctor(IProtoTypeSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在ProtoBuf.Serializers.CompiledSerializer.Wrap(IProtoTypeSerializer头,ProtoBuf.Meta.TypeModel模型)[0x00000]中:0
在:0中的ProtoBuf.Meta.MetaType.get_序列化程序()[0x00000]处
在ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32键,System.Object值,ProtoBuf.ProtoReader源)[0x00000]中:0
在ProtoBuf.Meta.TypeModel.Deserialize(System.IO.Stream源、System.Object值、System.Type类型、ProtoBuf.SerializationContext上下文)[0x00000]处,输入:0
在ProtoBuf.Serializer.Deserialize[T](System.IO.Stream source)[0x00000]处,输入:0
在MCPP.Proto.Deserialize[T](System.Byte[]bytes)[0x00000]处,输入:0
在0中的MenuPanel.OnReceiveData(MCPP.Notification Notification)[0x00000]处
在0中的MCPP.mcpkit.PostNotification(MCPP.Notification aNotification)[0x00000]处
在MCPP.IOSPlugin.didReceiveDataCallback(IntPtr ptr,Int32长度)[0x00000]中:0
MCPP.mcpkit:PostNotification(通知)
IOSPlugin:didReceiveDataCallback(IntPtr,Int32)
(文件名:当前在il2cpp行中不可用:-1)
不支持异常:/Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/DynamicMethod.cpp(24):不支持对il2cpp的内部调用:DynamicMethod::destroy\u dynamic\u method-System.Reflection.Emit。
在0中的System.Reflection.Emit.DynamicMethod.Finalize()[0x00000]处
UnityEngine.UnhandledExceptionHandler:PrintException(字符串,异常)
UnityEngine.UnhandledExceptionHandler:HandleUnhandledException(对象,UnhandledExceptionEventArgs)
(文件名:当前在il2cpp行中不可用:-1)
显然是som