将数据从 Python 返回到 Unity 的问题 [英] Issue returning data from Python to Unity

查看:30
本文介绍了将数据从 Python 返回到 Unity 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是这个问题的演变:Issue Return header byte fromPython 到 Unity

This question is an evolution of this: Issue returning header byte from Python to Unity

现在头字节的问题解决了,但是我在循环过程中遇到了问题.

Now the issue about header byte is solved, but I have a problem during the loop.

数据传入良好但在n"之后次循环中断,因为接收一个 json 字符串,其中包含带有开/关括号的整个数据结构加上下一个带有开括号的数据结构,一些数据和图像:"一些数据和许多\0"除非右括号......而且json解析器显然会中断.为什么以及我能做什么?谢谢修改后的代码如下:

The data incoming good but after "n" times the loop break because receive a json string with an entire structure of data with open/close bracket PLUS the next structure of data with open bracket, some data and in the "image:" some data and many of the "\0" and unless the close bracket.. And the json parser obviously breaks. Why and what can I do? Thanks Below the code revised:

IEnumerator Client()
{
   while (!IsConnected(client))
   {
      try
      {
         client = new TcpClient(host, port);                 
         s = client.GetStream();                
              
         byte[] byteBuffer = Encoding.UTF8.GetBytes("Connected to client");               
         s.Write(byteBuffer, 0, byteBuffer.Length);

         while (true)
         {
            if (s.DataAvailable)
            {            

               while (s.DataAvailable)
               { 
                  var header = new byte[4];
                  s.Read(header, 0, header.Length);
    
                  var fileSize = BitConverter.ToUInt32(header,0);

                  StringBuilder myCompleteMessage = new StringBuilder();
                  MemoryStream ms = new MemoryStream();
                       
                  int increment = 0;

                  while (ms.Length < fileSize)
                  {
                     byte[] dataReceived = new byte[fileSize];
                     increment = s.Read(dataReceived, 0, dataReceived.Length);
    
                     ms.Write(dataReceived.Take(increment).ToArray(), 0, increment);
                  }
                       
                  myCompleteMessage.AppendFormat("{0}",   Encoding.UTF8.GetString(ms.GetBuffer()));

                  JSONNode data = JSONNode.Parse(myCompleteMessage.ToString());
               
                  ....
                  // Do work with myCompleteMessage

                  yield return null;
               }
            }
         }
      }
   }
}

推荐答案

我这里没有你的 Unity 环境,所以我无法对此进行测试.

I don't have your Unity environment here, so I can't have tested this.

像这样的循环(可能在协程中运行或不会阻止 Unity 的默认处理的东西)应该足以处理我们之前建立的形状的消息流(表示消息的 4 字节标头)长度,后面跟着那么多字节的 JSON).

Something like a loop (likely run in a coroutine or something that doesn't block Unity's default handling) like this should be enough to handle a stream of messages of the shape we established before (a 4-byte header signifying the message length, followed by that many bytes of JSON).

while (!IsConnected(client))
{
    try
    {
        client = new TcpClient(host, port);
        s = client.GetStream();
        while (true)
        {
            var data = ReadPacket(s);
            // Do work with data
        }
    }
}

ReadPacket 应该类似于

ReadPacket should be something like

JSONNode ReadPacket(Stream s)
{
    var buffer = new byte[131072];

    // Read 4 bytes (we will assume we can always read that much)
    var n = s.Read(buffer, 0, 4);
    if(n < 4) throw new Exception("short read");
    var fileSize = BitConverter.ToUInt32(buffer, 0);

    Debug.Print("Reading a blob of length", fileSize);

    // Memory stream to accumulate the reads into.    
    var ms = new MemoryStream();
    while (ms.Length < fileSize)
    {
        // Figure out how much we can read -- if we're near the end,
        // don't overread.
        var maxRead = Math.Min(buffer.Length, fileSize - ms.Length);
        var increment = s.Read(buffer, 0, maxRead);
        // ... and write them into the stream.
        ms.Write(buffer, 0, increment);
        Debug.Print("Read", ms.Length, "of", fileSize);
    }
    // Decode the bytes to UTF-8 and parse.
    return JSONNode.Parse(Encoding.UTF8.GetString(ms.GetBuffer()));
}

这篇关于将数据从 Python 返回到 Unity 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆