代码之家  ›  专栏  ›  技术社区  ›  TheCascadian

使用MixedRealityToolkit Unity在自定义消息中发送字节数组

  •  2
  • TheCascadian  · 技术社区  · 8 年前

    我正在为全息透镜开发一个共享应用程序,并尝试使用 Custom Messages.cs 包含在 MixedRealityToolkit-Unity .

    byte double , float int , long , short Xstring NetworkConnection.[Write/Read]() 重载函数,但我无法获取 NetworkMessage.ReadArray()

    我在网络上成功地发送了许多其他自定义消息,因此我知道网络连接不应该成为问题。


    正在编写消息

    public void SendByteArray(byte[] array)
    {
        // If connected to session, broadcast command
        if (serverConnection != null && serverConnection.IsConnected())
        {
            // Create an outgoing network message to contain all the info we want to send
            NetworkOutMessage msg = CreateMessage((byte)TestMessageID.SendArray);
    
            // Append Command
            msg.WriteArray(array, Convert.ToUInt32(array.Length));
    
            // Send the message as a broadcast, which will cause the server to forward it to all other users in the session.
            serverConnection.Broadcast(
            msg,
            MessagePriority.Immediate,
            MessageReliability.UnreliableSequenced,
            MessageChannel.Avatar);
        }
    }
    

    private byte[] OnArrayReceived(NetworkInMessage msg)
    {
        msg.ReadInt64(); // need to read UserID first, but isn't used for this function
        return msg.ReadArray();
    }
    

    根据签名,它看起来像是一个复制得不好的、重命名过的书写函数,但我不知道如何修改它。

    public virtual void ReadArray(byte[] data, uint arrayLength) {
        global::System.Runtime.InteropServices.GCHandle pinHandle_data = global::System.Runtime.InteropServices.GCHandle.Alloc(data, global::System.Runtime.InteropServices.GCHandleType.Pinned); try {
        {
          SharingClientPINVOKE.NetworkInMessage_ReadArray(swigCPtr, (global::System.IntPtr)pinHandle_data.AddrOfPinnedObject(), arrayLength);
        }
        } finally { pinHandle_data.Free(); }
    }
    

    以下是 NetworkInMessage_ReadArray()

    但随之而来的是这个警告。

    //------------------------------------------------------------------------------
    // <auto-generated />
    //
    // This file was automatically generated by SWIG (http://www.swig.org).
    // Version 3.0.10
    //
    // Do not make changes to this file unless you know what you are doing--modify
    // the SWIG interface file instead.
    //------------------------------------------------------------------------------
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   jdweng    8 年前

            const uint BUFFER_SIZE = 1024;
            private byte[] OnArrayReceived(NetworkInMessage msg)
            {
                msg.ReadInt64(); // need to read UserID first, but isn't used for this function
                // method 1
                byte[] data = new byte[BUFFER_SIZE];
                ReadArray(ref data, BUFFER_SIZE);
                return data;
    
                //method 2
                IntPtr dataptr = Marshal.AllocHGlobal(BUFFER_SIZE);
                ReadArray2(dataptr, BUFFER_SIZE);
    
                byte[] data2 = new byte[BUFFER_SIZE];
                Marshal.Copy(dataptr, data2, 0, BUFFER_SIZE);
                Marshal.FreeHGlobal(dataptr);
                return data2;
            }
    
            public void ReadArray(ref byte[] data, uint length)
            {
            }
            public void ReadArray2(IntPtr data, uint length)
            {
            }