如何获得 Windows.Storage.Streams.IInputStream inputStream 长度?

How can I get Windows.Storage.Streams.IInputStream inputStream length?

我在 PC 上使用 HoloLens 2 作为客户端和 unity 服务器。 (有关此的更多讨论:)我丢失了调试 await reader1.LoadAsync(256);。我尝试了所有方法来获取我的流数据,但我做不到。我不想为缓冲区设置 const 值,我需要缓冲区的确切流大小。我对此进行了测试,它仅在缓冲区大小和数据流大小相等时才有效。或者你可以建议我其他方法?

// Create the StreamSocket and establish a connection to the server.
        using (var streamSocket = new Windows.Networking.Sockets.StreamSocket())
        {
            // The server hostname that we will be establishing a connection to.
            var hostName = new Windows.Networking.HostName(host);

            // client is trying to connect...

            await streamSocket.ConnectAsync(hostName, port);

            // client connected! 

            // Read data from the server.
            using (Windows.Storage.Streams.IInputStream inputStream = streamSocket.InputStream)
            {
                using (var reader1 = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    reader1.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.ReadAhead;
                    reader1.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    reader1.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;
                    
              
                    // Should be in stream size !!!
                    await reader1.LoadAsync(256);
                    
                    while (reader1.UnconsumedBufferLength > 0)
                    {
                        var bytes1 = new byte[reader1.UnconsumedBufferLength];
                        reader1.ReadBytes(bytes1);

                        // Handle byte array internally!
                        HandleData(bytes1);

                        await reader1.LoadAsync(256);
                    }
                    reader1.DetachStream();
                }
            }
        }
        // close socket
    }
    catch (Exception ex)
    {
        Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
    }

How can I get Windows.Storage.Streams.IInputStream inputStream length?

无法查找

IInputStream of SteamSocket。所以我们无法获得流的长度,这就是为什么我们需要设置一个缓冲区来一直加载输入流,直到流完成。

我检查了上面的代码,如果您将 InputStreamOptions 设置为 ReadAhead。它将进行下一步,当 256 缓冲区填满时,请尝试 InputStreamOptions as Partial.

reader1.InputStreamOptions = InputStreamOptions.ReadAhead;

更新

如果您想在加载前获取当前消息的长度,我们建议您将消息长度作为消息头写入steam。

例如

string stringToSend = "PC client uses System.Net.Sockets.TcpClient, System.Net.Sockets.NetworkStream but UWP (HoloLens) uses Windows.Networking.Sockets.StreamSocket.";
var bytes = Encoding.UTF8.GetBytes(stringToSend);
writer.WriteInt32(bytes.Length);
writer.WriteBytes(bytes);

接待客户

while (true)
{

    // Read first 4 bytes (length of the subsequent string).
    uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
    if (sizeFieldCount != sizeof(uint))
    {
        // The underlying socket was closed before we were able to read the whole data.
        return;
    }

    // Read the string.
    int bytesLength = reader.ReadInt32();
    uint Actualbytelength = await reader.LoadAsync((uint)bytesLength);
    if (Actualbytelength != bytesLength)
    {
        // The underlying socket was closed before we were able to read the whole data.
        return;
    }

    // Display the string on the screen. The event is invoked on a non-UI thread, so we need to marshal
    // the text back to the UI thread.

    var bytes = new byte[Actualbytelength];
    reader.ReadBytes(bytes);

}

嗨,尼科,抱歉更新晚了。我尝试了一切,但 TCP 对于带有 UWP 应用程序的 HoloLens 几乎是不可能的。所以我尝试了 UDP,它运行良好 (https://github.com/mbaytas/HoloLensUDP)。我希望微软在不久的将来为 HoloLens 1 和 2 提供 TCP 示例。