使用 MixedRealityToolkit-Unity 在自定义消息中发送字节数组
Using MixedRealityToolkit-Unity to send byte array in Custom Message
我正在为 HoloLens 开发共享应用程序,并尝试使用 MixedRealityToolkit-Unity 中包含的 Custom Messages.cs
脚本发送字节数组。
存储库附带 read/write 功能,适用于 byte
、double
、float
、int
、long
、short
,NetworkConnection.[Write/Read]()
重载函数中的 Xstring
,但我无法获得 NetworkMessage.ReadArray()
到 return 字节数组。它被列为无效函数,没有任何意义,而且我不知道如何在 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.
//------------------------------------------------------------------------------
回答 - 您应该需要的所有项目文件都包含在测试部分下的 repo(上面的链接)中。
代码是 return 将一个点返回到一个数组。因此,这两种方法中的一种应该有效。您需要分配一个足够大的数组来 return 所有数据。我制作了 1024 的阵列,你可以制作任何尺寸。如果数组必须位于托管或非托管内存中,我不确定是否看到了代码 space。所以第一种方法使用托管内存和第二种非托管内存。
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)
{
}
我正在为 HoloLens 开发共享应用程序,并尝试使用 MixedRealityToolkit-Unity 中包含的 Custom Messages.cs
脚本发送字节数组。
存储库附带 read/write 功能,适用于 byte
、double
、float
、int
、long
、short
,NetworkConnection.[Write/Read]()
重载函数中的 Xstring
,但我无法获得 NetworkMessage.ReadArray()
到 return 字节数组。它被列为无效函数,没有任何意义,而且我不知道如何在 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.
//------------------------------------------------------------------------------
回答 - 您应该需要的所有项目文件都包含在测试部分下的 repo(上面的链接)中。
代码是 return 将一个点返回到一个数组。因此,这两种方法中的一种应该有效。您需要分配一个足够大的数组来 return 所有数据。我制作了 1024 的阵列,你可以制作任何尺寸。如果数组必须位于托管或非托管内存中,我不确定是否看到了代码 space。所以第一种方法使用托管内存和第二种非托管内存。
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)
{
}