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



我正在为HoloLens开发一个共享应用程序,并尝试使用Mixed RealityToolkit-Unity中包含的Custom Messages.cs脚本发送字节数组。

存储库在NetworkConnection.[Write/Read]()重载函数中具有bytedoublefloatintlongshortXstring的读/写功能,但我无法让NetworkMessage.ReadArray()返回字节数组。它被列为 void 函数,没有任何意义,我不知道如何在 SWIG 文件中更改它。

我成功地通过网络发送了许多其他自定义消息,所以我知道网络连接应该不是问题。


撰写留言

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.
//------------------------------------------------------------------------------

应答 - 您应该需要的所有项目文件都包含在测试部分下的存储库(上面链接)中。

代码返回数组的点。 因此,两种方法之一应该有效。 您需要分配一个足够大的数组来返回所有数据。 我做了数组 1024 买,你可以做任何大小。 我不确定是否看到代码 如果阵列必须位于托管或非托管内存空间中。 因此,第一种方法使用托管内存,第二种方法使用非托管内存。

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)
{
}

最新更新